| 1 |
<?php |
| 2 |
|
| 3 |
namespace Aws\S3\S3Transfer\Progress; |
| 4 |
|
| 5 |
final class ConsoleProgressBar implements ProgressBarInterface |
| 6 |
{ |
| 7 |
public const DEFAULT_PROGRESS_BAR_CHAR = '#'; |
| 8 |
public const DEFAULT_PROGRESS_BAR_WIDTH = 50; |
| 9 |
public const MAX_PROGRESS_BAR_WIDTH = 50; |
| 10 |
|
| 11 |
|
| 12 |
private string $progressBarChar; |
| 13 |
|
| 14 |
|
| 15 |
private int $progressBarWidth; |
| 16 |
|
| 17 |
|
| 18 |
private int $percentCompleted; |
| 19 |
|
| 20 |
|
| 21 |
private AbstractProgressBarFormat $progressBarFormat; |
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
public function __construct( |
| 30 |
string $progressBarChar = self::DEFAULT_PROGRESS_BAR_CHAR, |
| 31 |
int $progressBarWidth = self::DEFAULT_PROGRESS_BAR_WIDTH, |
| 32 |
int $percentCompleted = 0, |
| 33 |
AbstractProgressBarFormat $progressBarFormat = new ColoredTransferProgressBarFormat(), |
| 34 |
) { |
| 35 |
$this->progressBarChar = $progressBarChar; |
| 36 |
$this->progressBarWidth = min( |
| 37 |
$progressBarWidth, |
| 38 |
self::MAX_PROGRESS_BAR_WIDTH |
| 39 |
); |
| 40 |
$this->percentCompleted = $percentCompleted; |
| 41 |
$this->progressBarFormat = $progressBarFormat; |
| 42 |
} |
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
public function getProgressBarChar(): string |
| 48 |
{ |
| 49 |
return $this->progressBarChar; |
| 50 |
} |
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
public function getProgressBarWidth(): int |
| 56 |
{ |
| 57 |
return $this->progressBarWidth; |
| 58 |
} |
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
public function getPercentCompleted(): int |
| 64 |
{ |
| 65 |
return $this->percentCompleted; |
| 66 |
} |
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
public function getProgressBarFormat(): AbstractProgressBarFormat |
| 72 |
{ |
| 73 |
return $this->progressBarFormat; |
| 74 |
} |
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
public function setPercentCompleted(int $percent): void |
| 84 |
{ |
| 85 |
$this->percentCompleted = max(0, min(100, $percent)); |
| 86 |
} |
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
public function render(): string |
| 92 |
{ |
| 93 |
$filledWidth = (int) round(($this->progressBarWidth * $this->percentCompleted) / 100); |
| 94 |
$progressBar = str_repeat($this->progressBarChar, $filledWidth) |
| 95 |
. str_repeat(' ', $this->progressBarWidth - $filledWidth); |
| 96 |
|
| 97 |
|
| 98 |
$this->progressBarFormat->setArg('progress_bar', $progressBar); |
| 99 |
$this->progressBarFormat->setArg('percent', $this->percentCompleted); |
| 100 |
|
| 101 |
return $this->progressBarFormat->format(); |
| 102 |
} |
| 103 |
} |