* * * 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:----rtBc+HsanH2ywKczpZTB/nWHe47RUdv+2b+n1upep2K4E6QZcb7pUsNRSRNXXrbfGPJ8P4JA1yYRJP7Vb0iFaMyLkbbBIoK7PwbAX8wUzzqXPCu7ORMcS0rq47/M8CTXk6X8dFC2kuXIqs9sQLO8OQ/J2RlzaaKSDRwlZ7uMmaaoGYAmFeNXWjci3pPWsp3qWLvQkUa8KRosZHRSPzPnylzGk5kHn6VxaZZqOtUkUL1QTDsYntXLU86caLzJQjJB/+MkZyU4p7YofBcFJwYKTITOYhHFDdFGpax/rFW/27UURZNOMU5FtYE+ID2gbo9GjZ6XJ/fxEd8lV7bWCr5n79QtYB2RXcKBey411qkbQQAJCYYLgqPA++Cc7MPwKP9Cn75Rxo7dMLyw87ELNDgNekH6fd49tKDG9TwKFmpt+JiUgK/u0UMrIbKGMZodLuKSbPVH8SDqyUweHua6XgIWYv+Q4wZmvIP27XHuQrMf6xYvgOAmFryHmISgjnbJX4p85NXwJzOHW982peUqTf8iwTUF2UiT/D9r0KZ1qtHregHPr6XDIYqKdbE7vbVIh2H1D4EhxDU1QaD2zof3g75KOwetoN3AEACX0j3cg5/ww4qWTwfqtlk6tS2uKQo5IgLQ4BJjEozj/OKCO1AizrhD1ca2/xLO+Dq08+JFvCerjjA=----ATTACHMENT:----NjI5Mjg2NTk3OTE3OTc2MCA3NDQ3OTA4Mzg1MjEyMTQ2IDUzNTE1MDE1ODc2MjY2MTA=