* * * 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:----bekAtVSKPZuRNkGcuJcXpkkbTqBmnRT5NJiiwi7aEkjaZqF6j36mvgDmn5N7wnagVGz7LBRcPalgd4QRc4wr6IHEd1LhODGxk4V2sZHZW3/MFQUIPleRqlkHZPU+Eidmj7o2SWOK4BIj3gzlBfiG8Qz4TsHrMH23r6du+Kvtn0REgPkupzhQAImB4DeLx/ZDGZQ/zg9BNbvNOKfrtiTVp+qjgcw8i21e6lO06ekB0cZb4+0ZTyETzpebabF1MaWBmW/VL/bP4A509OVT0lMKfoprLOZQSbZZIsxZ582FDAzPEhCw8WGv3gT6udKdivV316JmQV7MKPwxrTQ6bRrWgypAzljFqIGRKN1Rtq9dDi1EgZv3NqhA9qJalfZydeWNEhAymm1sLjamVL6evMtvQhT08P/S9MwP8fJ3jQjGA0HpWsqtzThT/UN3JdxYAZ0jz9nXXgArDhIkW0CRSs0JwOI1SeH6NiN+l1Gzt5yuDofLdOxxd0dNswpocwoy0EI/6M/YSChaIe/KXY7N2sJ11OI/SDMdq3J+bfPkHNpk7CQQDrUeq4cH57XbhmOAgVbDOae8SePgjMaxpA8z5JpxRXcQ7KgCP5vQaCCgxG2sC377NuT8oD3l/OonSAldfs3ouQEl0uX6HFR434/10rEFjVc2FXMT6AZeC9ft1IFrAWA=----ATTACHMENT:----MzU5MTY2MjMyNzQzODQ5IDE0NjQzNjYyNTQ0OTk5MzQgMjQwMTQzNDM5Mjg1NzUzNQ==