From 62c98d88364ffea048d8e7fab40df9a28017b56c Mon Sep 17 00:00:00 2001 From: Andrey Helldar Date: Tue, 17 Feb 2026 19:44:33 +0300 Subject: [PATCH 1/2] Refactor `ProgressBarView` --- src/View/ProgressBarView.php | 49 ++++++++++++++++++++++++++++++------ 1 file changed, 42 insertions(+), 7 deletions(-) diff --git a/src/View/ProgressBarView.php b/src/View/ProgressBarView.php index 9c69299..7a7c26c 100644 --- a/src/View/ProgressBarView.php +++ b/src/View/ProgressBarView.php @@ -9,12 +9,13 @@ use function floor; use function max; use function str_repeat; +use function vsprintf; class ProgressBarView extends View implements ProgressBar { protected int $current = 0; - protected int $barWidth = 28; + protected int $width = 28; protected int $total = 100; @@ -22,6 +23,7 @@ class ProgressBarView extends View implements ProgressBar * Creates a progress bar with the specified total number of steps. * * @param int $total The total number of steps. + * * @return $this */ public function create(int $total): static @@ -62,15 +64,48 @@ public function finish(): void */ protected function display(): void { - $percent = $this->current / $this->total; - $filled = (int) floor($percent * $this->barWidth); - $empty = $this->barWidth - $filled; - $percText = (int) floor($percent * 100); + $percent = $this->percent(); - $bar = str_repeat('▓', $filled) . str_repeat('░', $empty); + $filled = $this->filledText($percent); + $empty = $this->emptyText($filled); - $line = " {$this->current}/{$this->total} [{$bar}] {$percText}%"; + $line = $this->buildLine($filled, $empty, $percent); $this->write("\r" . $line); } + + protected function buildLine(int $filled, int $empty, float $percent): string + { + return vsprintf(' %s/%s [%s] %s%%', [ + $this->current, + $this->total, + $this->buildBar($filled, $empty), + $this->percentText($percent), + ]); + } + + protected function buildBar(int $filled, int $empty): string + { + return str_repeat('█', $filled) . str_repeat('░', $empty); + } + + protected function percent(): float + { + return $this->current / $this->total; + } + + protected function filledText(float $percent): int + { + return (int) floor($percent * $this->width); + } + + protected function emptyText(int $filled): int + { + return $this->width - $filled; + } + + protected function percentText(float $percent): int + { + return (int) floor($percent * 100); + } } From c885c34a377ed18750b3f401e6aed0b521815c5f Mon Sep 17 00:00:00 2001 From: Andrey Helldar Date: Tue, 17 Feb 2026 19:48:50 +0300 Subject: [PATCH 2/2] Refactor `ProgressBarView` --- src/View/ProgressBarView.php | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/src/View/ProgressBarView.php b/src/View/ProgressBarView.php index 7a7c26c..94b4d33 100644 --- a/src/View/ProgressBarView.php +++ b/src/View/ProgressBarView.php @@ -77,8 +77,8 @@ protected function display(): void protected function buildLine(int $filled, int $empty, float $percent): string { return vsprintf(' %s/%s [%s] %s%%', [ - $this->current, - $this->total, + $this->numberFormat($this->current), + $this->numberFormat($this->total), $this->buildBar($filled, $empty), $this->percentText($percent), ]); @@ -86,7 +86,7 @@ protected function buildLine(int $filled, int $empty, float $percent): string protected function buildBar(int $filled, int $empty): string { - return str_repeat('█', $filled) . str_repeat('░', $empty); + return str_repeat('▓', $filled) . str_repeat('░', $empty); } protected function percent(): float @@ -108,4 +108,9 @@ protected function percentText(float $percent): int { return (int) floor($percent * 100); } + + protected function numberFormat(int $number): string + { + return number_format($number, thousands_separator: "'"); + } }