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