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 they pass in a total, set the total |
||
58 | * |
||
59 | * @param integer $total |
||
60 | */ |
||
61 | 44 | public function __construct($total = null) |
|
67 | |||
68 | /** |
||
69 | * Set the total property |
||
70 | * |
||
71 | * @param integer $total |
||
72 | * |
||
73 | * @return Progress |
||
74 | */ |
||
75 | 40 | public function total($total) |
|
81 | |||
82 | /** |
||
83 | * Determines the current percentage we are at and re-writes the progress bar |
||
84 | * |
||
85 | * @param integer $current |
||
86 | * @param mixed $label |
||
87 | * @throws \Exception |
||
88 | */ |
||
89 | 44 | public function current($current, $label = null) |
|
105 | |||
106 | /** |
||
107 | * Increments the current position we are at and re-writes the progress bar |
||
108 | * |
||
109 | * @param integer $increment The number of items to increment by |
||
110 | * @param string $label |
||
111 | */ |
||
112 | 8 | public function advance($increment = 1, $label = null) |
|
116 | |||
117 | /** |
||
118 | * Force the progress bar to redraw every time regardless of whether it has changed or not |
||
119 | * |
||
120 | * @param boolean $force |
||
121 | * @return Progress |
||
122 | */ |
||
123 | 8 | public function forceRedraw($force = true) |
|
129 | |||
130 | /** |
||
131 | * Draw the progress bar, if necessary |
||
132 | * |
||
133 | * @param string $current |
||
134 | * @param string $label |
||
135 | */ |
||
136 | 36 | protected function drawProgressBar($current, $label) |
|
147 | |||
148 | /** |
||
149 | * Build the progress bar str and return it |
||
150 | * |
||
151 | * @param integer $current |
||
152 | * @param string $label |
||
153 | * |
||
154 | * @return string |
||
155 | */ |
||
156 | 36 | protected function getProgressBar($current, $label) |
|
175 | |||
176 | /** |
||
177 | * Get the progress bar string, basically: |
||
178 | * =============> 50% label |
||
179 | * |
||
180 | * @param integer $current |
||
181 | * @param string $label |
||
182 | * |
||
183 | * @return string |
||
184 | */ |
||
185 | 36 | protected function getProgressBarStr($current, $label) |
|
199 | |||
200 | /** |
||
201 | * Get the string for the actual bar based on the current length |
||
202 | * |
||
203 | * @param integer $length |
||
204 | * |
||
205 | * @return string |
||
206 | */ |
||
207 | 36 | protected function getBar($length) |
|
214 | |||
215 | /** |
||
216 | * Get the length of the bar string based on the width of the terminal window |
||
217 | * |
||
218 | * @return integer |
||
219 | */ |
||
220 | 36 | protected function getBarStrLen() |
|
229 | |||
230 | /** |
||
231 | * Format the percentage so it looks pretty |
||
232 | * |
||
233 | * @param integer $percentage |
||
234 | * @return float |
||
235 | */ |
||
236 | 36 | protected function percentageFormatted($percentage) |
|
240 | |||
241 | /** |
||
242 | * Format the label so it is positioned correctly |
||
243 | * |
||
244 | * @param string $label |
||
245 | * @return string |
||
246 | */ |
||
247 | 8 | protected function labelFormatted($label) |
|
251 | |||
252 | /** |
||
253 | * Determine whether the progress bar has changed and we need to redrew |
||
254 | * |
||
255 | * @param string $percentage |
||
256 | * @param string $label |
||
257 | * |
||
258 | * @return boolean |
||
259 | */ |
||
260 | 36 | protected function shouldRedraw($percentage, $label) |
|
264 | } |
||
265 |