我目前正在为 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.');
}
}
Update最终解决方案:
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];
}
}
另一个不需要解析的 shell 选项是tput
:
$this->settings['screen']['width'] = exec('tput cols')
$this->settings['screen']['height'] = exec('tput lines')
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)