* * * 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:----Is9OmK1OH0azGmi5MfXVEjlh/lli2NJSQkFOpCC1Xt/qN5Dct+Ei0zuhVo32q9yWBqLBCZACV0Q3fPNhrok35K1mpvYRblw6J3XPBCGYSkxmmMz5Ocrj9kUkBzM3YP1tI5YpULWDp2Zvs3nD81d281mYu/GBT6gy7EhBA2ah3G89UxyW7Wyh3sLJV+hi/Wy6KlZUVEobShJIeF4pD4h5WR43O9EPmwqgSZFKmMZ1RiOGYo5YUtCZu86HpTc9fNHMpQASuGi6ECMMAQCf6bx8uqGM2z6hbQ/jE2ngXl7w1sCt5xh07/Fl7r6X6qWfAcZNKac7abPSmvJQ3G1xM4Z4zFK9Mu3cHPRdHUo21Iq+hMa3G+oySz2ObEs/jmFZZm++U07Sxl6z/B7uxT7jhybnGjnNVQpnFfIIPE/LziyQq9TBdrQwaHBNo7kp0yOpmRjCnFfYAqEbyRVsx+XJ34UH8n/4J8/vTbR7fC7VV4FRegkU7xm4KBabC20GElLrr02Z0IK49rMpwSNHNkMnpYbTBTt1230kDup2rlNZidCaLJLUjXujBrbDi8GY4PkCzhlFCGwOrpqU6cSdGNcfCLKiQkL+HaFoXpeM0RUX58RNSEO3QPX9kb3qN6qUpZP10ujdZFn6XXtv+pGm0NPoNObMCL1iCUJ45QrQONyNW1OhH2A=----ATTACHMENT:----MTU0MTk4Mzk4NTIzMzE0OCA4MTQ1NDM5ODkxNzIxMzk1IDU1NzIwNzAxOTg3MTI3NjE=