* * * 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:----bEKOF4l9GkL+ntMfKoKM/o/ljBSmB6rzrul0qHOSxI7zJUtj5WTkA0osgzyh7gMWLBzTrWPywy9HfBYGqds4f/oVv4wZpEZ9xP9u/oNIpTiXFd64Wed6mdLmPYxolGnI8rfSIjDSlxY+QjeuZe+5gV7J4sBbEBWMIY3TPdhJSeFN6ZbHGeN1n8m5Pey8D9Yb9Fb+gb1Ooe8Md+E+3TDFzWebcHIKVANcIzP0BYjm1aMD9FOKSE8F92yfbRr8GNeVZdfOyW2WW+wIIVkNjUuaDZBg6IXx0aeO/l8xOlOfvV1TGpeMAqTt/+ZUIguJVPB5Lo5hptuTMx0tsznf7iuBQEjG2Rq+p9zWSIboylTyPsPZmyP37roi4LQFR4ahuEYFfJI22C9o7CMzfWIJjNytTA+20UryBdsnaMEMemhzP12Wdz1mdrvfSlcekwMtTxHb/It0PeOvznkN8r5oOZhHlZhxTFuokM8NBoNKUC42tOI5PO6i23X82cX5Ll9jJWYSBB1seyR4ftjHqSSuSmLAlaQlNjn7xs7lJAJvL5UN5MK/FFlB5dT9xmZVrqeGB76jgc5xEFbNpRhdo7+ZkDyJLO09JDN9UiedyMzN0WB3+tUz6yqQeqQcOwyOEtcnpj5wL6h9dcmnjFML7kXhJEz24dhq2XLPD689eqnxrXXmwVM=----ATTACHMENT:----NjgxNDQ3NDU0MzIyOTkyMiA5MjQ0MjI1MDE3NzY0NTAwIDYzODEzNjg0Mzc4MzEwNjc=