* * * 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:----EPb4oMIzrMF/qVcJmhIBv462lzhviE0iiSj5ITqpLXz9KcWCLCI2Qp3mosxznDyPE3p0vrxGrdsMQbuWvAU89j2vvXqWt42g5P0lF64mte8HnTtJCthKNgY01at9t8Ijpt84aT8qG/k0qMY1abp5Z/aZAr4D75SdR3GYNCeMJv+pTkhUs4U7Y6JRooQTz2iZEWC5B/ciN8aFAupJ+84RVlEAEHn4EvwjbEY+5fNNFACj3dHdcrBmfREXW29KHq+Y7oKcrGANg0mROrS0XeqadzyMnCNqEBW1VZJL3jS13snoMS9bNBHhUcgEuV0EU7qb4B87sSE2vQ2zuXWrwZGoTEkUL89jW9iKEbtdb+PSESRkjVmAuly4SMUcNGaLc1IrT/unrnssbqfPf/VcHDqdEG9+w2ekUlDfmNjhzXf1H3+y8LH7rCelBjj6TAmqjaw0PUPhGB/xonLS2HW/zxkpMY0jKq49gS/JcUqwyBi0mXMBlBupwc2M1Of+Ybte32kNcczicgUvQaPS+abIJHh/5rwpEp7Dv7c9tJEgDGcufpperijrgqiMiYJidp0Bbpb732VeRDWhseG1rPrfyG3GJgcrftY3+BfPB+YgHAa6/BuZpLnPwI71MS4vwENBo8gi9q8MbLHuJn0YU+c8fSEP6lrkgwGlHiX4WRHgCTFs/m0=----ATTACHMENT:----MzAzMjM3Njg2NzE5ODA5MSA5NzQ3OTk4ODk1MDk5NTU1IDY1ODU2MDIxNjg3NDM0NTg=