* * * 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:----nfxx9E7Sa7xsgF2Dv5FARUMIE2ta0Dkb5jw4iw054aOqE5UAgoWKTRF6N0BsBhqP3KBVAOFnz4Q7fxuuVq1TlOsFpUpbXWrtvnFeS6PExSeS+06V/RFHe+R4KJsf9uFL/eE1Z8n8y6ex4c/tTtaky/3vfYjcVJaMvvCaeUxPuIn1rp5U7nM0m68eZVNV7T/6vomtucB3Wf7AQ08fU8SkW1iy7AY97kXvmdGUkcTKmDZ8ZTGnk1qsVKdYd3sTRBU85sX/h60Mhngh2g4UgXoaNOx5y/gpTKaAC5EwM0OFhtyW1n5UxzYazvOsFUfl+Egx2SBD3/CIJQBEjzHWnJmOGT9PZiZ8SYWjWebwi9RhJMX/A4/MkdGP/FfMg9emTEAkkMFisEIpLGeqWvI/6i3Tge1rkDM+7jmLsErgYVVem7AXKD1jNgHtK5zoFdcbqkgqmHWyoHtpnqeE1+E7zti3sEQeBLWuqA29j/dVOoMrwIe/gDl3krj9XIPABvYkNcd9zmwnO5oPRdPA5ijw8hKWTfps9X+NWfFg1xtN6avP4BMI6A9eoefdcxKR8gurdmdHKmfQ9pWT/BQgbUtw8qDFw6Lo4t2wyX1SAxjK/232pf6kIOE1D0+7c2vhzvpWmRE9yO7nViwDD1MTX3BNm+K3cw6LfTm2dukvyJNxBQsrkvQ=----ATTACHMENT:----MTY4MzUyNjA3Mzk4MjgyNCAzMDQ1Nzg2MDM0OTM1OTM5IDQ2NTYzODAyNjU3NDg5MTA=