Completed
Pull Request — master (#97)
by Craig
07:41
created

Progress::getProgressBar()   B

Complexity

Conditions 4
Paths 6

Size

Total Lines 22
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 11
CRAP Score 4

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 22
ccs 11
cts 11
cp 1
rs 8.9197
cc 4
eloc 12
nc 6
nop 2
crap 4
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 they pass in a total, set the total
58
     *
59
     * @param integer $total
60
     */
61 44
    public function __construct($total = null)
62
    {
63 44
        if ($total !== null) {
64 20
            $this->total($total);
65 20
        }
66 44
    }
67
68
    /**
69
     * Set the total property
70
     *
71
     * @param  integer $total
72
     *
73
     * @return Progress
74
     */
75 40
    public function total($total)
76
    {
77 40
        $this->total = $total;
78
79 40
        return $this;
80
    }
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)
90
    {
91 44
        if ($this->total == 0) {
92
            // Avoid dividing by 0
93 4
            throw new \Exception('The progress total must be greater than zero.');
94
        }
95
96 40
        if ($current > $this->total) {
97 4
            throw new \Exception('The current is greater than the total.');
98
        }
99
100 36
        $this->drawProgressBar($current, $label);
101
102 36
        $this->current = $current;
103 36
        $this->label   = $label;
104 36
    }
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)
113
    {
114 8
        $this->current($this->current + $increment, $label);
115 8
    }
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)
124
    {
125 8
        $this->force_redraw = !!$force;
126
127 8
        return $this;
128
    }
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)
137
    {
138 36
        $percentage = $this->percentageFormatted($current / $this->total);
139
140 36
        if ($this->shouldRedraw($percentage, $label)) {
141 36
            $progress_bar = $this->getProgressBar($current, $label);
142 36
            $this->output->write($this->parser->apply($progress_bar));
143 36
        }
144
145 36
        $this->current_percentage = $percentage;
146 36
    }
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)
157
    {
158 36
        if ($this->first_line) {
159
            // Drop down a line (or 2 if we have a label),
160
            // we are about to re-write for the progress bar
161 36
            $this->output->write('');
162 36
            if (strlen($label) > 0) {
163 36
                $this->output->write('');
164
            }
165
            $this->first_line = false;
166 36
        }
167
168 36
        // Move the cursor up one line and clear it to the end
169 36
        $line_count    = (strlen($label) > 0) ? 2 : 1;
170 36
171 36
        $progress_bar  = $this->util->cursor->up($line_count);
172
        $progress_bar .= $this->util->cursor->startOfCurrentLine();
173 36
        $progress_bar .= $this->util->cursor->deleteCurrentLine();
174
        $progress_bar .= $this->getProgressBarStr($current, $label);
175
176
        return $progress_bar;
177
    }
178
179
    /**
180
     * Get the progress bar string, basically:
181
     * =============>             50% label
182
     *
183
     * @param integer $current
184
     * @param string $label
185 36
     *
186
     * @return string
187 36
     */
188 36
    protected function getProgressBarStr($current, $label)
189
    {
190 36
        $percentage = $current / $this->total;
191 36
        $bar_length = round($this->getBarStrLen() * $percentage);
192
193 36
        $bar        = $this->getBar($bar_length);
194 8
        $number     = $this->percentageFormatted($percentage);
195 8
196
        if ($label) {
197 36
            $label = $this->labelFormatted($label);
198
        }
199
200
        return trim("{$bar} {$number}{$label}");
201
    }
202
203
    /**
204
     * Get the string for the actual bar based on the current length
205
     *
206
     * @param integer $length
207 36
     *
208
     * @return string
209 36
     */
210 36
    protected function getBar($length)
211
    {
212 36
        $bar     = str_repeat('=', $length);
213
        $padding = str_repeat(' ', $this->getBarStrLen() - $length);
214
215
        return "{$bar}>{$padding}";
216
    }
217
218
    /**
219
     * Get the length of the bar string based on the width of the terminal window
220 36
     *
221
     * @return integer
222 36
     */
223
    protected function getBarStrLen()
224 36
    {
225 36
        if (!$this->bar_str_len) {
226
            // Subtract 10 because of the '> 100%' plus some padding, max 100
227 36
            $this->bar_str_len = min($this->util->width() - 10, 100);
228
        }
229
230
        return $this->bar_str_len;
231
    }
232
233
    /**
234
     * Format the percentage so it looks pretty
235
     *
236 36
     * @param integer $percentage
237
     * @return float
238 36
     */
239
    protected function percentageFormatted($percentage)
240
    {
241
        return round($percentage * 100) . '%';
242
    }
243
244
    /**
245
     * Format the label so it is positioned correctly
246
     *
247 8
     * @param string $label
248
     * @return string
249 8
     */
250
    protected function labelFormatted($label)
251
    {
252
        return "\n" . $this->util->cursor->startOfCurrentLine() . $this->util->cursor->deleteCurrentLine() . $label;
253
    }
254
255
    /**
256
     * Determine whether the progress bar has changed and we need to redrew
257
     *
258
     * @param string $percentage
259
     * @param string $label
260 36
     *
261
     * @return boolean
262 36
     */
263
    protected function shouldRedraw($percentage, $label)
264
    {
265
        return ($this->force_redraw || $percentage != $this->current_percentage || $label != $this->label);
266
    }
267
}
268