Skip to content

Core\Cli::height

Core\Cli::height(): uint

The terminal's height, in rows.

A row is one line on screen. Core\Cli::height tells you how many lines the window shows at once, so your program can print one page at a time, keep a heading in view, or stop a long list from scrolling away before anybody reads it.

The height is read once, when the program starts, in the same step as the width. Two calls in one run always agree.

Good to know: the result is never 0, so you can take a heading and a footer off it without checking first. When no standard stream is a terminal the result is 24, which is the height these examples print for.

The examples below print the height, leave room for a heading and a footer, and show only as many rows of a report as fit on the screen.

Return value

Returns uint. The row count, or 24 when no standard stream is a terminal. It is never 0, as for width.

Examples

Print the height

<?nvs
// Shows how to read the terminal's height. `Core\Cli::height` returns the
// number of rows the window shows at once.
uint $rows = Core\Cli::height();
// This prints 24, because this program runs without a terminal and 24 is the
// height it falls back to.
echo 'the screen shows ', $rows, " rows\n";
Output
the screen shows 24 rows

Leave room for a heading and a footer

<?nvs
// Shows how to work out how many lines are left for the content. A heading and
// a footer take one row each, and the rest of the screen is yours.
// `Core\Cli::height` is never 0, so this subtraction needs no check.
uint $content = Core\Cli::height() - 2;
echo "== orders ==\n";
echo 'there is room for ', $content, " lines between these two\n";
echo "== end ==\n";
Output
== orders ==
there is room for 22 lines between these two
== end ==

Show only the rows that fit

<?nvs
// Shows the everyday use: a report has more rows than the screen, so the
// program prints one page and says how many rows are left.
uint $orders = 100;
// One row for the heading and one for the last line, so the rest is the page.
uint $page = Core\Cli::height() - 2;
echo "orders\n";
uint $row = 0;
while ($row < $page && $row < $orders) {
echo ' order #', 1000 + $row, "\n";
$row = $row + 1;
}
echo $orders - $row, " more, run with --all to see them\n";
Output
orders
order #1000
order #1001
order #1002
order #1003
order #1004
order #1005
order #1006
order #1007
order #1008
order #1009
order #1010
order #1011
order #1012
order #1013
order #1014
order #1015
order #1016
order #1017
order #1018
order #1019
order #1020
order #1021
78 more, run with --all to see them