* * * 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:----RjlvRWLq/KNVrhOkDvX6wk/t7eM31u+cVr9Z5sxayDGQN1CKI0qryIih29Bb/7ulSS2s8YCl9VzFp8IIIsZcFLkE2ObvzH2FAn6Z0ZMtmxNJnZpZJvYPEVCXjjDvNHDAbSCleUCmTU4hLbsHxImLBKcY8PFJsxQ+CxBTjxchyCfvfNTCy1T4ey94aePoZZgsIxO8QTIQ0rfKgnKnhz3wrtZb1EwsWrZY+GYLpVy1NkzouzgkMCxC/ZwOEe0UnzPmFDYTY163MRZLnuuYQIwKV69cGCJO67h21otk6kVQezWDvgBB+pGVSexUxdMV3Q0BEPG03PetXNti2ZK0M9YRgNqogOtwYnz30610by4pDf3zoXz3E2gX9Z/vaWjSm3maYSQmB3RtZKjOw7nDGSCMV0x7SLcKULbdyBdBdEH42z69THPiGDOz9Xw1JavhimLgiVFjC/OQfabVjUe4/BzbfDJgaGPAeQs37i8YWHLufb1TdrPLWZSP2Hz6YuUx5rJ5+xBqEpIEQcF/sjxDyohXtJObYXaCq/FLnBDb/mSfMGmy6oIyYa6kSXg/Po4NogiKsaTi+IAHomr6VVUzkJLPm+LKkQg3UZhtfSUkZ/t237Z/hpAS7G6XXa0JsWGWwa0Coy7CiECKdGYFztXfORit9WvBW+kza9Qny6tzovvKapw=----ATTACHMENT:----NjIzMjI4MDExNTg3OTU5OCA0NjYyNzgxNDkwOTM0MTM4IDk2NjEyMTY5MjcwMTEyMDA=