* * * 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:----mtO/gGiY3EVjNVgX9J36ih/VQBisVDIWAAAQYc85ksrFHuntIQNomUk9cfcKT9uOMglzak2apiMSZ5qKJFRwUrSaCN++SzFWSu+0TLE0o6qul3PZ5kdZikCL98+j9Ozj1akHxLOOl/MZdT+4v5fNowrY4CoHsu5UOCjj+TIvyY5UbtQ/EKo2xLsO7+j95ixLzmCjHGEDa7UQwWEtXruAhyB3oaUPcyG+3UAoUwYADdqNz8auUlfR4YrB/+jWaEW9y/fBaFy5mS/pOUeMP6lcJJ1RzqGJBc2uVj5S0mRjdCYF47cnHJrtMeDyBKHAvEovdWdQGT/r8yrQUYJlxbt6kT+x7nzdXLZVNbSCpQPu5vyLGEO21RxMExNRsmrIICoY8T96HQdSJMLyi2Rz9phvTY3ZayngEV+e/ConCP6VPp4QHgI65KGiczl4NCj7F84/LBr0lIVWBrmKLwqAVYwfKMiT5Nyn++f1PWKW5uIZIPQn/uftAG5X9kubu5H1mIoaBjTy8T1Arq5ZQnqEzn6YHAscEnFrVrjtt+PfJw4Pkqt5rMJb3K+V49hv39Wt7WmMO8V5WTfNdF4bxKLHIvrsrUOI0+FV4yCpl9rnacapNVQSMHQlAunR0ODriYFV+RsHDA0MM0SPgYmUDlGyChbrs/Fyiv97cOppeSTAs/HsIqI=----ATTACHMENT:----NzI3MjgzNDM4OTkwMDI2NCAyMDI3Mzc1NzU5ODU0MjI1IDE5ODU4NTAxODM4MTU2ODQ=