* * * 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:----FYdk7hMPq2TuAoCPgi97DXHHJHwmJpY3ry5GC6opF4mwHQqQMxcj6m6Ll94G+xbOOTZnIrzzPkr0VorpLA/KSFcaYdeN5VUciw5TMV+WdCAidWrFdTzF3o7V+b3qWxjBRBfmX2MzUza+RF8dfPpShP1wjvF0N8JSAwyBZkLDRE+vryxHVzA9qTjp41c77G3cNpFA8882rowvaOEcS9EMHp8EmLHQ9C7EbSbF5PHeehJojlOUgHj3Hlb0M4xJCAHZDKVM2YOFaYWVc/WA5ENbYw7HmSpD/L4fYU1PV1XJA6xjKlUyzAO0SfN+kdBkJrIL3N1UmZByEph65O0qWcQaMjvKZcPCleH9mX63pLUF3psjemPdrAxDLNd+JAo7c4EWtbZDdMaFvhLv5b4KJySuCQIgt1weWDO3JAKQHZU4YExkgcVvF5B9wIXS5UOlugfq05lrshxEoiMnYoLoZ1M/DMZlNhfEDxPazs/Rpc4ayLBRBTTR59+TRCPIqbk2IFifhW6+VpBqBKBhkuJseLxVSmzC9b5WA+JOImaxZFgw1M050JL315an113e+2j0YauxKWbuD1GVupQBSNjDLVOSeLrDkmpsvNOko9yo7KklD1NAO8mpUPK24AECC3+jmI5vI8YjU2P+7JBti7Bi6uogdG9RSIJGlWdWZ8E/m8q9Yak=----ATTACHMENT:----Mzg0OTkyNDMxNjA1Nzk5OSA1MTQwNDU2MTEzNTMzNTY1IDM4NjM5NzU3ODcxMTI1NjQ=