Skip to content

Commit 6ccd82f

Browse files
committed
patch: provide fallback for mb_funcs when mbstring ext isn't loaded
1 parent 628ede7 commit 6ccd82f

File tree

3 files changed

+37
-9
lines changed

3 files changed

+37
-9
lines changed

src/Helper/InflectsString.php

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,11 @@
1212
namespace Ahc\Cli\Helper;
1313

1414
use function lcfirst;
15+
use function mb_strwidth;
16+
use function mb_substr;
1517
use function str_replace;
18+
use function strlen;
19+
use function substr;
1620
use function trim;
1721
use function ucwords;
1822

@@ -47,4 +51,28 @@ public function toWords(string $string): string
4751

4852
return ucwords($words);
4953
}
54+
55+
/**
56+
* Return width of string
57+
*/
58+
public function strwidth(string $string): int
59+
{
60+
if (function_exists('mb_strwidth')) {
61+
return mb_strwidth($string);
62+
}
63+
64+
return strlen($string);
65+
}
66+
67+
/**
68+
* Get part of string
69+
*/
70+
public function substr(string $string, int $start, ?int $length = null): string
71+
{
72+
if (function_exists('mb_substr')) {
73+
return mb_substr($string, $start, $length);
74+
}
75+
76+
return substr($string, $start, $length);
77+
}
5078
}

src/Output/Table.php

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,6 @@
2323
use function implode;
2424
use function is_array;
2525
use function max;
26-
use function mb_strwidth;
27-
use function mb_substr;
2826
use function reset;
2927
use function sprintf;
3028
use function str_repeat;
@@ -84,7 +82,7 @@ public function render(array $rows, array $styles = []): string
8482
$word = str_replace($matches[1], '', $text);
8583
$word = preg_replace('/\\x1b\[0m/', '', $word);
8684

87-
$size += mb_strwidth($text) - mb_strwidth($word);
85+
$size += $this->strwidth($text) - $this->strwidth($word);
8886
}
8987

9088
$parts[] = "$start " . $this->strPad($text, $size, ' ') . " $end";
@@ -131,8 +129,8 @@ protected function normalize(array $rows): array
131129
return $col;
132130
}, $cols);
133131

134-
$span = array_map('mb_strwidth', $cols);
135-
$span[] = mb_strwidth($col);
132+
$span = array_map([$this, 'strwidth'], $cols);
133+
$span[] = $this->strwidth($col);
136134
$value = max($span);
137135
}
138136

@@ -182,10 +180,10 @@ protected function parseStyle(array|callable $style, $val, array $row, array $ta
182180
*/
183181
protected function strPad(string $string, int $length, string $pad_string = ' '): string
184182
{
185-
if (1 > $paddingRequired = $length - mb_strwidth($string)) {
183+
if (1 > $paddingRequired = $length - $this->strwidth($string)) {
186184
return $string;
187185
}
188186

189-
return $string . mb_substr(str_repeat($pad_string, $paddingRequired), 0, $paddingRequired);
187+
return $string . $this->substr(str_repeat($pad_string, $paddingRequired), 0, $paddingRequired);
190188
}
191189
}

src/Output/Writer.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
namespace Ahc\Cli\Output;
1313

14+
use Ahc\Cli\Helper\InflectsString;
1415
use Ahc\Cli\Helper\Terminal;
1516

1617
use function fopen;
@@ -19,7 +20,6 @@
1920
use function method_exists;
2021
use function str_repeat;
2122
use function stripos;
22-
use function strlen;
2323
use function strpos;
2424
use function ucfirst;
2525

@@ -190,6 +190,8 @@
190190
*/
191191
class Writer
192192
{
193+
use InflectsString;
194+
193195
/** @var resource Output file handle */
194196
protected $stream;
195197

@@ -339,7 +341,7 @@ public function justify(string $first, ?string $second = null, array $options =
339341

340342
$second = (string) $second;
341343
$terminalWidth = $this->terminal->width() ?? 80;
342-
$dashWidth = $terminalWidth - (mb_strwidth($first) + mb_strwidth($second));
344+
$dashWidth = $terminalWidth - ($this->strwidth($first) + $this->strwidth($second));
343345
// remove left and right margins because we're going to add 1 space on each side (after/before the text).
344346
// if we don't have a second element, we just remove the left margin
345347
$dashWidth -= $second === '' ? 1 : 2;

0 commit comments

Comments
 (0)