Completed
Push — master ( bc712d...75e37f )
by Craig
05:51
created

Progress::each()   B

Complexity

Conditions 5
Paths 8

Size

Total Lines 22
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 13
CRAP Score 5.1647

Importance

Changes 0
Metric Value
dl 0
loc 22
ccs 13
cts 16
cp 0.8125
rs 8.6737
c 0
b 0
f 0
cc 5
eloc 13
nc 8
nop 2
crap 5.1647
1
<?php
2
3
namespace League\CLImate\TerminalObject\Dynamic;
4
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
    protected $has_label_line = false;
62
63
    /**
64
     * If they pass in a total, set the total
65
     *
66
     * @param integer $total
67
     */
68 64
    public function __construct($total = null)
69
    {
70 64
        if ($total !== null) {
71 24
            $this->total($total);
72 24
        }
73 64
    }
74
75
    /**
76
     * Set the total property
77
     *
78
     * @param  integer $total
79
     *
80
     * @return Progress
81
     */
82 60
    public function total($total)
83
    {
84 60
        $this->total = $total;
85
86 60
        return $this;
87
    }
88
89
    /**
90
     * Determines the current percentage we are at and re-writes the progress bar
91
     *
92
     * @param integer $current
93
     * @param mixed   $label
94
     * @throws \Exception
95
     */
96 64
    public function current($current, $label = null)
97
    {
98 64
        if ($this->total == 0) {
99
            // Avoid dividing by 0
100 4
            throw new \Exception('The progress total must be greater than zero.');
101
        }
102
103 60
        if ($current > $this->total) {
104 4
            throw new \Exception('The current is greater than the total.');
105
        }
106
107 56
        $this->drawProgressBar($current, $label);
108
109 52
        $this->current = $current;
110 52
        $this->label   = $label;
111 52
    }
112
113
    /**
114
     * Increments the current position we are at and re-writes the progress bar
115
     *
116
     * @param integer $increment The number of items to increment by
117
     * @param string $label
118
     */
119 24
    public function advance($increment = 1, $label = null)
120
    {
121 24
        $this->current($this->current + $increment, $label);
122 20
    }
123
124
    /**
125
     * Force the progress bar to redraw every time regardless of whether it has changed or not
126
     *
127
     * @param boolean $force
128
     * @return Progress
129
     */
130 8
    public function forceRedraw($force = true)
131
    {
132 8
        $this->force_redraw = !!$force;
133
134 8
        return $this;
135
    }
136
137
138
    /**
139
     * Update a progress bar using an iterable.
140
     *
141
     * @param iterable $items Array or any other iterable object
142
     * @param callable $callback A handler to run on each item
143
     */
144 16
    public function each($items, callable $callback = null)
145
    {
146 16
        if ($items instanceof \Traversable) {
147
            $items = iterator_to_array($items);
148
        }
149
150 16
        $total = count($items);
151 16
        if (!$total) {
152
            return;
153
        }
154 16
        $this->total($total);
155
156 16
        foreach ($items as $key => $item) {
157 16
            if ($callback) {
158 12
                $label = $callback($item, $key);
159 12
            } else {
160 4
                $label = null;
161
            }
162
163 16
            $this->advance(1, $label);
164 12
        }
165 12
    }
166
167
168
    /**
169
     * Draw the progress bar, if necessary
170
     *
171
     * @param string $current
172
     * @param string $label
173
     */
174 56
    protected function drawProgressBar($current, $label)
175
    {
176 56
        $percentage = $this->percentageFormatted($current / $this->total);
177
178 56
        if ($this->shouldRedraw($percentage, $label)) {
179 56
            $progress_bar = $this->getProgressBar($current, $label);
180 56
            $this->output->write($this->parser->apply($progress_bar));
181 52
        }
182
183 52
        $this->current_percentage = $percentage;
184 52
    }
185
186
    /**
187
     * Build the progress bar str and return it
188
     *
189
     * @param integer $current
190
     * @param string $label
191
     *
192
     * @return string
193
     */
194 56
    protected function getProgressBar($current, $label)
195
    {
196 56
        if ($this->first_line) {
197
            // Drop down a line, we are about to
198
            // re-write this line for the progress bar
199 56
            $this->output->write('');
200 56
            $this->first_line = false;
201 56
        }
202
203
        // Move the cursor up and clear it to the end
204 56
        $line_count = $this->has_label_line ? 2 : 1;
205
206 56
        $progress_bar  = $this->util->cursor->up($line_count);
207 56
        $progress_bar .= $this->util->cursor->startOfCurrentLine();
208 56
        $progress_bar .= $this->util->cursor->deleteCurrentLine();
209 56
        $progress_bar .= $this->getProgressBarStr($current, $label);
210
211
        // If this line has a label then set that this progress bar has a label line
212 56
        if (strlen($label) > 0) {
213 16
            $this->has_label_line = true;
214 16
        }
215
216 56
        return $progress_bar;
217
    }
218
219
    /**
220
     * Get the progress bar string, basically:
221
     * =============>             50% label
222
     *
223
     * @param integer $current
224
     * @param string $label
225
     *
226
     * @return string
227
     */
228 56
    protected function getProgressBarStr($current, $label)
229
    {
230 56
        $percentage = $current / $this->total;
231 56
        $bar_length = round($this->getBarStrLen() * $percentage);
232
233 56
        $bar        = $this->getBar($bar_length);
234 56
        $number     = $this->percentageFormatted($percentage);
235
236 56
        if ($label) {
237 16
            $label = $this->labelFormatted($label);
238
        // If this line doesn't have a label, but we've had one before,
239
        // then ensure the label line is cleared
240 56
        } elseif ($this->has_label_line) {
241 4
            $label = $this->labelFormatted('');
242 4
        }
243
244 56
        return trim("{$bar} {$number}{$label}");
245
    }
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 56
    protected function getBar($length)
255
    {
256 56
        $bar     = str_repeat('=', $length);
257 56
        $padding = str_repeat(' ', $this->getBarStrLen() - $length);
258
259 56
        return "{$bar}>{$padding}";
260
    }
261
262
    /**
263
     * Get the length of the bar string based on the width of the terminal window
264
     *
265
     * @return integer
266
     */
267 56
    protected function getBarStrLen()
268
    {
269 56
        if (!$this->bar_str_len) {
270
            // Subtract 10 because of the '> 100%' plus some padding, max 100
271 56
            $this->bar_str_len = min($this->util->width() - 10, 100);
272 56
        }
273
274 56
        return $this->bar_str_len;
275
    }
276
277
    /**
278
     * Format the percentage so it looks pretty
279
     *
280
     * @param integer $percentage
281
     * @return float
282
     */
283 56
    protected function percentageFormatted($percentage)
284
    {
285 56
        return round($percentage * 100) . '%';
286
    }
287
288
    /**
289
     * Format the label so it is positioned correctly
290
     *
291
     * @param string $label
292
     * @return string
293
     */
294 16
    protected function labelFormatted($label)
295
    {
296 16
        return "\n" . $this->util->cursor->startOfCurrentLine() . $this->util->cursor->deleteCurrentLine() . $label;
297
    }
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 56
    protected function shouldRedraw($percentage, $label)
308
    {
309 56
        return ($this->force_redraw || $percentage != $this->current_percentage || $label != $this->label);
310
    }
311
}
312