Reputation: 9401
I'm currently creating a new neat CLI library for PHP, and i'd like to figure out the width/height of the console it's running in.
I've tried many things like digging through $_ENV, exec("echo $COLUMNS"), etc, but no result, while if i type echo $COLUMNS or $ROWS in bash commandline, it neatly displays the value.
What do i need to do to access this value from PHP?
I'm using .sh scripts like this:
#!/usr/bin/php -q
<?php
require_once('lib.commandline.php');
class HelloWorld extends CommandLineApp {
public function main($args) {
echo('O, Hai.');
}
}
Update Final solution:
public function getScreenSize() {
preg_match_all("/rows.([0-9]+);.columns.([0-9]+);/", strtolower(exec('stty -a |grep columns')), $output);
if(sizeof($output) == 3) {
$this->settings['screen']['width'] = $output[1][0];
$this->settings['screen']['height'] = $output[2][0];
}
}
Upvotes: 28
Views: 15032
Reputation: 6854
For the record, it is my function (PHP 7.2 and higher):
It works in Windows and Linux. In case of errors, it returns 80.
In the case of some wacky or too small value, it returns the minimum value (80)
ps: "mode con" does return the number of lines but it is based on the buffer than on the screen size.
ps2: Oddly but if I run it as a website, it returns 120 (Windows Host).
function calculateColSize($min=80) {
try {
if (PHP_OS_FAMILY === 'Windows') {
$a1 = shell_exec('mode con');
/*
* Estado para dispositivo CON:
* ----------------------------
* Líneas: 9001
* Columnas: 85
* Ritmo del teclado: 31
* Retardo del teclado: 1
* Página de códigos: 65001
*/
$arr = explode("\n", $a1);
$col = trim(explode(':', $arr[4])[1]);
} else {
$col = exec('tput cols');
/*
* 184
*/
}
} catch (Exception $ex) {
$col = 80;
}
}
Upvotes: 2
Reputation: 34592
Maybe this link might be the answer, you could use the ANSI Escape codes to do that, by using the echo
using the specific Escape code sequence, in particular the 'Query Device', which I found another link here that explains in detail. Perhaps using that might enable you to get the columns and rows of the screen...
Upvotes: 1
Reputation: 721
I dunno, why one should ever need grep
to parse stty
output: it does have a separate option to report "the number of rows and columns according to the kernel".
One-liner, no error handling:
list($rows, $cols) = explode(' ', exec('stty size'));
One-liner, assume both rows/cols to be 0 in case of problems and suppress any error output:
list($rows, $cols) = explode(' ', @exec('stty size 2>/dev/null') ?: '0 0');
Upvotes: 5
Reputation: 359985
Another shell option that requires no parsing is tput
:
$this->settings['screen']['width'] = exec('tput cols')
$this->settings['screen']['height'] = exec('tput lines')
Upvotes: 57
Reputation: 26699
Use the PHP ncurses_getmaxyx
function.
ncurses_getmaxyx (STDSCR, $Height, $Width)
PREVIOUSLY:
http://php.net/manual/en/function.getenv.php
$cols = getenv('COLUMNS');
$rows = getenv('ROWS');
The "proper" way is probably to call the TIOCGSIZE
ioctl to get the kernel's idea of the window size, or call the command stty -a
and parse the results for rows
and columns
Upvotes: 6
Reputation: 9305
$COLUMNS
and $LINES
is probably not being exported to your program. You can run export LINES COLUMNS
before running your app, or you can get this information directly:
$fp=popen("resize", "r");
$b=stream_get_contents($fp);
preg_match("/COLUMNS=([0-9]+)/", $b, $matches);$columns = $matches[1];
preg_match("/LINES=([0-9]+)/", $b, $matches);$rows = $matches[1];
pclose($fp);
Upvotes: 3
Reputation: 15969
Environment variables can be found in the $_ENV super global variable.
echo $_ENV['ROWS'];
for instance.
Upvotes: -2