1 | <?php |
||
5 | class Progress extends DynamicTerminalObject |
||
6 | { |
||
7 | /** |
||
8 | * The total number of items involved |
||
9 | * |
||
10 | * @var integer $total |
||
11 | */ |
||
12 | protected $total = 0; |
||
13 | |||
14 | /** |
||
15 | * The current item that the progress bar represents |
||
16 | * |
||
17 | * @var integer $current |
||
18 | */ |
||
19 | protected $current = 0; |
||
20 | |||
21 | /** |
||
22 | * The current percentage displayed |
||
23 | * |
||
24 | * @var string $current_percentage |
||
25 | */ |
||
26 | protected $current_percentage = ''; |
||
27 | |||
28 | /** |
||
29 | * The string length of the bar when at 100% |
||
30 | * |
||
31 | * @var integer $bar_str_len |
||
32 | */ |
||
33 | protected $bar_str_len; |
||
34 | |||
35 | /** |
||
36 | * Flag indicating whether we are writing the bar for the first time |
||
37 | * |
||
38 | * @var boolean $first_line |
||
39 | */ |
||
40 | protected $first_line = true; |
||
41 | |||
42 | /** |
||
43 | * Current status bar label |
||
44 | * |
||
45 | * @var string $label |
||
46 | */ |
||
47 | protected $label; |
||
48 | |||
49 | /** |
||
50 | * Force a redraw every time |
||
51 | * |
||
52 | * @var boolean $force_redraw |
||
53 | */ |
||
54 | protected $force_redraw = false; |
||
55 | |||
56 | /** |
||
57 | * If this progress bar ever displayed a label. |
||
58 | * |
||
59 | * @var boolean $has_label_line |
||
60 | */ |
||
61 | 48 | protected $has_label_line = false; |
|
62 | |||
63 | 48 | /** |
|
64 | 20 | * If they pass in a total, set the total |
|
65 | 20 | * |
|
66 | 48 | * @param integer $total |
|
67 | */ |
||
68 | public function __construct($total = null) |
||
74 | |||
75 | 44 | /** |
|
76 | * Set the total property |
||
77 | 44 | * |
|
78 | * @param integer $total |
||
79 | 44 | * |
|
80 | * @return Progress |
||
81 | */ |
||
82 | public function total($total) |
||
88 | |||
89 | 48 | /** |
|
90 | * Determines the current percentage we are at and re-writes the progress bar |
||
91 | 48 | * |
|
92 | * @param integer $current |
||
93 | 4 | * @param mixed $label |
|
94 | * @throws \Exception |
||
95 | */ |
||
96 | 44 | public function current($current, $label = null) |
|
112 | 12 | ||
113 | /** |
||
114 | 12 | * Increments the current position we are at and re-writes the progress bar |
|
115 | 12 | * |
|
116 | * @param integer $increment The number of items to increment by |
||
117 | * @param string $label |
||
118 | */ |
||
119 | public function advance($increment = 1, $label = null) |
||
123 | 8 | ||
124 | /** |
||
125 | 8 | * Force the progress bar to redraw every time regardless of whether it has changed or not |
|
126 | * |
||
127 | 8 | * @param boolean $force |
|
128 | * @return Progress |
||
129 | */ |
||
130 | public function forceRedraw($force = true) |
||
136 | 4 | ||
137 | |||
138 | 4 | /** |
|
139 | 4 | * Update a progress bar using an iterable. |
|
140 | 4 | * |
|
141 | * @param iterable $items Array or any other iterable object |
||
142 | 4 | * @param callable $callback A handler to run on each item |
|
143 | 4 | */ |
|
144 | 4 | public function each($items, callable $callback = null) |
|
166 | |||
167 | |||
168 | /** |
||
169 | * Draw the progress bar, if necessary |
||
170 | * |
||
171 | * @param string $current |
||
172 | * @param string $label |
||
173 | */ |
||
174 | 40 | protected function drawProgressBar($current, $label) |
|
185 | |||
186 | 40 | /** |
|
187 | 40 | * Build the progress bar str and return it |
|
188 | 40 | * |
|
189 | 40 | * @param integer $current |
|
190 | * @param string $label |
||
191 | 40 | * |
|
192 | * @return string |
||
193 | */ |
||
194 | protected function getProgressBar($current, $label) |
||
218 | |||
219 | /** |
||
220 | * Get the progress bar string, basically: |
||
221 | * =============> 50% label |
||
222 | * |
||
223 | * @param integer $current |
||
224 | * @param string $label |
||
225 | 40 | * |
|
226 | * @return string |
||
227 | 40 | */ |
|
228 | 40 | protected function getProgressBarStr($current, $label) |
|
246 | |||
247 | /** |
||
248 | * Get the string for the actual bar based on the current length |
||
249 | * |
||
250 | * @param integer $length |
||
251 | * |
||
252 | * @return string |
||
253 | */ |
||
254 | 40 | protected function getBar($length) |
|
261 | |||
262 | /** |
||
263 | * Get the length of the bar string based on the width of the terminal window |
||
264 | * |
||
265 | 8 | * @return integer |
|
266 | */ |
||
267 | 8 | protected function getBarStrLen() |
|
276 | |||
277 | /** |
||
278 | 40 | * Format the percentage so it looks pretty |
|
279 | * |
||
280 | 40 | * @param integer $percentage |
|
281 | * @return float |
||
282 | */ |
||
283 | protected function percentageFormatted($percentage) |
||
287 | |||
288 | /** |
||
289 | * Format the label so it is positioned correctly |
||
290 | * |
||
291 | * @param string $label |
||
292 | * @return string |
||
293 | */ |
||
294 | protected function labelFormatted($label) |
||
298 | |||
299 | /** |
||
300 | * Determine whether the progress bar has changed and we need to redrew |
||
301 | * |
||
302 | * @param string $percentage |
||
303 | * @param string $label |
||
304 | * |
||
305 | * @return boolean |
||
306 | */ |
||
307 | protected function shouldRedraw($percentage, $label) |
||
311 | } |
||
312 |