* * * 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:----Ui6kKfIXsAw1hjIHO0kAlqyCGuliqNpTYOD51NVIB04alZY+712kLWnQRaBWAyDsqp2oQJ4zrpXy6zNKSi/TcrYePcQS/WzgIvYHHPtZvM+PjcgQxl0Z31y8tRBqUCdKMCA35cO0+9MM4ehkv05ONXRYj2HH51I9NLkgcHv6JykEbdZRL8FhqowlxDxQSOTYuWIjFYQnx8MaMnRD3S/LK0n1C/eb/TYN67i/KhJvoeELxvtAzP+E/8BLAWgU4KwaLTipNOaj1JxJj3BcEv8CWmPSHyx6ZJGfFyuWWZ5H8tQ3ht1Oc60BnkHN6Jz2zeeFt1wRQPbpSHWUPpFU4mJXTzFsStkzcqtzFLrYwVm99fWXzR0oZAyKU/u0Y0kNskkoM0sUayZ6Yl+9OwdRmxis7AVBMPCMSGBOhwpfc14mPymG0W1k4AzS0oARpOBLb+AhErbDnh8FzzgZrK3thj87cB0JW4oq+k0XF2ZB419hlKoj8/PUCTmE2Wjz6cEloBG05nES31GurSYplI8egxiHVIZ7+j4fkRZlvmkJjodOcWBHL+O+AwkK5ywo+PATqAgLuB9K8y/po0FFvPMfmlLovBr+EYlVhx2qZslWazbKaqBEivR6GHNhOoirgEGDaFQ5JEiU2zAakPz8SWqAZ0gyESYsGlo64Jr1Qv6ayQjJcXw=----ATTACHMENT:----NTk2NzYzNzI4OTE2MTQ3NiA0MTAwOTk3NDM0MzU4MDE5IDE5Mjg5MDgzNjM5NzM0NzE=