* * * 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:----0SDBKPPDwX+q98Zmuw+CyiN4eYEn7hh1EKABVQf0KCJojxLQa5zpuTWork2EsCME0fXVNEm7A8Ucdu+K7TlF4nrV9GWFKbuvhuD9ze9TPtZ3F3BLo8sKPFSUowa3lPeZQ+7GRgk0YA6yd03x7NmLGAX4S15Tq/v9+7/+/VCVftB4ePHyWVx+czdhrom2MpIv1Kn/+7QSt/+JzL3+LjxDJ6bU0vmd6QsyHsJvin/i3U9XqzgPX53qHfQ3IQOsoiwo2uQQCerMdX2UwRz0cNp0o2XiFW3DtQEHFR8x14gdDl0laWo7FDd6auoLQdW1x7Qz9+tabui0zNSEKx3GD2KEAXHl3pCbeAGkQS+OHsg2BqnOCFOH9PXXPiQ5mbQAXx/cSvBiJA++CfoAhEY4NDP97bDjTB+IZqQR5aH7VjL3Vo0nqgN06QCyXVwXWcNvHHcYIWQIM6v2xV3B4cVeEyfxJhtTmuvyMEi5HkGttfvjmPhTJwCAA0W6EgDiZImXbbJoFl7q/21O/VkBLcBdoUvJ8fhmkDnzLpuASNCpL55Os8sCd5LzcB5bJ5h9nOID/uBpo7LpRjZQuLO4EMmAytsIrFc1+efZjPOKnQIFjPYCS3oxlTfNBXQEDJ8cu3N4+1hvCxGmsQcm6g3i8XjbD4n1S5dI1GxEkTFMAVBKqOBUucs=----ATTACHMENT:----MTk5Mzg3OTkzMjAxMTU1MiAzNTA4MDYxMzQ5MjY2MjI4IDM3NzgwMzE3ODU4MDkxNDM=