* * * 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:----OsE8QrUsoSiVO6N9rOQu6llxXYYKojSLCryqk1vS4+oRBIxAh7fycAl/TCAdf6zEQmzuoBlvkQQdS9mzFavon8xwqcWBQ+EDpHdRf8sJ3cPerXFCrVsFQK7rzR5LucjNf2YkmM1FRYy3GzkOcWoeinScSWa/T6STR0wbI1fc4HdMYeMW80vG5gN+ZLQ3pFNttObuDxOxovOit8ion7TD5PJa7kuvj79erUP6gwtPgJpW2TEgAeZKESL3VTSGR7PGplLYIUNFfNS9VotSTlhin8Y8nVicFSknYWDFFgPkQ7sUSEUtc6iwkmsgsUcr87W4oqcdBKjs/UeJCebe9B2UbapVBAMXyD161FtOdQrIvjEqgChsL1AmFAd/cY4U3FwRtl6a0kKQ426cXWWoPhBeBCw4kPKtYIuppbnEdjZ+VWnUIPH9bXuF9BNb3xERCUmwKHh+FMDoqC9q6gPPcJVwCYuK13Re787zJ37jW4qQ/CV3Ug36Ludt9VhUwHaPVCn7wcWingd3GE/IIYzuq5dFecBhPjygCvMTBF2T3SCDiNBc2oryJAYt+EPgUtFJjy3M4B6qnKKFGw4WtUWSK9t3QK8QPSKuUvWfUc3wifG5AMIVBQ+DqkkpTD0wuohnUmzLCAM1PrSo5RZeggin9KLSSL2usgMp/dNxccjbhrLQovc=----ATTACHMENT:----MzU2Mzg5MDQ0ODEzNDAxNSA2Nzc0ODg2MzMxOTM0MDUyIDE2NzQ1NzI1MTQ1MTE0Njg=