Skip to content

Commit a1891d3

Browse files
authored
Improve display of progress bar fraction completed (#204)
1 parent 5cd1b5f commit a1891d3

File tree

2 files changed

+18
-10
lines changed

2 files changed

+18
-10
lines changed

src/Themes/Default/ProgressRenderer.php

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,31 +27,31 @@ public function __invoke(Progress $progress): string
2727
->box(
2828
$this->dim($this->truncate($progress->label, $progress->terminal()->cols() - 6)),
2929
$this->dim($filled),
30-
info: $progress->progress.'/'.$progress->total,
30+
info: $this->fractionCompleted($progress),
3131
),
3232

3333
'error' => $this
3434
->box(
3535
$this->truncate($progress->label, $progress->terminal()->cols() - 6),
3636
$this->dim($filled),
3737
color: 'red',
38-
info: $progress->progress.'/'.$progress->total,
38+
info: $this->fractionCompleted($progress),
3939
),
4040

4141
'cancel' => $this
4242
->box(
4343
$this->truncate($progress->label, $progress->terminal()->cols() - 6),
4444
$this->dim($filled),
4545
color: 'red',
46-
info: $progress->progress.'/'.$progress->total,
46+
info: $this->fractionCompleted($progress),
4747
)
4848
->error($progress->cancelMessage),
4949

5050
default => $this
5151
->box(
5252
$this->cyan($this->truncate($progress->label, $progress->terminal()->cols() - 6)),
5353
$this->dim($filled),
54-
info: $progress->progress.'/'.$progress->total,
54+
info: $this->fractionCompleted($progress),
5555
)
5656
->when(
5757
$progress->hint,
@@ -60,4 +60,12 @@ public function __invoke(Progress $progress): string
6060
)
6161
};
6262
}
63+
64+
/**
65+
* @param Progress<int|iterable<mixed>> $progress
66+
*/
67+
protected function fractionCompleted(Progress $progress): string
68+
{
69+
return number_format($progress->progress).' / '.number_format($progress->total);
70+
}
6371
}

tests/Feature/ProgressTest.php

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,28 +16,28 @@
1616
Prompt::assertStrippedOutputContains(<<<'OUTPUT'
1717
┌ Adding States ───────────────────────────────────────────────┐
1818
│ │
19-
└───────────────────────────────────────────────────────── 0/4 ┘
19+
└─────────────────────────────────────────────────────── 0 / 4 ┘
2020
OUTPUT);
2121

2222
Prompt::assertStrippedOutputContains(<<<'OUTPUT'
2323
│ ███████████████ │
24-
└───────────────────────────────────────────────────────── 1/4 ┘
24+
└─────────────────────────────────────────────────────── 1 / 4 ┘
2525
OUTPUT);
2626

2727
Prompt::assertStrippedOutputContains(<<<'OUTPUT'
2828
│ ██████████████████████████████ │
29-
└───────────────────────────────────────────────────────── 2/4 ┘
29+
└─────────────────────────────────────────────────────── 2 / 4 ┘
3030
OUTPUT);
3131

3232
Prompt::assertStrippedOutputContains(<<<'OUTPUT'
3333
│ █████████████████████████████████████████████ │
34-
└───────────────────────────────────────────────────────── 3/4 ┘
34+
└─────────────────────────────────────────────────────── 3 / 4 ┘
3535
OUTPUT);
3636

3737
Prompt::assertStrippedOutputContains(<<<'OUTPUT'
3838
┌ Adding States ───────────────────────────────────────────────┐
3939
│ ████████████████████████████████████████████████████████████ │
40-
└───────────────────────────────────────────────────────── 4/4 ┘
40+
└─────────────────────────────────────────────────────── 4 / 4 ┘
4141
OUTPUT);
4242
})->with([
4343
'array' => [['Alabama', 'Alaska', 'Arizona', 'Arkansas']],
@@ -60,7 +60,7 @@
6060
Prompt::assertStrippedOutputContains(<<<'OUTPUT'
6161
┌──────────────────────────────────────────────────────────────┐
6262
│ │
63-
└───────────────────────────────────────────────────────── 0/6 ┘
63+
└─────────────────────────────────────────────────────── 0 / 6 ┘
6464
OUTPUT);
6565
});
6666

0 commit comments

Comments
 (0)