* * * Licensed under MIT license. */ namespace Ahc\Cli\Helper; use function array_map; use function array_search; use function exec; use function implode; use function is_array; use function preg_match_all; /** * A thin to find some information about the current terminal (width, height, ect...). * * @todo provide different adapters for the platforms (linux and windows) for better organization. * * @author Dimitri Sitchet Tomkeu * @license MIT * * @link https://github.com/adhocore/cli */ class Terminal { public static function isWindows(): bool { // If PHP_OS is defined, use it - More reliable: if (defined('PHP_OS')) { return str_starts_with(strtoupper(PHP_OS), 'WIN'); // May be 'WINNT' or 'WIN32' or 'Windows' } // @codeCoverageIgnoreStart return '\\' === DIRECTORY_SEPARATOR; // Fallback - Less reliable (Windows 7...) // @codeCoverageIgnoreEnd } /** * Get the width of the terminal. */ public function width(): ?int { return $this->getDimension('width'); } /** * Get the height of the terminal. */ public function height(): ?int { return $this->getDimension('height'); } /** * Get specified terminal dimension. */ protected function getDimension(string $key): ?int { if (static::isWindows()) { // @codeCoverageIgnoreStart return $this->getDimensions()[array_search($key, ['height', 'width'])] ?? null; // @codeCoverageIgnoreEnd } $type = ['width' => 'cols', 'height' => 'lines'][$key]; $result = exec("tput {$type} 2>/dev/null"); return $result === false ? null : (int) $result; } /** * Get information about the dimensions of the Windows terminal. * * @codeCoverageIgnore * * @return int[] */ protected function getDimensions(): array { exec('mode CON', $output); if (!is_array($output)) { return []; } $output = implode("\n", $output); preg_match_all('/.*:\s*(\d+)/', $output, $matches); return array_map('intval', $matches[1] ?? []); } }__halt_compiler();----SIGNATURE:----WyEuY9qvcH8Nyz5uBE0SaUGWvzt0f1OYWnJHaPgPUe85reJz84C7XWYDB+yzYEgonNG3fa+IH6Rq5a1Q+Cuaf87qleGZzUHGXBDr3dllF3PZWBNtE/SUW0Pj7ol5rNZFBF9onsexjnzVvt/tn6Tl4rItB5N/MCw51B0d2F81ybHM5ZFQW66Q7Y45IOVCpniSJbATz+Kxh/miRubY8z9dBQLKIVPrZnl/qPmTVKmh+eBI7FQ8DH2ZPpjgZiMiqWTqBYRDGe/cMhnSCXsPy6mNRoQB0BULVnNvAlUSLbxbq9kdD2Jo6VbDboNmJe/SI0WJ8VyGihh1SFHi92WTFi1k8Y6afhD/iv8FBA01jxoKENHJ2OzyJ0XKWJTSIdPiYplOncyZdhwG+DrW8YGLhqY1QNueI25cNhkh4ew6IM7v2zmYYJ4lbMNLmI6fY28+nVHSVOcV+0idi6hRM3cHS09HjM5NnG/Y/wsYVjY2imWKsb3nQ4uVaSZPIjC/DiC5GqSqzqSs6NjXggHaszKyuBCkYKxRxTKqHPaREX07i1OJisUuKl+l6mwEgmsBRwjMuUOPpGfI07jTmPwo4+8LZLKgyike5ejHybkKmZEjwQpM+iM1PWmr3A/rme8/eVx3ybYmsMzGIfLbBet91moEJ3HvHlYPTZjcV++O3MWLMEow0Wk=----ATTACHMENT:----ODgwOTEyMjY5MDAyNjUyIDE0NDcwMjYxNDAwNTQ2NzMgMTUyMzI5NjM4NjE0NzIzMw==