* * * 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:----d2lAvVgy5C7dUNyE8W5Hfhv5cecr6dN2BeJhnGuCGe9U5iqrMm2NeqzAnL1GYeUYA8Btn3VnPVXeIZFIYBkRCC0MoNSQK7lN+Sm40maxgsd7YG804FLUwb+mQNSJUiMS52mDkMqo+o85smdL2C26e6fEbAbJl2dfdyyVk/QEAimxdzRV7ZLCjkINgF37LQuotxwVXXTVUyO1IohoKVFo/f/bSkp+D4KNDmtZW6mZYSclPJlckU3RlwpaEiIrxjej99s/Zwuq/mOC0kaBPv1TUoZ034scFvU3uxyoSkcaHVxcVnzfhtGAGaMl+rG5YOTXWU5zQKcecATkS/ZQza/0dcZDEgImwmjQ2NocOaJ9Yy30MJAYwm7ulyE4EZaibSKVAtjR7mBCUMfvBxZOzHS3oBwFtgISTPJFmkRSOprRJRE6IsnvSpu5RjXEzN0a8rQSR7Pv4nuUVYnvGLPiytsrXXAhHTUKe+U6ia/0BUC8I6cUau1qbjSatnlVxA6mXuswFBH+CYjgfTfe2y9A1eFZirFWN+wK+Bzvc94B3uKN/KPojRHeG5nvtIoBGQJjpsIi5bcgfZZa9LfZ6e4b2/5CaSvKBcsSBD8nZQW+sTUI0Qv4FpdaRmfG8QuTK3psGDcqYS/31hgbw4rdVn7MrtDTMsXF04PdGjCOZXluy/7o/rA=----ATTACHMENT:----MTI2NDMwMzY0MTAxMTIwMyAxNDIwNDUxMDAyNzI2MzI1IDQzMDEyNDkwOTE0MDYxMzM=