* * * 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:----YnXEy7G/4Bh1dB/xyScpKMh8NA/9oWAIMMtQn0a2FoZO3p0aQQqTvQeXVahp6zRby5hnzFuElI7fVAXC3MnRPPyCrMsYCifNciZ9gnqcB4VoBEvmCYokdVZUUdNh8eJxm4ZAakdvd1yW9gaOxUmWRulI0v/CmUXXyJgL2NeGWYQwFG1miji3iHSonRzomCEbCuDsX/S7w2Z6FqdrmdJ8UJChJuOy5eHAvAELHTNkfZToHS3iqgNdr01jx1wGPLueyaB0tEHH9Yg1HBdOwNDQYFKmcIbzVir9QDXpUmDpFeYD1YMsjz+6xqEsPoKTIz1mv8pCFG/HZfq5kuS+Et8Gxc6xhazrRUj7e3catibDvfYSNGxE4IVKIf7C4V8Fp8IIB674qIhNMCPwLP+UwId55fYuMHk/SjyV6OJb7kmIFRwPLOR3u/Aqej+ktq2ARpRCNLuhfXezWbWejrII+rK5hH7eUDjenQ2hxsVwU+2vubhK6GCXic9HvpDRa41lrSfMFaY8c4YTQGZolpTkC8TBJpbp4xBzR9106Xkh1oeBeESoMhHw0jT65NiOCBB2tINupWV+OxYpox8C+lZHcZ1kZtFQ/wqVsse3dh3ImLPktW/QjOxsgltrxgQa6LyFeiOunHoYboMTkL0BgDT3fvN02Utfg4DPwTBAbq8iJRUG2CM=----ATTACHMENT:----Njg4NzYzNjQ3OTI0MDYyOCA5MjkwMzkzMzk0MjcxODUyIDE2NTM5Nzk3NzUyMDQzMzM=