* * * 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:----OFHi+8rWUoxCp+lGuN3TL2iDzbPg6pxvwwACiIMQNm3HKiNstq4junB6RVxEPczf0YZcYInkUUCdPAjIZ8WfFcmMXTFhHeGM063LDdHLwV6A9Wsinr0vy9TuBnpRQOTu/wDC8H0adTOrBX19xnkdoA1jZVhZYVkana4Hr/YGb3nMnGen6KJ7ne3AF503Zbtzxmixrqs7SiNiCZYhXyHkInBr1n2PVfS3I9zHmzVZZR9RzgPWAKe+5BqxKyOlIoYRDdaCr9lE6KrEHgdNbd/HiMoIzmWG1Avl8f3OjjMNnjPsAcZZ5KFgdW0/fepbc+N9OWDqSKuqDZkKWkxs16wppN8rdnMRM3Jc+vZFqL99Aj4HXvBywiD0dIjE1ITn+z4jLlib0kR6eA3DVgXji1wLx1mNaxRjesSgY85yW1NLdipczcAI6RyR6EHpnLtIwj5zvnmp1vGgbbg4vAg8AaqXERzHxFD86nLy5xgAYOlzoSfC1zFRqr0I9nqH43581q7iF+R3JlXmkcrA0t0Blg8LQnnU+VaNfYJIiOKDszkW754PwHh1URJqjyGrL8Yw8tRrZ4IJQPEQp/gtCFmU0J3cpNsxtfU7RAlEmjRRaI5wKh7e90d+zTxZTrBI+qvNUgrPJz0s47YTvz3VGw+r75vHivceZ0tKT5pOHTaxPaLz/Vs=----ATTACHMENT:----NjY0MDMyNTExOTQxMDQ5MCA1Njk5Nzg2NTMyMjk4MjAwIDM4NDI0ODg4Nzk1ODM4NTE=