Skip to content
Permalink
Browse files

Use a horizontal progress bar with more than 1500 files

  • Loading branch information...
muglug committed May 30, 2019
1 parent 9d7cf66 commit dedd4aced059418d2b3f8801cfa4a47c182c6a13
Showing with 111 additions and 60 deletions.
  1. +18 −60 src/Psalm/Progress/DefaultProgress.php
  2. +82 −0 src/Psalm/Progress/LongProgress.php
  3. +11 −0 src/Psalm/Progress/Progress.php
@@ -1,79 +1,37 @@
<?php
namespace Psalm\Progress;
class DefaultProgress extends Progress
class DefaultProgress extends LongProgress
{
public const NUMBER_OF_COLUMNS = 60;
const TOO_MANY_FILES = 1500;
/** @var int|null */
private $number_of_tasks;
/** @var int */
private $progress = 0;
/** @var bool */
private $print_failures = false;
public function __construct(bool $print_failures = true)
{
$this->print_failures = $print_failures;
}
public function startScanningFiles(): void
public function taskDone(bool $successful): void
{
$this->write('Scanning files...' . "\n");
}
if ($this->number_of_tasks > self::TOO_MANY_FILES) {
++$this->progress;
public function startAnalyzingFiles(): void
{
$this->write('Analyzing files...' . "\n\n");
}
$expected_bars = round(($this->progress / $this->number_of_tasks) * self::NUMBER_OF_COLUMNS);
public function start(int $number_of_tasks): void
{
$this->number_of_tasks = $number_of_tasks;
$this->progress = 0;
}
$inner_progress = str_repeat(self::doesTerminalSupportUtf8() ? '' : 'X', $expected_bars);
public function taskDone(bool $successful): void
{
if ($successful || !$this->print_failures) {
$this->write('_');
} else {
$this->write('F');
}
if ($expected_bars !== self::NUMBER_OF_COLUMNS) {
$expected_bars--;
}
++$this->progress;
$progress_bar = $inner_progress . str_repeat(' ', self::NUMBER_OF_COLUMNS - $expected_bars);
if (($this->progress % self::NUMBER_OF_COLUMNS) !== 0) {
return;
$this->write($progress_bar . ' ' . $this->getOverview() . "\r");
} else {
parent::taskDone($successful);
}
$this->printOverview();
$this->write(PHP_EOL);
}
public function finish(): void
{
$this->write(PHP_EOL);
}
private function printOverview(): void
{
if ($this->number_of_tasks === null) {
throw new \LogicException('Progress::start() should be called before Progress::startDone()');
if ($this->number_of_tasks > self::TOO_MANY_FILES) {
$this->write(str_repeat(' ', self::NUMBER_OF_COLUMNS + strlen($this->getOverview()) + 1) . "\r");
} else {
parent::finish();
}
$leadingSpaces = 1 + strlen((string) $this->number_of_tasks) - strlen((string) $this->progress);
$percentage = round($this->progress / $this->number_of_tasks * 100);
$message = sprintf(
'%s%s / %s (%s%%)',
str_repeat(' ', $leadingSpaces),
$this->progress,
$this->number_of_tasks,
$percentage
);
$this->write($message);
}
}
@@ -0,0 +1,82 @@
<?php
namespace Psalm\Progress;
class LongProgress extends Progress
{
public const NUMBER_OF_COLUMNS = 60;
/** @var int|null */
protected $number_of_tasks;
/** @var int */
protected $progress = 0;
/** @var bool */
protected $print_failures = false;
public function __construct(bool $print_failures = true)
{
$this->print_failures = $print_failures;
}
public function startScanningFiles(): void
{
$this->write('Scanning files...' . "\n");
}
public function startAnalyzingFiles(): void
{
$this->write('Analyzing files...' . "\n\n");
}
public function start(int $number_of_tasks): void
{
$this->number_of_tasks = $number_of_tasks;
$this->progress = 0;
}
public function taskDone(bool $successful): void
{
if ($successful || !$this->print_failures) {
$this->write(self::doesTerminalSupportUtf8() ? '' : '_');
} else {
$this->write('F');
}
++$this->progress;
if (($this->progress % self::NUMBER_OF_COLUMNS) !== 0) {
return;
}
$this->printOverview();
$this->write(PHP_EOL);
}
public function finish(): void
{
$this->write(PHP_EOL);
}
protected function getOverview() : string
{
if ($this->number_of_tasks === null) {
throw new \LogicException('Progress::start() should be called before Progress::startDone()');
}
$leadingSpaces = 1 + strlen((string) $this->number_of_tasks) - strlen((string) $this->progress);
$percentage = round($this->progress / $this->number_of_tasks * 100);
return sprintf(
'%s%s / %s (%s%%)',
str_repeat(' ', $leadingSpaces),
$this->progress,
$this->number_of_tasks,
$percentage
);
}
private function printOverview(): void
{
$this->write($this->getOverview());
}
}
@@ -36,4 +36,15 @@ protected function write(string $message): void
{
fwrite(STDERR, $message);
}
protected static function doesTerminalSupportUtf8() : bool
{
if (\strtoupper(\substr(PHP_OS, 0, 3)) === 'WIN') {
if (!\function_exists('sapi_windows_cp_is_utf8') || !\sapi_windows_cp_is_utf8()) {
return false;
}
}
return true;
}
}

4 comments on commit dedd4ac

@muglug

This comment has been minimized.

Copy link
Member Author

replied May 30, 2019

Progress bar looks like

$ ../psalm/psalm --threads=10 --show-info=false
Scanning files...
Analyzing files...

░░░░░░░░░░░░░░░░░░░░░░░░░░                                     2455 / 5581 (44%)
@iluuu1994

This comment has been minimized.

Copy link
Contributor

replied May 30, 2019

Looks great!

Looks like the closing bracket isn't properly cleaned when the progress is finished:

image

@muglug

This comment has been minimized.

Copy link
Member Author

replied May 30, 2019

Should be now

@iluuu1994

This comment has been minimized.

Copy link
Contributor

replied May 30, 2019

It is 🙂👍

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