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.');
    }
}
更新 最终解决方案
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];
      }
    }
Pau*_*ce. 46
另一个不需要解析的shell选项是tput:
$this->settings['screen']['width'] = exec('tput cols')
$this->settings['screen']['height'] = exec('tput lines')
使用PHP ncurses_getmaxyx函数.
ncurses_getmaxyx (STDSCR, $Height, $Width)
先前:
http://php.net/manual/en/function.getenv.php
$cols = getenv('COLUMNS');
$rows = getenv('ROWS');
"正确"的方法可能是调用TIOCGSIZEioctl来获取内核对窗口大小的想法,或调用命令stty -a并解析结果rows和columns