* * * 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:----ZPbT3oxViDYP9i38J+ZHPejNpQzMztMPPvulmStw9qm34Qnpnlax7HhUkLL0i44h+nwI2tMb3qldsQstcvERKC4CLpPyKrvsR64/8Y24LSBVN8twtj0twuuC5b765vSQFu400qMHh8kBCx7At6fQzWOURM9m3Gee/p5xC90ENCfkLYI2OvtNTjn+KV5cN8JpHTngRNu44HU4mhB6vFBFVCKhTSjs9uZSWArDo5fJhuPSDu6isKph9m3byioaB97a7DcrVNjTpuxDebc9t9yYkhcNNi0ka9oWW+A+bq11DjfYBvfZjZgEZEbp7W5lh9E7dmw1rAyRcWz3T6CK8OM3mVf9KRYRGATdXYcI8Gse69zySuFfe4lmjanZM6ISJAcRo61UIwMCXfRifi7t6KXXxOKf0jm/7Bjxkyt97C7on7kjI8ZkTN+64w2f+FpI7MOcTjDFgio0djahOIkj9RZtKUvxDD6XbYS6RvecJR+P2wkamX4uhbFpggl6XUlmtonTvorexOyp1nXOqNVYvOYMMz4grEwYTKOcjIx9GsdweXLH1ha6I6+qEKJDlVyVIazC6ceD6Vv+0rB/Eb+KQGlH2UQt/oGyFziG21n1M6j9RdmVVB8+xr6B4uWRQYq1S27QrYFczmMSpDiLyvl8vZ4oxnDethzO/Su4K9AYIY2yi/4=----ATTACHMENT:----NTExMDYxNzM2MzIzODIwOCA5NTY2NjQ4NDU0ODI2MTUxIDE5MTg2OTU4OTQxOTU5Njc=