add terminal size caching

This commit is contained in:
crazywhalecc 2022-05-13 17:18:26 +08:00
parent c14e6b75b8
commit bcc6a9b4e4
2 changed files with 14 additions and 9 deletions

View File

@ -10,7 +10,7 @@ $data = [
'php_version_id' => PHP_VERSION_ID, 'php_version_id' => PHP_VERSION_ID,
'info' => '并且支持中文!!', 'info' => '并且支持中文!!',
'path2' => __DIR__, 'path2' => __DIR__,
'long' => '我可以' . str_repeat('很长', 50), '中文级别的key' => '我可以' . str_repeat('很长', 50),
]; ];
(new \ZM\Logger\TablePrinter($data)) (new \ZM\Logger\TablePrinter($data))

View File

@ -41,6 +41,8 @@ class TablePrinter
*/ */
protected $row_overflow_hide = false; protected $row_overflow_hide = false;
protected $terminal_size;
public function __construct(array $params, string $head = '=', string $foot = '=', int $max_border_length = 79) public function __construct(array $params, string $head = '=', string $foot = '=', int $max_border_length = 79)
{ {
$this->params = $params; $this->params = $params;
@ -216,16 +218,19 @@ class TablePrinter
* *
* @return int 终端大小 * @return int 终端大小
*/ */
private function fetchTerminalSize(): int public function fetchTerminalSize(): int
{ {
if (STDIN === false) { if (!isset($this->terminal_size)) {
return 79; if (STDIN === false) {
return $this->terminal_size = 79;
}
$size = exec('stty size 2>/dev/null');
if (empty($size)) {
return $this->terminal_size = 79;
}
return $this->terminal_size = (int) explode(' ', trim($size))[1];
} }
$size = exec('stty size 2>/dev/null'); return $this->terminal_size;
if (empty($size)) {
return 79;
}
return (int) explode(' ', trim($size))[1];
} }
/** /**