如何从PHP cli获取linux控制台$ COLUMNS和$ ROWS?

Sch*_*kie 21 php linux bash command-line command-line-interface

我正在为PHP创建一个新的整洁的CLI库,我想弄清楚它正在运行的控制台的宽度/高度.

我尝试了很多东西,比如挖掘$ _ENV,exec("echo $ COLUMNS")等,但是没有结果,而如果我在bash命令行中键入echo $ COLUMNS或$ ROWS,它会整齐地显示值.

从PHP访问此值需要做什么?

我正在使用这样的.sh脚本:

#!/usr/bin/php -q
<?php

require_once('lib.commandline.php');


class HelloWorld extends CommandLineApp {

  public function main($args) {

       echo('O, Hai.');

    }

}
Run Code Online (Sandbox Code Playgroud)

更新 最终解决方案

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];
      }
    }
Run Code Online (Sandbox Code Playgroud)

Pau*_*ce. 46

另一个不需要解析的shell选项是tput:

$this->settings['screen']['width'] = exec('tput cols')
$this->settings['screen']['height'] = exec('tput lines')
Run Code Online (Sandbox Code Playgroud)


Joe*_*erg 5

使用PHP ncurses_getmaxyx函数.

ncurses_getmaxyx (STDSCR, $Height, $Width)
Run Code Online (Sandbox Code Playgroud)

先前:

http://php.net/manual/en/function.getenv.php

$cols = getenv('COLUMNS');
$rows = getenv('ROWS');
Run Code Online (Sandbox Code Playgroud)

"正确"的方法可能是调用TIOCGSIZEioctl来获取内核对窗口大小的想法,或调用命令stty -a并解析结果rowscolumns