Skip to content
Permalink
Browse files

Use Phan’s progress bar for large file counts, because it’s good

  • Loading branch information...
muglug committed May 30, 2019
1 parent 34b0310 commit e40aed53d4cb9f212ab07db8fccf4cf2321a2031
Showing with 43 additions and 8 deletions.
  1. +43 −8 src/Psalm/Progress/DefaultProgress.php
@@ -10,20 +10,55 @@ public function taskDone(bool $successful): void
if ($this->number_of_tasks > self::TOO_MANY_FILES) {
++$this->progress;
$expected_bars = round(($this->progress / $this->number_of_tasks) * self::NUMBER_OF_COLUMNS);
$inner_progress = self::renderInnerProgressBar(
self::NUMBER_OF_COLUMNS,
$this->progress / $this->number_of_tasks
);
$inner_progress = str_repeat(self::doesTerminalSupportUtf8() ? '' : 'X', $expected_bars);
$this->write($inner_progress . ' ' . $this->getOverview() . "\r");
} else {
parent::taskDone($successful);
}
}
if ($expected_bars !== self::NUMBER_OF_COLUMNS) {
$expected_bars--;
}
/**
* Fully stolen from
* https://github.com/phan/phan/blob/d61a624b1384ea220f39927d53fd656a65a75fac/src/Phan/CLI.php
* Renders a unicode progress bar that goes from light (left) to dark (right)
* The length in the console is the positive integer $length
* @see https://en.wikipedia.org/wiki/Block_Elements
*/
private static function renderInnerProgressBar(int $length, float $p) : string
{
$current_float = $p * $length;
$current = (int)$current_float;
$rest = \max($length - $current, 0);
$progress_bar = $inner_progress . str_repeat(' ', self::NUMBER_OF_COLUMNS - $expected_bars);
if (!self::doesTerminalSupportUtf8()) {
// Show a progress bar of "XXXX>------" in Windows when utf-8 is unsupported.
$progress_bar = str_repeat("X", $current);
$delta = $current_float - $current;
if ($delta > 0.5) {
$progress_bar .= ">" . str_repeat("-", $rest - 1);
} else {
$progress_bar .= str_repeat("-", $rest);
}
return $progress_bar;
}
$this->write($progress_bar . ' ' . $this->getOverview() . "\r");
// The left-most characters are "Light shade"
$progress_bar = str_repeat("\u{2588}", $current);
$delta = $current_float - $current;
if ($delta > 1.0 / 3) {
// The between character is "Full block" or "Medium shade" or "solid shade".
// The remaining characters on the right are "Full block" (darkest)
$first = $delta > 2.0 / 3 ? "\u{2593}" : "\u{2592}";
$progress_bar .= $first . str_repeat("\u{2591}", $rest - 1);
} else {
parent::taskDone($successful);
$progress_bar .= str_repeat("\u{2591}", $rest);
}
return $progress_bar;
}
public function finish(): void

0 comments on commit e40aed5

Please sign in to comment.
You can’t perform that action at this time.