Completed
Push — master ( 5b0b35...d2344e )
by Craig
17:16 queued 06:11
created

Progress   A

Complexity

Total Complexity 25

Size/Duplication

Total Lines 276
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 25
lcom 1
cbo 5
dl 0
loc 276
ccs 71
cts 71
cp 1
rs 10
c 0
b 0
f 0

13 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 2
A total() 0 6 1
A current() 0 16 3
A advance() 0 4 1
A forceRedraw() 0 6 1
A drawProgressBar() 0 11 2
B getProgressBar() 0 24 4
A getProgressBarStr() 0 18 3
A getBar() 0 7 1
A getBarStrLen() 0 9 2
A percentageFormatted() 0 4 1
A labelFormatted() 0 4 1
A shouldRedraw() 0 4 3
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 48
    public function __construct($total = null)
69
    {
70 48
        if ($total !== null) {
71 24
            $this->total($total);
72 24
        }
73 48
    }
74
75
    /**
76
     * Set the total property
77
     *
78
     * @param  integer $total
79
     *
80
     * @return Progress
81
     */
82 44
    public function total($total)
83
    {
84 44
        $this->total = $total;
85
86 44
        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 48
    public function current($current, $label = null)
97
    {
98 48
        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 44
        if ($current > $this->total) {
104 4
            throw new \Exception('The current is greater than the total.');
105
        }
106
107 40
        $this->drawProgressBar($current, $label);
108
109 40
        $this->current = $current;
110 40
        $this->label   = $label;
111 40
    }
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 8
    public function advance($increment = 1, $label = null)
120
    {
121 8
        $this->current($this->current + $increment, $label);
122 8
    }
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
     * Draw the progress bar, if necessary
139
     *
140
     * @param string $current
141
     * @param string $label
142
     */
143 40
    protected function drawProgressBar($current, $label)
144
    {
145 40
        $percentage = $this->percentageFormatted($current / $this->total);
146
147 40
        if ($this->shouldRedraw($percentage, $label)) {
148 40
            $progress_bar = $this->getProgressBar($current, $label);
149 40
            $this->output->write($this->parser->apply($progress_bar));
150 40
        }
151
152 40
        $this->current_percentage = $percentage;
153 40
    }
154
155
    /**
156
     * Build the progress bar str and return it
157
     *
158
     * @param integer $current
159
     * @param string $label
160
     *
161
     * @return string
162
     */
163 40
    protected function getProgressBar($current, $label)
164
    {
165 40
        if ($this->first_line) {
166
            // Drop down a line, we are about to
167
            // re-write this line for the progress bar
168 40
            $this->output->write('');
169 40
            $this->first_line = false;
170 40
        }
171
172
        // Move the cursor up and clear it to the end
173 40
        $line_count = $this->has_label_line ? 2 : 1;
174
175 40
        $progress_bar  = $this->util->cursor->up($line_count);
176 40
        $progress_bar .= $this->util->cursor->startOfCurrentLine();
177 40
        $progress_bar .= $this->util->cursor->deleteCurrentLine();
178 40
        $progress_bar .= $this->getProgressBarStr($current, $label);
179
180
        // If this line has a label then set that this progress bar has a label line
181 40
        if (strlen($label) > 0) {
182 12
            $this->has_label_line = true;
183 12
        }
184
185 40
        return $progress_bar;
186
    }
187
188
    /**
189
     * Get the progress bar string, basically:
190
     * =============>             50% label
191
     *
192
     * @param integer $current
193
     * @param string $label
194
     *
195
     * @return string
196
     */
197 40
    protected function getProgressBarStr($current, $label)
198
    {
199 40
        $percentage = $current / $this->total;
200 40
        $bar_length = round($this->getBarStrLen() * $percentage);
201
202 40
        $bar        = $this->getBar($bar_length);
203 40
        $number     = $this->percentageFormatted($percentage);
204
205 40
        if ($label) {
206 12
            $label = $this->labelFormatted($label);
207
        // If this line doesn't have a label, but we've had one before,
208
        // then ensure the label line is cleared
209 40
        } elseif ($this->has_label_line) {
210 4
            $label = $this->labelFormatted('');
211 4
        }
212
213 40
        return trim("{$bar} {$number}{$label}");
214
    }
215
216
    /**
217
     * Get the string for the actual bar based on the current length
218
     *
219
     * @param integer $length
220
     *
221
     * @return string
222
     */
223 40
    protected function getBar($length)
224
    {
225 40
        $bar     = str_repeat('=', $length);
226 40
        $padding = str_repeat(' ', $this->getBarStrLen() - $length);
227
228 40
        return "{$bar}>{$padding}";
229
    }
230
231
    /**
232
     * Get the length of the bar string based on the width of the terminal window
233
     *
234
     * @return integer
235
     */
236 40
    protected function getBarStrLen()
237
    {
238 40
        if (!$this->bar_str_len) {
239
            // Subtract 10 because of the '> 100%' plus some padding, max 100
240 40
            $this->bar_str_len = min($this->util->width() - 10, 100);
241 40
        }
242
243 40
        return $this->bar_str_len;
244
    }
245
246
    /**
247
     * Format the percentage so it looks pretty
248
     *
249
     * @param integer $percentage
250
     * @return float
251
     */
252 40
    protected function percentageFormatted($percentage)
253
    {
254 40
        return round($percentage * 100) . '%';
255
    }
256
257
    /**
258
     * Format the label so it is positioned correctly
259
     *
260
     * @param string $label
261
     * @return string
262
     */
263 12
    protected function labelFormatted($label)
264
    {
265 12
        return "\n" . $this->util->cursor->startOfCurrentLine() . $this->util->cursor->deleteCurrentLine() . $label;
266
    }
267
268
    /**
269
     * Determine whether the progress bar has changed and we need to redrew
270
     *
271
     * @param string $percentage
272
     * @param string $label
273
     *
274
     * @return boolean
275
     */
276 40
    protected function shouldRedraw($percentage, $label)
277
    {
278 40
        return ($this->force_redraw || $percentage != $this->current_percentage || $label != $this->label);
279
    }
280
}
281