Completed
Push — master ( 3ebcb1...9e4cf7 )
by personal
22s queued 17s
created

ProgressBar::hasAnsi()   A

Complexity

Conditions 6
Paths 6

Size

Total Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 6
nc 6
nop 0
dl 0
loc 15
rs 9.2222
c 0
b 0
f 0
1
<?php
2
3
/*
4
 * (c) Jean-François Lépine <https://twitter.com/Halleck45>
5
 *
6
 * For the full copyright and license information, please view the LICENSE
7
 * file that was distributed with this source code.
8
 */
9
10
namespace Hal\Component\Output;
11
12
/**
13
 * @package Hal\Component\Output
14
 */
15
class ProgressBar
16
{
17
18
    /**
19
     * @var Output
20
     */
21
    private $output;
22
23
    /**
24
     * @var int
25
     */
26
    private $max;
27
28
    /**
29
     * @var int
30
     */
31
    private $current = 0;
32
33
    /**
34
     * @param Output $output
35
     * @param int $max
36
     */
37
    public function __construct(Output $output, $max)
38
    {
39
        $this->output = $output;
40
        $this->max = $max;
41
    }
42
43
    /**
44
     * Start progress bar
45
     */
46
    public function start()
47
    {
48
        $this->current = 0;
49
    }
50
51
    /**
52
     * Advance progress bar
53
     */
54
    public function advance()
55
    {
56
        $this->current++;
57
58
        if ($this->output->hasAnsi()) {
59
            $percent = round($this->current / $this->max * 100);
60
            $this->output->write("\x0D");
61
            $this->output->write("\x1B[2K");
62
            $this->output->write(sprintf('... %s%% ...', $percent));
63
        } else {
64
            $this->output->write('.');
65
        }
66
    }
67
68
    /**
69
     * Clear console
70
     */
71
    public function clear()
72
    {
73
        if ($this->output->hasAnsi()) {
74
            $this->output->write("\x0D");
75
            $this->output->write("\x1B[2K");
76
            $this->output->clearln();
77
        }
78
    }
79
}
80