* * * 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:----RR973/d7BIuASSTyFHtcUInnAVyN0xWAo4m1BIL1zLBHyaZf7Cl5aXAOXpduPc4+aE6xrlFDKOnFqCBuyD3npJJ+RlW3K73RQPEgGowH/j5SBZSaQnbqEbYMLz8UCi32pwjboLd3xZghZsu9qZ7N33LUyD9J6K7xUg2038KkqZm192FmTKQfM2WFv/v5+Kpj+f5W/jFT4Y7iYLhJr2Xbh7UXG5KADNQadr3I69NzGLtEA/aPZ40et1YBA2E83eY+voqoDjCEDhspLr0/yOQ8XboC0JrZstLb1gk+buGjcNB0pvBZ0W3hHDLWDD2w0l8pFKG3KBlkyBKHmz6LPHEIUTRwTSOhQ0lrt1nvWssmbWZVChHkzz0MD6Dc/o7+B6wSSlvqWp9r7H45QckWDpYGh8HyR+EvqIXddJ93IohtELM1ZNboDQCWcvK/DFJCshGMqMBuAsXq8WgvWJppH+07TXm7hZxOB0J9Bz80dTYTHHZ5LuML8pC1Vbc9nObBSjMQ+q2gvFdREHg/iNTc6Rl8PiYlP6PJ8S8hNVSXfIPdzUpzus/sA9WxR/07nhhGjTa1YbOGnDNF/K8fytCy5z34jgi2RYZqpAOwZ/EZChio7ANmeCeopzzwYxNKsVn8cueqGhCtvSu5BJLAKHfaE6Nd04xEJB0nAPqdEFjr6LcLVpo=----ATTACHMENT:----NzQwNjAzMDUyNDM2NDg4MyAzNzUzMzg1NzY2ODY3NzggMzc3OTk3NDU2ODEzNDUyNQ==