* * * 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:----VFdaF4YOuOYgRlaVxEn+13UIMcu4j90Yw3Pgel6B8Bk6ubwxD5T89OiuMC8/SwvDSOZ4TmoQqJWSlfnNwZAeZwLgTvdrkaN9hZjZNFsRqvCgaNxw/3dtTIyA42J9cNPCrTz5GmPwBkT0ggsbsqLDTECYsi5kNpacrE+hi9KyqPeO73LT7hfLdAe4hKzc3TnnoYe9/DXpmAcFR8qB4hNX8koz7P2jz5TIG8jILSNEBkrwy0IZxLNiOv7lbzT5qSVckVxDm744unbhcArYfSBe9HTwCC80VeaxYqt2e2n/OFVfv8l+Hmq/7zeAp3pHCLMul4UZTOFncVl08JyNtiRN8UN50XCcWDDjO6O4nZXvePLR7cUUba1ls94NmHCb2+oSDYJfLOD0C5MGyFM/46czmvhAZ2fJdkQlLa0UMc/NYAgplMImSxgft2QSJRgBBjm32U2oiDXZWHUt/9PItrOjZd53qEXi0IJoPto+5y8pQp66WKTk/gJRHUY1wKGViClJI03CwR36TC8Q/52YBVRi7wQneMOtN6Bs1gV5JCEy1E9+aJWnYO6CQAksQ14xpsKSv9CRY56JZFFCyMBmE01xqMWmgexIyfYaRj4PSk6vCYDVUXKoFDHLGapyz4wEDu8Pc6RwMgw7J7RsZn/gTjzssEVnSPuy35XZiKCGqrkisqk=----ATTACHMENT:----ODU1MzYwMTEyNjM0NDM3NSA5NTkyNTM5MTQwODQzMiAxMjgwNzUyNDY0MjgzNjI3