Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Table with unicode character #117

Merged
merged 4 commits into from
Nov 29, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 19 additions & 8 deletions src/Output/Table.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,11 @@
use function implode;
use function is_array;
use function max;
use function mb_strwidth;
adhocore marked this conversation as resolved.
Show resolved Hide resolved
use function mb_substr;
use function reset;
use function sprintf;
use function str_pad;
use function str_repeat;
use function strlen;
use function trim;

use const PHP_EOL;
Expand All @@ -51,7 +51,7 @@ public function render(array $rows, array $styles = []): string
$pos = 0;
foreach ($head as $col => $size) {
$dash[] = str_repeat('-', $size + 2);
$title[] = str_pad($this->toWords($col), $size, ' ');
$title[] = $this->strPad($this->toWords($col), $size, ' ');
$positions[$col] = ++$pos;
}

Expand All @@ -62,7 +62,6 @@ public function render(array $rows, array $styles = []): string
$parts = [];
$line++;

[$start, $end] = $styles[['even', 'odd'][(int) $odd]];
foreach ($head as $col => $size) {
$colNumber = $positions[$col];

Expand All @@ -85,10 +84,10 @@ public function render(array $rows, array $styles = []): string
$word = str_replace($matches[1], '', $text);
$word = preg_replace('/\\x1b\[0m/', '', $word);

$size += strlen($text) - strlen($word);
$size += mb_strwidth($text) - mb_strwidth($word);
}

$parts[] = "$start " . str_pad($text, $size, ' ') . " $end";
$parts[] = "$start " . $this->strPad($text, $size, ' ') . " $end";
}

$odd = !$odd;
Expand Down Expand Up @@ -132,8 +131,8 @@ protected function normalize(array $rows): array
return $col;
}, $cols);

$span = array_map('strlen', $cols);
$span[] = strlen($col);
$span = array_map('mb_strwidth', $cols);
$span[] = mb_strwidth($col);
$value = max($span);
}

Expand Down Expand Up @@ -177,4 +176,16 @@ protected function parseStyle(array|callable $style, $val, array $row, array $ta

return ['', ''];
}

/**
* Pad a multibyte string to a certain length with another multibyte string
*/
protected function strPad(string $string, int $length, string $pad_string = ' '): string
{
if (1 > $paddingRequired = $length - mb_strwidth($string)) {
return $string;
}

return $string . mb_substr(str_repeat($pad_string, $paddingRequired), 0, $paddingRequired);
}
}
2 changes: 1 addition & 1 deletion src/Output/Writer.php
Original file line number Diff line number Diff line change
Expand Up @@ -339,7 +339,7 @@ public function justify(string $first, ?string $second = null, array $options =

$second = (string) $second;
$terminalWidth = $this->terminal->width() ?? 80;
$dashWidth = $terminalWidth - (strlen($first) + strlen($second));
$dashWidth = $terminalWidth - (mb_strwidth($first) + mb_strwidth($second));
// remove left and right margins because we're going to add 1 space on each side (after/before the text).
// if we don't have a second element, we just remove the left margin
$dashWidth -= $second === '' ? 1 : 2;
Expand Down
22 changes: 22 additions & 0 deletions tests/Output/TableTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -639,4 +639,26 @@ public function test_render_with_html_like_tags_in_cell_content(): void

$this->assertSame($expectedOutput, trim($result));
}

public function test_render_with_unicode_characters_in_cell_content(): void
{
$rows = [
['name' => 'François', 'greeting' => 'Bonjour'],
['name' => 'Jürgen', 'greeting' => 'Guten Tag'],
['name' => '北京', 'greeting' => '你好']
];

$expectedOutput =
"+----------+-----------+" . PHP_EOL .
"| Name | Greeting |" . PHP_EOL .
"+----------+-----------+" . PHP_EOL .
"| François | Bonjour |" . PHP_EOL .
"| Jürgen | Guten Tag |" . PHP_EOL .
"| 北京 | 你好 |" . PHP_EOL .
"+----------+-----------+";

$result = $this->table->render($rows);

$this->assertSame($expectedOutput, trim($result));
}
}