Skip to content

Core\Cli::width

Core\Cli::width(): uint

The terminal's width, in columns.

A column is one character cell on screen. Core\Cli::width tells you how many of them fit on a line, so your program can wrap a paragraph, shorten a long path, or draw a bar that reaches the edge and no further.

The width is read once, when the program starts. Two calls in one run always agree, so a line you measured and a line you printed cannot disagree about how wide the screen is.

Good to know: the result is never 0, so you can subtract a margin from it without checking first. When no standard stream is a terminal the result is 80, which is the width these examples print for.

The examples below print the width, draw a line across the screen, and shorten a long path so the line does not wrap.

Return value

Returns uint. The column count, or 80 when no standard stream is a terminal. Never 0, so a caller may subtract a margin from it without checking.

Examples

Print the width

<?nvs
// Shows how to read the terminal's width. `Core\Cli::width` returns the number
// of columns that fit on one line.
uint $columns = Core\Cli::width();
// This prints 80, because this program runs without a terminal and 80 is the
// width it falls back to.
echo 'the screen is ', $columns, " columns wide\n";
Output
the screen is 80 columns wide

Draw a line across the screen

<?nvs
// Shows how to draw a line that reaches the edge of the screen and no further.
// The heading takes room too, so its width is taken off first.
string $heading = ' orders ';
// `Core\Cli::width` is never 0, so this subtraction needs no check.
uint $rule = Core\Cli::width() - Core\Str::length($heading);
// The heading and the line together are exactly as wide as the screen.
echo $heading, Core\Str::repeat('-', $rule), "\n";
Output
orders ------------------------------------------------------------------------

Shorten a long path

<?nvs
// Shows the everyday use: a program prints a path that may be longer than the
// screen. A line that wraps is hard to read, so the start of the path is cut
// off and three dots take its place.
string $path = '/var/lib/orders/2026/09/customers/europe/germany/berlin/invoice-4821.json';
uint $room = Core\Cli::width() - Core\Str::length('reading ');
string $shown = $path;
if (Core\Str::length($path) > $room) {
// The end of a path names the file, so that is the part to keep.
int $keep = ($room - 3) as int;
$shown = '...' . Core\Str::slice($path, (Core\Str::length($path) as int) - $keep, $keep);
}
echo 'reading ', $shown, "\n";
Output
reading .../lib/orders/2026/09/customers/europe/germany/berlin/invoice-4821.json