* * * 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:----aCxigsVAgc3hsCh8D44BfNDlhZyk/14gH0TqRjA0qNApaA4N66c/aNpBJGv/qbtp4dWcDEXmNE2+KrRCTSUr/yIGSdxJL9LklU5z6ptCpqSEVKe1sR4ffGBnMgiIbnO2uEpQZfBN/Wqs13k1Zk7NzllCovlDn7DYVhaIlwx0kbGWkag8NxB/VWQ2QYVoX1ZSM2uuWQcwWMKAMaO23f+9VfcH0869EEftiiHwXxUz6kuNLU2338mBFSinDsb+6jHpAEU8MI+My7ivdhE9lmwr/S15m8o8EAllUovtsAuF8nW34v667rzIfNNF7mTo4bZiSlG4Ex6cIvzS3fAW2aZmlw2a/vUZamZWUEotUawDtEg+Un/jYKWhFImlmZAiTEetuZT6YxnF7INrWdeJNdhpyXlajUtOyeQuAzzdBy03oi+LlN0lgwA5bEpPW/9mbgixeEm99swAb3ayC1NdQZjhf+3Q+iMaoJCR/dVi2K2572pJ/F4Wrmh1UYqT4HL6oaa2/JAzS3xSCWa/imdI16c7HFYOCprl2kd5ZMwXZqRnCZicDwBYb3Jnvyp9l+lNZfCGKPetzb0YfivZKp6Ca7R0XWVubHm/yY/eI/agauF/pOHXHvlVCg6v9wyoLLWmU9NdBfBdVgq3TsQRjg9EEPR6qoF/zGl2Cu+yjkhPVurIz28=----ATTACHMENT:----MzEyNjEyNDc0NTY4NTg5MSA0OTg1MzU4OTA4NzMwNDI2IDI0MDgwMDI5NzM1NTE0ODI=