|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace WebServCo\Framework\Cli\Progress; |
|
6
|
|
|
|
|
7
|
|
|
//https://gist.github.com/mayconbordin/2860547 |
|
8
|
|
|
final class Bar |
|
9
|
|
|
{ |
|
10
|
|
|
protected string $type; |
|
11
|
|
|
|
|
12
|
|
|
protected int $width; |
|
13
|
|
|
protected int $padding; |
|
14
|
|
|
protected int $total; |
|
15
|
|
|
protected int $item; |
|
16
|
|
|
|
|
17
|
|
|
protected string $outBar; |
|
18
|
|
|
protected string $outPad; |
|
19
|
|
|
protected string $outMessage; |
|
20
|
|
|
|
|
21
|
|
|
public function __construct(int $width = 20) |
|
22
|
|
|
{ |
|
23
|
|
|
$this->type = 'single_line'; |
|
24
|
|
|
$this->width = $width; |
|
25
|
|
|
$this->padding = 25; |
|
26
|
|
|
$this->total = 100; |
|
27
|
|
|
$this->item = 1; |
|
28
|
|
|
} |
|
29
|
|
|
|
|
30
|
|
|
public function start(int $total = 100): bool |
|
31
|
|
|
{ |
|
32
|
|
|
$this->total = $total; |
|
33
|
|
|
return true; |
|
34
|
|
|
} |
|
35
|
|
|
|
|
36
|
|
|
public function advanceTo(int $item): bool |
|
37
|
|
|
{ |
|
38
|
|
|
$this->item = $item; |
|
39
|
|
|
return true; |
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
|
|
public function setType(string $type): bool |
|
43
|
|
|
{ |
|
44
|
|
|
if (!\in_array($type, ['single_line', 'multi_line'], true)) { |
|
45
|
|
|
throw new \InvalidArgumentException('Invalid type.'); |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
$this->type = $type; |
|
49
|
|
|
return true; |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
public function prefix(string $message = ''): string |
|
53
|
|
|
{ |
|
54
|
|
|
switch ($this->type) { |
|
55
|
|
|
case 'single_line': |
|
56
|
|
|
case 'multi_line': |
|
57
|
|
|
return $this->prefixProgress($message); |
|
58
|
|
|
default: |
|
59
|
|
|
throw new \InvalidArgumentException('Invalid type.'); |
|
60
|
|
|
} |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
public function suffix(bool $result = true): string |
|
64
|
|
|
{ |
|
65
|
|
|
switch ($this->type) { |
|
66
|
|
|
case 'single_line': |
|
67
|
|
|
return $this->suffixSingle($result); |
|
68
|
|
|
case 'multi_line': |
|
69
|
|
|
return $this->suffixMulti($result); |
|
70
|
|
|
default: |
|
71
|
|
|
throw new \InvalidArgumentException('Invalid type.'); |
|
72
|
|
|
} |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
public function finish(): string |
|
76
|
|
|
{ |
|
77
|
|
|
return "\033[" . 0 . 'D' . \str_repeat(' ', 74) . "\r"; |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
protected function prefixProgress(string $message): string |
|
81
|
|
|
{ |
|
82
|
|
|
$percent = \round($this->item * 100 / $this->total); |
|
83
|
|
|
$bar = (int) \round($this->width * $percent / 100); |
|
84
|
|
|
$this->outBar = \sprintf( |
|
85
|
|
|
"%s%% [%s>%s] %s", |
|
86
|
|
|
$percent, |
|
87
|
|
|
\str_repeat('=', $bar), |
|
88
|
|
|
\str_repeat(' ', (int) \round($this->width - $bar)), |
|
89
|
|
|
$this->item . '/' . $this->total, |
|
90
|
|
|
); |
|
91
|
|
|
$this->outMessage = $message; |
|
92
|
|
|
|
|
93
|
|
|
$padLen = $this->width + $this->padding - \strlen($this->outBar); |
|
94
|
|
|
$this->outPad = 0 < $padLen |
|
95
|
|
|
? \str_repeat(' ', (int) $padLen) |
|
96
|
|
|
: ''; |
|
97
|
|
|
return $this->outBar . $this->outPad . $this->outMessage; |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
|
|
protected function suffixSingle(bool $result, bool $overwrite = false): string |
|
101
|
|
|
{ |
|
102
|
|
|
$totalLen = \strlen($this->outBar . $this->outPad . $this->outMessage); |
|
103
|
|
|
$output = null; |
|
104
|
|
|
|
|
105
|
|
|
if ($overwrite) { |
|
106
|
|
|
//overwrite current line |
|
107
|
|
|
$output .= "\033[" . $totalLen . 'D'; |
|
108
|
|
|
$output .= \str_repeat(' ', $this->width + $this->padding); |
|
109
|
|
|
$output .= $this->outMessage; |
|
110
|
|
|
} |
|
111
|
|
|
|
|
112
|
|
|
$padLen = 74 - $totalLen; |
|
113
|
|
|
if (0 < $padLen) { |
|
114
|
|
|
$output .= \str_repeat(' ', $padLen); |
|
115
|
|
|
} |
|
116
|
|
|
$output .= '['; |
|
117
|
|
|
$output .= $result |
|
118
|
|
|
? "\e[32mOK" |
|
119
|
|
|
: "\e[31mKO"; |
|
120
|
|
|
$output .= "\e[0m" . ']'; |
|
121
|
|
|
$output .= "\r"; |
|
122
|
|
|
|
|
123
|
|
|
return $output; |
|
124
|
|
|
} |
|
125
|
|
|
|
|
126
|
|
|
protected function suffixMulti(bool $result, bool $overwrite = true): string |
|
127
|
|
|
{ |
|
128
|
|
|
$output = $this->suffixSingle($result, $overwrite); |
|
129
|
|
|
$output .= \PHP_EOL; |
|
130
|
|
|
|
|
131
|
|
|
return $output; |
|
132
|
|
|
} |
|
133
|
|
|
} |
|
134
|
|
|
|