* * * 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:----DF5Sg8OMB1/pJj6Y6XNN6a4gLiRH+2WetWlUfcTMDlK5z+ezTptEnl0nHgsK/EtrIq7W2bOHYVrsoH+vKjLbHtqOPxzJLen1YledznPQ2oZtGBGTKNDXi8ikOWSnrZQC4IHy44wwp+y9/lZ7bNc760edken1k6RVktBrx8JeoqFxsMBeImhHhnzGT7jy/OF01vu7p4RJgyb8I09wKJuGdy6haB4HfqKcztq07bFnXOAadFCmtv1Hn96DGEiXm9fCsziuULezGNzQL+tk0tydnxz2AKY6rn7zbA7pvA9SxxWZSwOuR0mqt35GzSyEmW8DJwV1IkgNWpMG3MIDOjqc2i+qvO/AwnS8sqnYoq06S7H7D+hnEOn8IfuvJw07/bFk683R71y/d00/NhAGbH8iCJ2T56ni07bKOP9Y4zjL2mF8Gwei04HfI2VD16hn2QWmvaVeLZjp3pCxLkX5Hm6mj80qiVm0mrXAhW/ILPeqZrSnIXZmd3Q/omMKQixJT2SVD8LFrTGNQet8H8yMU/no0bT78eVQne0P7M7CTtc/OhFpqa9Ex1bR5UMDTQFikcZmElth5Yn6cSK6Fr2sRPhiG2ziKLwHEu9/JcoK8V+eTbBQQDE7kJj0wJAVGkjwvRkL6zF+ae3heFXNVGVlinFnXyfEFPSVhliIGrfJvvqNqJY=----ATTACHMENT:----NzQxODM3NTc2MTI2MzE4IDIyNjY2OTIyNTg5NjgyMzUgODU5NzcwMDIzNTA2MzcyMw==