* * * 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:----XIJ6VKa5FeRIA5iiKvvPVPP4o28TD8fKxQLBPN0lD/eBu/tVBS6hEx9lyIgobk0hjJRzCO2U7QM8nVHiG4/n9BBJAFiAyf82FgozMEf/lPDDF8Emzj2z4noit9w8bzsLQWlb8r17XqH24FgzsAC9RXBxDlsbKG2kuJvw251JzLbKy70ucHUtM6efM4gj6XCg9c4aIx1aLIDOz/lBxUCXAAMoZiVkppN9S9gOq0HwZr3MYZ+8ZGP/Ccz+7uFfjnZLKNM+Oc9ACk5WEDQBPt/tHPV6gfGV+8hcxbbnloF2pRG+SRsCnJGGy8KnLYGNX71iKh7oBoenyfpcX5ZvdPHo69RVpUbjYUQ9YpGd5xX8ilM9vFtp+Ltrla+laqkmOyQSpxnerNbhETNusGrj92xFf0WNgW/pV8IyBxf6WclUtjOI52mZjVkwqT5OFPxe2fUqbiJljzoeVQKyvOiBjaEsQz5V+oIQpY/exPYw6986ruqm6rDYC/d6HobE/Cbf1fsHX5Q41LwB30bhELBdTbdbdCB9rLCtB9b2Epq4eIqnS4NBQ/9x+qoc8E/qqEQo/KYVQq65ZCmxH0p1PcQ9X2u5TNzTshMdj22E0SUdWNm3+FIWSXH63DIULneiOZebMhlRkimMhDm7u3QAuDSR0nS0SYhULRa/bIXBiniwgB/TN/w=----ATTACHMENT:----MTMzNTYzOTIyMDE5NTc3NCAxMzIzNzY5MTg5NzgyMjE5IDk0MzAwNzM1MDc3MzE3MTE=