1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* This file is part of the O2System Framework package. |
4
|
|
|
* |
5
|
|
|
* For the full copyright and license information, please view the LICENSE |
6
|
|
|
* file that was distributed with this source code. |
7
|
|
|
* |
8
|
|
|
* @author Steeve Andrian Salim |
9
|
|
|
* @copyright Copyright (c) Steeve Andrian Salim |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
// ------------------------------------------------------------------------ |
13
|
|
|
|
14
|
|
|
namespace O2System\Kernel\Cli\Writers; |
15
|
|
|
|
16
|
|
|
// ------------------------------------------------------------------------ |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* Class ProgressBar |
20
|
|
|
* |
21
|
|
|
* @package O2System\Kernel\Cli\Writers |
22
|
|
|
*/ |
23
|
|
|
class ProgressBar |
24
|
|
|
{ |
25
|
|
|
/** |
26
|
|
|
* ProgressBar::$columns |
27
|
|
|
* |
28
|
|
|
* @var mixed |
29
|
|
|
*/ |
30
|
|
|
protected $columns; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* ProgressBar::$limiter |
34
|
|
|
* |
35
|
|
|
* @var \O2System\Kernel\Cli\Writers\ProgressBar\Limiter |
36
|
|
|
*/ |
37
|
|
|
protected $limiter; |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* ProgressBar::$units |
41
|
|
|
* |
42
|
|
|
* @var mixed |
43
|
|
|
*/ |
44
|
|
|
protected $units; |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* ProgressBar::$total |
48
|
|
|
* |
49
|
|
|
* @var mixed |
50
|
|
|
*/ |
51
|
|
|
protected $total; |
52
|
|
|
|
53
|
|
|
// ------------------------------------------------------------------------ |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* ProgressBar::__construct |
57
|
|
|
*/ |
58
|
|
|
public function __construct() |
59
|
|
|
{ |
60
|
|
|
// change the fps limit as needed |
61
|
|
|
$this->limiter = new ProgressBar\Limiter(10); |
62
|
|
|
|
63
|
|
|
output()->write(PHP_EOL); |
|
|
|
|
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
// ------------------------------------------------------------------------ |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* ProgressBar::__destruct |
70
|
|
|
*/ |
71
|
|
|
public function __destruct() |
72
|
|
|
{ |
73
|
|
|
$this->write(); |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
// ------------------------------------------------------------------------ |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* ProgressBar::write |
80
|
|
|
* |
81
|
|
|
* Write progress bar. |
82
|
|
|
*/ |
83
|
|
|
protected function write() |
84
|
|
|
{ |
85
|
|
|
$this->updateSize(); |
86
|
|
|
$this->writeStatus($this->units, $this->total, $this->columns, $this->columns); |
|
|
|
|
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
// ------------------------------------------------------------------------ |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* ProgressBar::updateSize |
93
|
|
|
* |
94
|
|
|
* Execute columns update. |
95
|
|
|
*/ |
96
|
|
|
protected function updateSize() |
97
|
|
|
{ |
98
|
|
|
// get the number of columns |
99
|
|
|
$this->columns = exec("tput cols"); |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
// ------------------------------------------------------------------------ |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* ProgressBar::writeStatus |
106
|
|
|
* |
107
|
|
|
* @param $done |
108
|
|
|
* @param $total |
109
|
|
|
* @param int $size |
110
|
|
|
* @param int $lineWidth |
111
|
|
|
*/ |
112
|
|
|
protected function writeStatus($done, $total, $size = 30, $lineWidth = -1) |
113
|
|
|
{ |
114
|
|
|
if ($lineWidth <= 0) { |
115
|
|
|
$lineWidth = input()->env('COLUMNS'); |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
static $start_time; |
119
|
|
|
|
120
|
|
|
// to take account for [ and ] |
121
|
|
|
$size -= 3; |
122
|
|
|
// if we go over our bound, just ignore it |
123
|
|
|
if ($done > $total) { |
124
|
|
|
return; |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
if (empty($start_time)) { |
128
|
|
|
$start_time = time(); |
129
|
|
|
} |
130
|
|
|
$now = time(); |
131
|
|
|
|
132
|
|
|
$percent = (double)($done / $total); |
133
|
|
|
|
134
|
|
|
$bar = floor($percent * $size); |
135
|
|
|
|
136
|
|
|
// jump to the begining |
137
|
|
|
output()->write("\r"); |
138
|
|
|
|
139
|
|
|
// jump a line up |
140
|
|
|
output()->write("\x1b[A"); |
141
|
|
|
|
142
|
|
|
$statusBar = "["; |
143
|
|
|
$statusBar .= str_repeat("=", $bar); |
|
|
|
|
144
|
|
|
if ($bar < $size) { |
145
|
|
|
$statusBar .= ">"; |
146
|
|
|
$statusBar .= str_repeat(" ", $size - $bar); |
147
|
|
|
} else { |
148
|
|
|
$statusBar .= "="; |
149
|
|
|
} |
150
|
|
|
|
151
|
|
|
$percentOutput = number_format($percent * 100, 0); |
152
|
|
|
|
153
|
|
|
$statusBar .= "]"; |
154
|
|
|
$details = "$percentOutput% $done/$total"; |
155
|
|
|
|
156
|
|
|
$rate = ($now - $start_time) / $done; |
157
|
|
|
$left = $total - $done; |
158
|
|
|
$eta = round($rate * $left, 2); |
159
|
|
|
|
160
|
|
|
$elapsed = $now - $start_time; |
161
|
|
|
|
162
|
|
|
|
163
|
|
|
$details .= ' ' . language()->getLine('CLI_PROGRESS_BAR_ESTIMATION') . ': ' . $this->formatTime($eta) . ' ' . language()->getLine('CLI_PROGRESS_BAR_ELAPSED') . ': ' . $this->formatTime($elapsed) . ' '; |
164
|
|
|
|
165
|
|
|
$lineWidth--; |
166
|
|
|
if (strlen($details) >= $lineWidth) { |
167
|
|
|
$details = substr($details, 0, $lineWidth - 1); |
168
|
|
|
} |
169
|
|
|
|
170
|
|
|
output()->write(implode(PHP_EOL, [ |
171
|
|
|
$details, |
172
|
|
|
$statusBar, |
173
|
|
|
])); |
174
|
|
|
|
175
|
|
|
//echo "$details\n$status_bar"; |
176
|
|
|
|
177
|
|
|
flush(); |
178
|
|
|
|
179
|
|
|
// when done, send a newline |
180
|
|
|
if ($done == $total) { |
181
|
|
|
//echo "\n"; |
182
|
|
|
output()->write(PHP_EOL); |
183
|
|
|
} |
184
|
|
|
|
185
|
|
|
} |
186
|
|
|
|
187
|
|
|
// ------------------------------------------------------------------------ |
188
|
|
|
|
189
|
|
|
/** |
190
|
|
|
* ProgressBar::formatTime |
191
|
|
|
* |
192
|
|
|
* @param $time |
193
|
|
|
* |
194
|
|
|
* @return string |
195
|
|
|
*/ |
196
|
|
|
protected function formatTime($time) |
197
|
|
|
{ |
198
|
|
|
if ($time > 100) { |
199
|
|
|
$time /= 60; |
200
|
|
|
if ($time > 100) { |
201
|
|
|
$time /= 60; |
202
|
|
|
|
203
|
|
|
return number_format($time) . " hr"; |
204
|
|
|
} |
205
|
|
|
|
206
|
|
|
return number_format($time) . " min"; |
207
|
|
|
} |
208
|
|
|
|
209
|
|
|
return number_format($time) . " sec"; |
210
|
|
|
} |
211
|
|
|
|
212
|
|
|
// ------------------------------------------------------------------------ |
213
|
|
|
|
214
|
|
|
/** |
215
|
|
|
* ProgressBar::update |
216
|
|
|
* |
217
|
|
|
* @param $units |
218
|
|
|
* @param $total |
219
|
|
|
*/ |
220
|
|
|
public function update($units, $total) |
221
|
|
|
{ |
222
|
|
|
$this->units = $units; |
223
|
|
|
$this->total = $total; |
224
|
|
|
if ( ! $this->limiter->isValid()) { |
225
|
|
|
return; |
226
|
|
|
} |
227
|
|
|
$this->write(); |
228
|
|
|
} |
229
|
|
|
} |
This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.
This is most likely a typographical error or the method has been renamed.