* * * 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:----otGE+h/cMIzNisWKXu0/Mmf6GVPWAYBwlozX8qUzA7Qua/P30EAbQ5M1nJjJEx2RGckyzYdIKMYmH16skz6bwZYOsE8lOQWjJaoXwAjOFoc2xxTgNZKfcV23BBvP0Ag23oomRn3TaXhwETZAqYo/+GDtbvzt6cEZp2T/2lRENTLwjiwH0BsABs7zTRlGQJqt+SY8CS5P2l2SXEtI8J9C8t6ncIOMeZQKkzMH1suhY81cg/MD7Wk6jympaNqdo4Bs9QgvCmdaDu3XCxDiYxuqYXinFFh9HTSdEFPW17Lwb2LCpI6J4K/h+EaI96tdWS8xIwYJ1KHRUhU2PQIBLp5hHJ/QsHex437G3iIT8yH8PIrOGIYD9IyqMOtl5PCf4uanPi9JpqxS9Uz3kBUjrJQeE3S7cNNtejESR9FVglJ14BusJO3GFG3y3l+ketG3h+DJ3667IzSNewd6/FmkAaFJYnwoMS1zGWjxW2Md6+aVg2wlL0VyXbMqHXpKe5SAGuUIVyhZy1/gdZZdIMcC+FH6V7EXElNvKqStMOM7Od0OzhbYCaQcdVod+Wq5UkQ/Yed7a6VQpOup2gKrIJEsThA9nXqrLfXjuiBuxSgRiAV5Xc8tOJWN3vCcmJnJ0dyG0S5ATt+4dJ0reJL3GSahwA8eb+roxPCcCt2/BcOMyidciUM=----ATTACHMENT:----NTYyNzgzNjA2MjMxMjczOCA3ODI4OTM4NTQxOTc3ODc0IDM1NzQ3MDMzNDI0ODAzMTc=