public static function getConsoleWidth() { if (!static::$consoleWidth) { static::$consoleWidth = (int) `tput cols`; static::$consoleWidth > 30 or static::$consoleWidth = 80; } return static::$consoleWidth; }
/** * Checks if console window has been resized and recalculates screens coords */ protected static function updateSizes() { $count = count(static::$screens); $width = static::_getCols(); $height = static::_getRows(); if ($width != static::$consoleWidth || $height != static::$consoleHeight || $count != static::$screensCount) { static::$consoleWidth = $width; static::$consoleHeight = $height; static::$screensCount = $count; static::clearAll(); $cols = 1; $rows = 1; if ($count > 1) { // I assume that comfortable width and height of screen is 60x40. if (floor($height / 40) >= $count || floor($width / 60) < 2) { $rows = $count; } else { // If we can't reach comfortable zone, then let's split as convenient as possible $cols = min($count, floor($width / 60)); $rows = ceil($count / $cols); $newCols = null; // Tricky point: we want to cover surface as effective as possible // If there is a way to smash screens in less rows - let's to it! for ($i = $cols; $i > 0; $i--) { if (ceil($count / $i) > $rows) { $newCols = $i + 1; break; } } if (!$newCols) { $newCols = $i; } $cols = $newCols; $rows = ceil($count / $cols); } } $colWidth = floor((static::$consoleWidth - ($cols - 1) - 2) / $cols); $rowHeight = floor((static::$consoleHeight - ($rows - 1) - 2) / $rows); $col = 1; $row = 1; /** * Recalc screens coords * @var $screen $this */ foreach (static::$screens as $screen) { $screen->setCoords(($col - 1) * ($colWidth + 1) + 1, ($row - 1) * ($rowHeight + 1) + 1, $colWidth, $rowHeight); $col++; if ($col > $cols) { $col = 1; $row++; } } } }