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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2203437/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-17 21:38:11  来源:igfitidea点击:

How to get linux console $COLUMNS and $ROWS from PHP cli?

phplinuxbashcommand-linecommand-line-interface

提问by SchizoDuckie

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.

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

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.

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

What do i need to do to access this value from PHP?

我需要做什么才能从 PHP 访问这个值?

I'm using .sh scripts like this:

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

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

require_once('lib.commandline.php');


class HelloWorld extends CommandLineApp {

  public function main($args) {

       echo('O, Hai.');

    }

}

UpdateFinal 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];
      }
    }

回答by Paused until further notice.

Another shell option that requires no parsing is tput:

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

$this->settings['screen']['width'] = exec('tput cols')
$this->settings['screen']['height'] = exec('tput lines')

回答by Joe Koberg

Use the PHP ncurses_getmaxyxfunction.

使用 PHPncurses_getmaxyx函数。

ncurses_getmaxyx (STDSCR, $Height, $Width)

PREVIOUSLY:

之前:

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

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

$cols = getenv('COLUMNS');
$rows = getenv('ROWS');

The "proper" way is probably to call the TIOCGSIZEioctl to get the kernel's idea of the window size, or call the command stty -aand parse the results for rowsand columns

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

回答by geocar

$COLUMNSand $LINESis probably not being exported to your program. You can run export LINES COLUMNSbefore running your app, or you can get this information directly:

$COLUMNS并且$LINES可能没有被导出到您的程序中。您可以export LINES COLUMNS在运行应用程序之前运行,也可以直接获取此信息:

$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);

回答by t0mm13b

Maybe this linkmight be the answer, you could use the ANSI Escape codes to do that, by using the echousing the specific Escape code sequence, in particular the 'Query Device', which I found another link herethat explains in detail. Perhaps using that might enable you to get the columns and rows of the screen...

也许这个链接可能是答案,你可以使用 ANSI 转义码来做到这一点,通过使用echo特定的转义码序列,特别是“查询设备”,我在这里找到了另一个详细解释的链接。也许使用它可以让您获得屏幕的列和行......

回答by Alex Offshore

I dunno, why one should ever need grepto parse sttyoutput: it does have a separate option to report "the number of rows and columns according to the kernel".

我不知道,为什么需要grep解析stty输出:它确实有一个单独的选项来报告“根据内核的行数和列数”。

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:

单行,假设两行/列都为 0 以防出现问题并抑制任何错误输出:

list($rows, $cols) = explode(' ', @exec('stty size 2>/dev/null') ?: '0 0');

回答by johannes

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

环境变量可以在 $_ENV 超级全局变量中找到。

echo $_ENV['ROWS'];

for instance.

例如。