With my new, neat CLI library for PHP, and I'd like to get the width and height of the console (TTY) the PHP script is running in.
I've tried many things like digging through _ENV, exec("echo $COLUMNS"), but no result despite when I type the following listing into my bash(1) command line
$ bash
$ echo $COLUMNS or $ROWS
80 or
it neatly displays the value (at least of COLUMNS).
What do I need to do to access both parameter's values from PHP?
I'm using the PHP CGI SAPI as an executable script like this:
#!/usr/bin/php -q
<?php # --no-header
# -q Quiet-mode. Suppress HTTP header output (CGI only).
# cf. php(1)
require_once ('lib.commandline.php');
class HelloWorld extends CommandLineApp
{
public function main($args)
{
echo ('O, Hai.');
}
}
My Answer in the Question
My final solution is that stty(1) gets the parameters from the teletype terminal, these are no shell environment variables.
It requires the PHP pcre extension though (often available, check with php -m | grep -F pcre):
public function getScreenSize()
{
preg_match_all(
"/rows.([0-9]+);.columns.([0-9]+);/",
strtolower(
exec('stty -a |grep columns')
),
$output
);
if (count($output) == 3)
{
$this->settings['screen']['width'] = $output[1][0];
$this->settings['screen']['height'] = $output[2][0];
}
}
Another shell option that requires no parsing is tput
:
$this->settings['screen']['width'] = exec('tput cols');
$this->settings['screen']['height'] = exec('tput lines');
And as scanning into integers and a single tput (subshell) invocation:
[$this->settings['screen']['width']
,$this->settings['screen']['height']] = sscanf(`tput cols lines`, ' %d %d');