|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace AlecRabbit\Spinner\Extras\Procedure; |
|
6
|
|
|
|
|
7
|
|
|
use AlecRabbit\Spinner\Contract\IFrame; |
|
8
|
|
|
use AlecRabbit\Spinner\Core\Factory\FrameFactory; |
|
9
|
|
|
use AlecRabbit\Spinner\Extras\Contract\IProgressBarSprite; |
|
10
|
|
|
use AlecRabbit\Spinner\Extras\Contract\IProgressValue; |
|
11
|
|
|
use AlecRabbit\Spinner\Extras\Procedure\A\AProgressValueProcedure; |
|
12
|
|
|
|
|
13
|
|
|
/** @psalm-suppress UnusedClass */ |
|
14
|
|
|
final class ProgressBarProcedure extends AProgressValueProcedure |
|
15
|
|
|
{ |
|
16
|
|
|
protected const UNITS = 5; |
|
17
|
|
|
protected string $open; |
|
18
|
|
|
protected string $close; |
|
19
|
|
|
protected string $empty; |
|
20
|
|
|
protected string $done; |
|
21
|
|
|
protected string $cursor; |
|
22
|
|
|
protected float $cursorThreshold; |
|
23
|
|
|
|
|
24
|
|
|
|
|
25
|
|
|
public function __construct( |
|
26
|
|
|
IProgressValue $progressValue, |
|
27
|
|
|
protected ?IProgressBarSprite $sprite = null, |
|
28
|
|
|
protected int $units = self::UNITS, |
|
29
|
|
|
protected bool $withCursor = true, |
|
30
|
|
|
) { |
|
31
|
|
|
parent::__construct($progressValue); |
|
32
|
|
|
|
|
33
|
|
|
$this->init(); |
|
34
|
|
|
} |
|
35
|
|
|
|
|
36
|
|
|
protected function init(): void |
|
37
|
|
|
{ |
|
38
|
|
|
$this->cursorThreshold = $this->progressValue->getMax(); |
|
39
|
|
|
$this->units = $this->withCursor ? $this->units - 1 : $this->units; |
|
40
|
|
|
$this->open = $this->sprite ? $this->sprite->getOpen() : '['; |
|
41
|
|
|
$this->close = $this->sprite ? $this->sprite->getClose() : ']'; |
|
42
|
|
|
$this->empty = $this->sprite ? $this->sprite->getEmpty() : '-'; |
|
43
|
|
|
$this->done = $this->sprite ? $this->sprite->getDone() : '='; |
|
44
|
|
|
$this->cursor = $this->sprite ? $this->sprite->getCursor() : '>'; |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
|
|
private function getCursor(float $fraction): string |
|
48
|
|
|
{ |
|
49
|
|
|
return |
|
50
|
|
|
$fraction >= $this->cursorThreshold |
|
51
|
|
|
? $this->done |
|
52
|
|
|
: $this->cursor; |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
public function update(float $dt = null): IFrame |
|
56
|
|
|
{ |
|
57
|
|
|
if ($this->progressValue->isFinished()) { |
|
58
|
|
|
if ($this->finishedDelay < 0) { |
|
59
|
|
|
return FrameFactory::createEmpty(); |
|
60
|
|
|
} |
|
61
|
|
|
$this->finishedDelay -= $dt ?? 0.0; |
|
62
|
|
|
} |
|
63
|
|
|
$v = $this->createBar($this->progressValue->getValue()); |
|
64
|
|
|
return |
|
65
|
|
|
FrameFactory::create($v); |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
private function createBar(float $progress): string |
|
69
|
|
|
{ |
|
70
|
|
|
$p = (int)($progress * $this->units); |
|
71
|
|
|
|
|
72
|
|
|
$cursor = |
|
73
|
|
|
$this->withCursor |
|
74
|
|
|
? $this->getCursor($progress) |
|
75
|
|
|
: ''; |
|
76
|
|
|
|
|
77
|
|
|
return |
|
78
|
|
|
$this->open . |
|
79
|
|
|
str_repeat($this->done, $p) . |
|
80
|
|
|
$cursor . |
|
81
|
|
|
str_repeat($this->empty, $this->units - $p) . |
|
82
|
|
|
$this->close; |
|
83
|
|
|
} |
|
84
|
|
|
} |
|
85
|
|
|
|