2525use function max ;
2626use function reset ;
2727use function sprintf ;
28- use function str_pad ;
2928use function str_repeat ;
30- use function strlen ;
3129use function trim ;
3230
3331use const PHP_EOL ;
@@ -51,7 +49,7 @@ public function render(array $rows, array $styles = []): string
5149 $ pos = 0 ;
5250 foreach ($ head as $ col => $ size ) {
5351 $ dash [] = str_repeat ('- ' , $ size + 2 );
54- $ title [] = str_pad ($ this ->toWords ($ col ), $ size , ' ' );
52+ $ title [] = $ this -> strPad ($ this ->toWords ($ col ), $ size , ' ' );
5553 $ positions [$ col ] = ++$ pos ;
5654 }
5755
@@ -62,7 +60,6 @@ public function render(array $rows, array $styles = []): string
6260 $ parts = [];
6361 $ line ++;
6462
65- [$ start , $ end ] = $ styles [['even ' , 'odd ' ][(int ) $ odd ]];
6663 foreach ($ head as $ col => $ size ) {
6764 $ colNumber = $ positions [$ col ];
6865
@@ -85,10 +82,10 @@ public function render(array $rows, array $styles = []): string
8582 $ word = str_replace ($ matches [1 ], '' , $ text );
8683 $ word = preg_replace ('/ \\x1b\[0m/ ' , '' , $ word );
8784
88- $ size += strlen ($ text ) - strlen ($ word );
85+ $ size += $ this -> strwidth ($ text ) - $ this -> strwidth ($ word );
8986 }
9087
91- $ parts [] = "$ start " . str_pad ($ text , $ size , ' ' ) . " $ end " ;
88+ $ parts [] = "$ start " . $ this -> strPad ($ text , $ size , ' ' ) . " $ end " ;
9289 }
9390
9491 $ odd = !$ odd ;
@@ -132,8 +129,8 @@ protected function normalize(array $rows): array
132129 return $ col ;
133130 }, $ cols );
134131
135- $ span = array_map (' strlen ' , $ cols );
136- $ span [] = strlen ($ col );
132+ $ span = array_map ([ $ this , ' strwidth ' ] , $ cols );
133+ $ span [] = $ this -> strwidth ($ col );
137134 $ value = max ($ span );
138135 }
139136
@@ -177,4 +174,16 @@ protected function parseStyle(array|callable $style, $val, array $row, array $ta
177174
178175 return ['' , '' ];
179176 }
177+
178+ /**
179+ * Pad a multibyte string to a certain length with another multibyte string
180+ */
181+ protected function strPad (string $ string , int $ length , string $ pad_string = ' ' ): string
182+ {
183+ if (1 > $ paddingRequired = $ length - $ this ->strwidth ($ string )) {
184+ return $ string ;
185+ }
186+
187+ return $ string . $ this ->substr (str_repeat ($ pad_string , $ paddingRequired ), 0 , $ paddingRequired );
188+ }
180189}
0 commit comments