1212use Inhere \Console \Component \MessageFormatter ;
1313use Inhere \Console \Console ;
1414use Toolkit \Cli \Color \ColorTag ;
15+ use Toolkit \Stdlib \Helper \DataHelper ;
1516use Toolkit \Stdlib \Str ;
1617use Toolkit \Stdlib \Str \StrBuffer ;
1718use function array_keys ;
@@ -112,63 +113,66 @@ public static function show(array $data, string $title = 'Data Table', array $op
112113 $ colBorderChar = $ opts ['colBorderChar ' ];
113114
114115 $ info = [
115- 'rowCount ' => count ($ data ),
116+ // 'rowCount' => count($data),
116117 'columnCount ' => 0 , // how many column in the table.
117118 'columnMaxWidth ' => [], // table column max width
118- 'tableWidth ' => 0 , // table width. equals to all max column width's sum.
119+ // 'tableWidth' => 0, // table width. equals to all max column width's sum.
119120 ];
120121
121122 // parse table data
122- foreach ($ data as $ row ) {
123+ foreach ($ data as & $ row ) {
123124 // collection all field name
124125 if ($ rowIndex === 0 ) {
125126 $ head = $ tableHead ?: array_keys ($ row );
126- //
127+ // column count
127128 $ info ['columnCount ' ] = count ($ row );
128129
129130 foreach ($ head as $ index => $ name ) {
130131 if (is_string ($ name )) {// maybe no column name.
131132 $ hasHead = true ;
132133 }
133134
134- $ info ['columnMaxWidth ' ][$ index ] = Str::utf8Len ($ name, ' UTF-8 ' );
135+ $ info ['columnMaxWidth ' ][$ index ] = Str::utf8Len ($ name );
135136 }
136137 }
137138
138139 $ colIndex = 0 ;
140+ $ rowData = (array )$ row ;
141+
142+ foreach ($ rowData as &$ value ) {
143+ // always convert to string
144+ $ value = DataHelper::toString ($ value );
139145
140- foreach ((array )$ row as $ value ) {
141146 // collection column max width
142147 if (isset ($ info ['columnMaxWidth ' ][$ colIndex ])) {
143- if (is_bool ($ value )) {
144- $ colWidth = $ value ? 4 : 5 ;
145- } else {
146- $ colWidth = Str::utf8Len ($ value , 'UTF-8 ' );
147- }
148+ $ colWidth = Str::utf8Len ($ value );
148149
149150 // If current column width gt old column width. override old width.
150151 if ($ colWidth > $ info ['columnMaxWidth ' ][$ colIndex ]) {
151152 $ info ['columnMaxWidth ' ][$ colIndex ] = $ colWidth ;
152153 }
153154 } else {
154- $ info ['columnMaxWidth ' ][$ colIndex ] = Str::utf8Len ($ value, ' UTF-8 ' );
155+ $ info ['columnMaxWidth ' ][$ colIndex ] = Str::utf8Len ($ value );
155156 }
156157
157158 $ colIndex ++;
158159 }
160+ unset($ value );
159161
160162 $ rowIndex ++;
163+ $ row = $ rowData ;
161164 }
165+ unset($ row );
162166
163- $ tableWidth = $ info ['tableWidth ' ] = array_sum ($ info ['columnMaxWidth ' ]);
164167 $ columnCount = $ info ['columnCount ' ];
168+ $ tableWidth = (int )array_sum ($ info ['columnMaxWidth ' ]);
165169
166170 // output title
167171 if ($ title ) {
168172 $ tStyle = $ opts ['titleStyle ' ] ?: 'bold ' ;
169173 $ title = Str::ucwords (trim ($ title ));
170- $ titleLength = Str::utf8Len ($ title, ' UTF-8 ' );
171- $ indentSpace = Str::pad (' ' , ceil ($ tableWidth / 2 ) - ceil ($ titleLength / 2 ) + ($ columnCount * 2 ), ' ' );
174+ $ titleLength = Str::utf8Len ($ title );
175+ $ indentSpace = Str::pad (' ' , ceil ($ tableWidth / 2 ) - ceil ($ titleLength / 2 ) + ($ columnCount * 2 ));
172176 $ buf ->write (" $ indentSpace< $ tStyle> $ title</ $ tStyle> \n" );
173177 }
174178
@@ -190,7 +194,7 @@ public static function show(array $data, string $title = 'Data Table', array $op
190194 // format head title
191195 // $name = Str::pad($name, $colMaxWidth, ' ');
192196 // use Str::padByWidth support zh-CN words
193- $ name = Str::padByWidth ($ name , $ colMaxWidth, ' ' );
197+ $ name = Str::padByWidth ($ name , $ colMaxWidth );
194198 $ name = ColorTag::wrap ($ name , $ opts ['headStyle ' ]);
195199 // join string
196200 $ headStr .= " $ name $ colBorderChar " ;
@@ -225,7 +229,7 @@ public static function show(array $data, string $title = 'Data Table', array $op
225229
226230 // $value = Str::pad($value, $colMaxWidth, ' ');
227231 // use Str::padByWidth support zh-CN words
228- $ value = Str::padByWidth ($ value , $ colMaxWidth, ' ' );
232+ $ value = Str::padByWidth ($ value , $ colMaxWidth );
229233 $ value = ColorTag::wrap ($ value , $ opts ['bodyStyle ' ]);
230234 $ rowStr .= " $ value $ colBorderChar " ;
231235 $ colIndex ++;
0 commit comments