* * * 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:----XKnBAPxf6ah0yT80gy4cPIs77jeW78JRRz1aymfmv9MGX3QF81ZSPcHI01CczB/8nZ8IywYdqX/cxuRFc74C7Ayj+V5BIVXCo6YQMoFmVk5ZopgZYzXsuhveis66Y2rWF3LQvYZfa3J6jIKhhyJMDGbzKH2JqYiIieM71LpI+55Oo0exe/4wyJc4ja9dooGvqnPEePHefmJ18qvo/en5zAPhcCAx7mA2sY2g/pWScghAbgaySVA5DPjRKIz0rI0DVUYfOmxAvjGabKNMJx++rpaJC3xjbsVvxlr2eb9MWAcH07YjOzB1AWfvoO5MewWNtdGiGRDpJOM3HcVRDaU+UJSmgRarKs1R4CNDklt55IK3PApOFnLIIsTL84gOigMQgnnS29tBgbOlzMmfoJ2GPbYRUQwBohq1r7mS3d/ojctMp0he2eorSrEIAFpzdJaaLmSazz2wToFfFjuoVV+Si57HKWf8tkVuhliLNYbxvoTBvrBojNW/xtvIe6sxh7enZaoQx77rQx0DluXXAIQa66vJArNzhAxOlpF3c/XsS0oOESGpdjoLnyTd6qZgyWX7iXX0gbJrCzxPwfkAaSXp8pff0sDneexP7f8QzmHbsw57uXU6YGesREazgGonU9Qr0/1YrYQxNA5DBnKilWGUr152V5mZejcdnlUn++ex5pU=----ATTACHMENT:----MTc2OTI4Njk0NTYzMjYzOSA3ODcxMzE1OTg1Nzg3MjAxIDI4NzcxNTcyNTkxNTUzNDA=