Skip to content

Commit eb1d449

Browse files
committed
up: use Str::padByWidth support zh-CN words on render table
1 parent 5d46a87 commit eb1d449

File tree

1 file changed

+26
-13
lines changed

1 file changed

+26
-13
lines changed

src/Component/Formatter/Table.php

Lines changed: 26 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,8 @@
1818
use function array_sum;
1919
use function ceil;
2020
use function count;
21+
use function is_bool;
2122
use function is_string;
22-
use function mb_strlen;
2323
use function ucwords;
2424

2525
/**
@@ -131,7 +131,7 @@ public static function show(array $data, string $title = 'Data Table', array $op
131131
$hasHead = true;
132132
}
133133

134-
$info['columnMaxWidth'][$index] = mb_strlen($name, 'UTF-8');
134+
$info['columnMaxWidth'][$index] = Str::utf8Len($name, 'UTF-8');
135135
}
136136
}
137137

@@ -140,14 +140,18 @@ public static function show(array $data, string $title = 'Data Table', array $op
140140
foreach ((array)$row as $value) {
141141
// collection column max width
142142
if (isset($info['columnMaxWidth'][$colIndex])) {
143-
$colWidth = mb_strlen($value, 'UTF-8');
143+
if (is_bool($value)) {
144+
$colWidth = $value ? 4 : 5;
145+
} else {
146+
$colWidth = Str::utf8Len($value, 'UTF-8');
147+
}
144148

145149
// If current column width gt old column width. override old width.
146150
if ($colWidth > $info['columnMaxWidth'][$colIndex]) {
147151
$info['columnMaxWidth'][$colIndex] = $colWidth;
148152
}
149153
} else {
150-
$info['columnMaxWidth'][$colIndex] = mb_strlen($value, 'UTF-8');
154+
$info['columnMaxWidth'][$colIndex] = Str::utf8Len($value, 'UTF-8');
151155
}
152156

153157
$colIndex++;
@@ -163,7 +167,7 @@ public static function show(array $data, string $title = 'Data Table', array $op
163167
if ($title) {
164168
$tStyle = $opts['titleStyle'] ?: 'bold';
165169
$title = ucwords(trim($title));
166-
$titleLength = mb_strlen($title, 'UTF-8');
170+
$titleLength = Str::utf8Len($title, 'UTF-8');
167171
$indentSpace = Str::pad(' ', ceil($tableWidth / 2) - ceil($titleLength / 2) + ($columnCount * 2), ' ');
168172
$buf->write(" {$indentSpace}<$tStyle>{$title}</$tStyle>\n");
169173
}
@@ -183,9 +187,12 @@ public static function show(array $data, string $title = 'Data Table', array $op
183187

184188
foreach ($head as $index => $name) {
185189
$colMaxWidth = $info['columnMaxWidth'][$index];
186-
// format
187-
$name = Str::pad($name, $colMaxWidth, ' ');
188-
$name = ColorTag::wrap($name, $opts['headStyle']);
190+
// format head title
191+
// $name = Str::pad($name, $colMaxWidth, ' ');
192+
// use Str::padByWidth support zh-CN words
193+
$name = Str::padByWidth($name, $colMaxWidth, ' ');
194+
$name = ColorTag::wrap($name, $opts['headStyle']);
195+
// join string
189196
$headStr .= " {$name} {$colBorderChar}";
190197
}
191198

@@ -194,10 +201,10 @@ public static function show(array $data, string $title = 'Data Table', array $op
194201
// head border: split head and body
195202
if ($headBorderChar = $opts['headBorderChar']) {
196203
$headBorder = $leftIndent . Str::pad(
197-
$headBorderChar,
198-
$tableWidth + ($columnCount * 3) + 2,
199-
$headBorderChar
200-
);
204+
$headBorderChar,
205+
$tableWidth + ($columnCount * 3) + 2,
206+
$headBorderChar
207+
);
201208
$buf->write($headBorder . "\n");
202209
}
203210
}
@@ -212,7 +219,13 @@ public static function show(array $data, string $title = 'Data Table', array $op
212219
foreach ((array)$row as $value) {
213220
$colMaxWidth = $info['columnMaxWidth'][$colIndex];
214221
// format
215-
$value = Str::pad($value, $colMaxWidth, ' ');
222+
if (is_bool($value)) {
223+
$value = $value ? 'TRUE' : 'FALSE';
224+
}
225+
226+
// $value = Str::pad($value, $colMaxWidth, ' ');
227+
// use Str::padByWidth support zh-CN words
228+
$value = Str::padByWidth($value, $colMaxWidth, ' ');
216229
$value = ColorTag::wrap($value, $opts['bodyStyle']);
217230
$rowStr .= " {$value} {$colBorderChar}";
218231
$colIndex++;

0 commit comments

Comments
 (0)