Core\Cli::displayWidth
Counts how many terminal columns a text needs.
A terminal writes text into a grid of columns. Most characters take one column. A Chinese, Japanese or Korean character takes two, an emoji takes two, and a combining mark takes none. Core\Cli::displayWidth counts the columns the terminal will use, so your program can pad a table column, draw a box or shorten a line and have it come out straight. Counting characters instead makes a table of mixed scripts come out crooked.
Two characters are measured by what they do to the cursor. A tab moves to the next column that is a multiple of eight. A newline ends the row, and the result is then the width of the widest row.
Good to know: the text may be tainted. A column count is a number, so nothing of what was measured comes back out of it.
The examples below measure a label, compare characters with columns, and line up a table.
Parameters
$valuestringThe text to measure. Ataintedvalue is allowed, because a column count contains nothing from the text.
Return value
Returns uint. The column count. A tab moves to the next multiple of eight, and a newline ends the row. For text with several rows, the result is the width of the widest row. That is the width to pad a box to.
Examples
Measure a label
<?nvs// Shows how to measure how much room a text needs at the terminal.// `Core\Cli::displayWidth` counts columns, and some characters take two of them.
array<string> $labels = ['Order', '注文', '🎧'];
// This prints 5, then 4, then 2.foreach ($labels as string $label) { echo Core\Cli::displayWidth($label), "\n";}542Columns are not characters
<?nvs// Shows the difference between characters and columns. `Core\Str::length`// counts characters. `Core\Cli::displayWidth` counts the columns a terminal// gives them. The two numbers are not always the same.
string $japanese = '注文';echo '注文 is ', Core\Str::length($japanese), ' characters and ';echo Core\Cli::displayWidth($japanese), " columns\n";
// A tab moves to the next column that is a multiple of eight. The letter "a",// a tab and the letter "b" take 9 columns together.string $tabbed = "a\tb";echo 'a<tab>b is ', Core\Str::length($tabbed), ' characters and ';echo Core\Cli::displayWidth($tabbed), " columns\n";注文 is 2 characters and 4 columnsa<tab>b is 3 characters and 9 columnsLine up a table
<?nvs// Shows the everyday use: a table whose columns line up at the terminal. Each// name is padded to the width of the widest one, measured in columns.
array<string> $names = ['Headphones', '注文', 'Café blend'];array<uint> $prices = [49, 120, 12];
uint $widest = 0;foreach ($names as string $name) { uint $width = Core\Cli::displayWidth($name); if ($width > $widest) { $widest = $width; }}
int $row = 0;while ($row < 3) { string $label = $names[$row]; // The padding is the room the name does not use. "注文" is 2 characters and // 4 columns, so counting characters would pad that row two columns too far. string $padding = Core\Str::repeat(' ', $widest - Core\Cli::displayWidth($label)); echo $label, $padding, ' ', $prices[$row], "\n"; $row = $row + 1;}Headphones 49注文 120Café blend 12