views:

406

answers:

5

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];
      }
    }
+3  A: 

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

Joe Koberg
@Joe: Dang!!!! I totally forgot about that and posted a crappy answer!!! :) LOL
tommieb75
Jeffrey Aylesworth
The environment variables are also not updated if the terminal is resized while your program is running.
ephemient
The comments on that PHP manual page are very helpful.
Joe Koberg
Getenv doesn't work indeed, nor does $_ENV But thanks for the hint to stty -a, i'll parse that :)
SchizoDuckie
I like the portability of my current solution :) Not all boxes have ncurses in PHP so i'd like it to be as bare as possible
SchizoDuckie
A: 

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...

Hope this helps, Best regards, Tom.

tommieb75
A: 

Environment variables can be found in the $_ENV super global variable.

echo $_ENV['ROWS'];

for instance.

johannes
+1  A: 

$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);
geocar
+6  A: 

Another shell option that requires no parsing is tput:

$this->settings['screen']['width'] = exec('tput cols')
$this->settings['screen']['height'] = exec('tput lines')
Dennis Williamson
excellent. I totally forgot about `tput` while digging for how to get just that info from `stty`.
Joe Koberg
Awesome, even better!
SchizoDuckie