Passed
Push — master ( fabe0e...4e2ef4 )
by Alec
09:27
created

Spinner::update()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 3.072

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 4
c 2
b 0
f 0
dl 0
loc 7
ccs 4
cts 5
cp 0.8
rs 10
cc 3
nc 4
nop 0
crap 3.072
1
<?php
2
3
declare(strict_types=1);
4
5
namespace AlecRabbit\Snake;
6
7
use AlecRabbit\Snake\Contracts\Color;
8
use AlecRabbit\Snake\Core\Driver;
9
10
class Spinner
11
{
12
    private const CHARS = ['⠏', '⠛', '⠹', '⢸', '⣰', '⣤', '⣆', '⡇'];
13
    private const COLORS = [
14
        196,
15
        196,
16
        202,
17
        202,
18
        208,
19
        208,
20
        214,
21
        214,
22
        220,
23
        220,
24
        226,
25
        226,
26
        190,
27
        190,
28
        154,
29
        154,
30
        118,
31
        118,
32
        82,
33
        82,
34
        46,
35
        46,
36
        47,
37
        47,
38
        48,
39
        48,
40
        49,
41
        49,
42
        50,
43
        50,
44
        51,
45
        51,
46
        45,
47
        45,
48
        39,
49
        39,
50
        33,
51
        33,
52
        27,
53
        27,
54
        56,
55
        56,
56
        57,
57
        57,
58
        93,
59
        93,
60
        129,
61
        129,
62
        165,
63
        165,
64
        201,
65
        201,
66
        200,
67
        200,
68
        199,
69
        199,
70
        198,
71
        198,
72
        197,
73
        197,
74
    ];
75
76
    /** @var Driver */
77
    private $driver;
78
    /** @var int */
79
    private $currentCharIdx = 0;
80
    /** @var int */
81
    private $currentColorIdx = 0;
82
    /** @var int */
83
    private $framesCount;
84
    /** @var int */
85
    private $colorCount;
86
    /** @var bool */
87
    private $enabled;
88
89 5
    public function __construct(int $colorLevel = Color::COLOR_256)
90
    {
91 5
        $this->enabled = $colorLevel >= Color::NO_COLOR;
92 5
        $this->driver = new Driver($colorLevel);
93 3
        $this->framesCount = count(self::CHARS);
94 3
        $this->colorCount = count(self::COLORS);
95 3
    }
96
97 3
    public function spin(): void
98
    {
99 3
        if (!$this->enabled) {
100 2
            return;
101
        }
102 1
        $this->driver->write(
103 1
            $this->driver->eraseSequence(),
104 1
            $this->driver->frameSequence(
105 1
                self::COLORS[$this->currentColorIdx],
106 1
                self::CHARS[$this->currentCharIdx]
107
            ),
108 1
            $this->driver->moveBackSequence()
109
        );
110 1
        $this->update();
111 1
    }
112
113 1
    private function update(): void
114
    {
115 1
        if (++$this->currentCharIdx === $this->framesCount) {
116 1
            $this->currentCharIdx = 0;
117
        }
118 1
        if (++$this->currentColorIdx === $this->colorCount) {
119
            $this->currentColorIdx = 0;
120
        }
121 1
    }
122
123
    /**
124
     * Returns spinner refresh interval
125
     * @return float
126
     */
127 3
    public function interval(): float
128
    {
129 3
        return 0.1;
130
    }
131
132 3
    public function begin(): void
133
    {
134 3
        if (!$this->enabled) {
135 2
            return;
136
        }
137 1
        $this->driver->hideCursor();
138 1
    }
139
140 3
    public function end(): void
141
    {
142 3
        if (!$this->enabled) {
143 2
            return;
144
        }
145 1
        $this->erase();
146 1
        $this->driver->showCursor();
147 1
    }
148
149 1
    public function erase(): void
150
    {
151 1
        if (!$this->enabled) {
152
            return;
153
        }
154 1
        $this->driver->write(
155 1
            $this->driver->eraseSequence()
156
        );
157 1
    }
158
159
    public function useStdOut(): void
160
    {
161
        if (!$this->enabled) {
162
            return;
163
        }
164
        $this->driver->useStdOut();
165
//        $this->driver->disableStdErr();
166
    }
167
168 1
    public function disable(): void
169
    {
170 1
        $this->enabled = false;
171 1
    }
172
173
    /**
174
     * @return bool
175
     */
176 3
    public function isEnabled(): bool
177
    {
178 3
        return $this->enabled;
179
    }
180
}
181