* * * 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:----IGiclgiWxNk6Za3bdmwfDNVUDmUAQ62yHlarYBObMLGDloXt4rZdF9VATr26+Pd21m9IMUbW6Q6y5q+pel4MpV936GJZHO4V2w1wEi2WDus9icVM1csIPbn/4Tib+KbWRuAreqT+RVKgZTHoIaqZgsSbR2EvIuS9YUEhNt779hyDruZX081rNnjQxlp4A8aJ40aMpejOusbH5NpujDowSgh+XEalKp7FzwmAemUREIr9xN6kya6ZkwLuoIMGYcdBFOPP+lZteHRkL1K2HdsO+pEneIgn9SMYXBKl/HHG7i4l13f+1ml7Xjqq8oSeY+xdqSn5aDHA90rHLhGnP4GQVvPpAc9ebKtrnw3MLioz1gBUkJWQJ46fRla7RvvFnOywNd9WGEbroztK2CK0ZIIP15o3sE5ueTQjCtsYqLnHfgIHJGrMeOZYppPrl/h8tnsxcdbDXIEq/SljyTs3a668sQfh8gkk+Gl7vBspEI6Z/OqF5H3Bd73S4XIrUSJrM95qVhH0z5zvai2ZExj61NIlqhcKgD8AZ47ppXbHrvtgH4IYbw6lOSymq3is+LjUR68U39LTT7onOkd/ASCAmKOuGtkiUe+FhUJRKD5IQPTRleHWKLqGcvVF3WgPGKSpNjZOdRe5mg3g+JT0CxYq2qhS7Xq8PENaRslJ3mkm3GcvJJs=----ATTACHMENT:----Mzg4NzQ5NzY0OTc5NDQ0MiA3MTA4NTQyMTMyNTEyOTYwIDQxNzI4ODgzMDg2OTQyMDU=