public static function getScreenSize($refresh = false)
{
static $size;
if ($size !== null && !$refresh) {
return $size;
}
if (static::isRunningOnWindows()) {
$output = [];
exec('mode con', $output);
if (isset($output, $output[1]) && strpos($output[1], 'CON') !== false) {
return $size = [(int) preg_replace('~\\D~', '', $output[4]), (int) preg_replace('~\\D~', '', $output[3])];
}
} else {
// try stty if available
$stty = [];
if (exec('stty -a 2>&1', $stty) && preg_match('/rows\\s+(\\d+);\\s*columns\\s+(\\d+);/mi', implode(' ', $stty), $matches)) {
return $size = [(int) $matches[2], (int) $matches[1]];
}
// fallback to tput, which may not be updated on terminal resize
if (($width = (int) exec('tput cols 2>&1')) > 0 && ($height = (int) exec('tput lines 2>&1')) > 0) {
return $size = [$width, $height];
}
// fallback to ENV variables, which may not be updated on terminal resize
if (($width = (int) getenv('COLUMNS')) > 0 && ($height = (int) getenv('LINES')) > 0) {
return $size = [$width, $height];
}
}
return $size = false;
}