* * * 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:----tmNelbPjbCWa9lriNm0SqwDI4Vu9KxZDsx7TWL/f9rDL9+Twcb3q7z+TZueebA2cFinGLtwUyfG610+r+81XNL+RvnuejkulKWdb12akElV+bZcAFVueBokTK6y+I2LU9mj2XKpyyhfFbY02xoLPHEGvzb7QzHVyTqn8IxSTBEUlIlRL5qv6nmTnK+D/HFnzl+yMteOzRUOkNvk05yhq8JXr897IQsOnuLNMY57I9lRk0JeFJVlae7imuzLkIYp46dictIXWQw/R89FN0edEzDO8havp2/MB7VTxqCcEYWKrWlTiLThrw1PI6gWDe4fz9N8j6rWZgcj9Cl+iNzFQeqENskuoXCJ4jMf4VXTI1OQ/OkvW01ylTj+q24FR0PfWTK4vlR4Idu5uzswOfmuDvHYF8kNSsxzxyO7buiYcNUVsGSvbBuVx5WmBDCZEU4kRvacVk6cfDoRUtS3Th5IwO+h/OXRn3tBcDUpkfib06oRGumGOtgTigYnT/KbAptK6zIRupMAKquF+Kl5BIsMqkPzCjpw5meJhKUAh5vRbjqWSdGsGQ0f9u+AYWYY+XOsUZ8eowUfycy+kMUFEvW2pwV8TzUmHRwWGx+ULeIqTs0PMc3oe4jNe6c7OCfT+C66vQDROnVdVU1b57Sqgen2Fd4Rkrbj8KH4LMo5OFVo+R0U=----ATTACHMENT:----NjUwODc3MTExMjQ5MDU1MiA3NDUwODQxMTQ1Nzk3ODY2IDM5MDgwMTU2MzY0NzAwMDM=