1 | <?php |
||
7 | class Progress extends DynamicTerminalObject |
||
8 | { |
||
9 | /** |
||
10 | * The total number of items involved |
||
11 | * |
||
12 | * @var integer $total |
||
13 | */ |
||
14 | protected $total = 0; |
||
15 | |||
16 | /** |
||
17 | * The current item that the progress bar represents |
||
18 | * |
||
19 | * @var integer $current |
||
20 | */ |
||
21 | protected $current = 0; |
||
22 | |||
23 | /** |
||
24 | * The current percentage displayed |
||
25 | * |
||
26 | * @var string $current_percentage |
||
27 | */ |
||
28 | protected $current_percentage = ''; |
||
29 | |||
30 | /** |
||
31 | * The string length of the bar when at 100% |
||
32 | * |
||
33 | * @var integer $bar_str_len |
||
34 | */ |
||
35 | protected $bar_str_len; |
||
36 | |||
37 | /** |
||
38 | * Flag indicating whether we are writing the bar for the first time |
||
39 | * |
||
40 | * @var boolean $first_line |
||
41 | */ |
||
42 | protected $first_line = true; |
||
43 | |||
44 | /** |
||
45 | * Current status bar label |
||
46 | * |
||
47 | * @var string $label |
||
48 | */ |
||
49 | protected $label; |
||
50 | |||
51 | /** |
||
52 | * Force a redraw every time |
||
53 | * |
||
54 | * @var boolean $force_redraw |
||
55 | */ |
||
56 | protected $force_redraw = false; |
||
57 | |||
58 | /** |
||
59 | * If this progress bar ever displayed a label. |
||
60 | * |
||
61 | * @var boolean $has_label_line |
||
62 | */ |
||
63 | protected $has_label_line = false; |
||
64 | |||
65 | /** |
||
66 | * If they pass in a total, set the total |
||
67 | * |
||
68 | 52 | * @param integer $total |
|
69 | */ |
||
70 | 52 | public function __construct($total = null) |
|
76 | |||
77 | /** |
||
78 | * Set the total property |
||
79 | * |
||
80 | * @param integer $total |
||
81 | * |
||
82 | 48 | * @return Progress |
|
83 | */ |
||
84 | 48 | public function total($total) |
|
90 | |||
91 | /** |
||
92 | * Determines the current percentage we are at and re-writes the progress bar |
||
93 | * |
||
94 | * @param integer $current |
||
95 | * @param mixed $label |
||
96 | 52 | * |
|
97 | * @return void |
||
98 | 52 | * @throws UnexpectedValueException |
|
99 | */ |
||
100 | 4 | public function current($current, $label = null) |
|
116 | |||
117 | /** |
||
118 | * Increments the current position we are at and re-writes the progress bar |
||
119 | 12 | * |
|
120 | * @param integer $increment The number of items to increment by |
||
121 | 12 | * @param string $label |
|
122 | 12 | */ |
|
123 | public function advance($increment = 1, $label = null) |
||
127 | |||
128 | /** |
||
129 | * Force the progress bar to redraw every time regardless of whether it has changed or not |
||
130 | 8 | * |
|
131 | * @param boolean $force |
||
132 | 8 | * @return Progress |
|
133 | */ |
||
134 | 8 | public function forceRedraw($force = true) |
|
140 | |||
141 | |||
142 | /** |
||
143 | 4 | * Update a progress bar using an iterable. |
|
144 | * |
||
145 | 4 | * @param iterable $items Array or any other iterable object |
|
146 | 4 | * @param callable $callback A handler to run on each item |
|
147 | 4 | */ |
|
148 | public function each($items, callable $callback = null) |
||
170 | 44 | ||
171 | 44 | ||
172 | /** |
||
173 | * Draw the progress bar, if necessary |
||
174 | * |
||
175 | * @param string $current |
||
176 | * @param string $label |
||
177 | */ |
||
178 | protected function drawProgressBar($current, $label) |
||
189 | |||
190 | /** |
||
191 | 44 | * Build the progress bar str and return it |
|
192 | * |
||
193 | 44 | * @param integer $current |
|
194 | 44 | * @param string $label |
|
195 | 44 | * |
|
196 | 44 | * @return string |
|
197 | */ |
||
198 | protected function getProgressBar($current, $label) |
||
222 | |||
223 | 44 | /** |
|
224 | 12 | * Get the progress bar string, basically: |
|
225 | * =============> 50% label |
||
226 | * |
||
227 | 44 | * @param integer $current |
|
228 | 4 | * @param string $label |
|
229 | 4 | * |
|
230 | * @return string |
||
231 | 44 | */ |
|
232 | protected function getProgressBarStr($current, $label) |
||
250 | |||
251 | /** |
||
252 | * Get the string for the actual bar based on the current length |
||
253 | * |
||
254 | 44 | * @param integer $length |
|
255 | * |
||
256 | 44 | * @return string |
|
257 | */ |
||
258 | 44 | protected function getBar($length) |
|
265 | |||
266 | /** |
||
267 | * Get the length of the bar string based on the width of the terminal window |
||
268 | * |
||
269 | * @return integer |
||
270 | 44 | */ |
|
271 | protected function getBarStrLen() |
||
280 | |||
281 | 12 | /** |
|
282 | * Format the percentage so it looks pretty |
||
283 | 12 | * |
|
284 | * @param integer $percentage |
||
285 | * @return float |
||
286 | */ |
||
287 | protected function percentageFormatted($percentage) |
||
291 | |||
292 | /** |
||
293 | * Format the label so it is positioned correctly |
||
294 | 44 | * |
|
295 | * @param string $label |
||
296 | 44 | * @return string |
|
297 | */ |
||
298 | protected function labelFormatted($label) |
||
302 | |||
303 | /** |
||
304 | * Determine whether the progress bar has changed and we need to redrew |
||
305 | * |
||
306 | * @param string $percentage |
||
307 | * @param string $label |
||
308 | * |
||
309 | * @return boolean |
||
310 | */ |
||
311 | protected function shouldRedraw($percentage, $label) |
||
315 | } |
||
316 |