Passed
Push — master ( d93186...fdbeea )
by Alec
01:52
created

Spinner::interval()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 1
c 1
b 0
f 0
dl 0
loc 3
ccs 2
cts 2
cp 1
rs 10
cc 1
nc 1
nop 0
crap 1
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
87 1
    public function __construct(int $colorLevel = Color::COLOR_256)
88
    {
89 1
        $this->driver = new Driver($colorLevel);
90 1
        $this->framesCount = count(self::CHARS);
91 1
        $this->colorCount = count(self::COLORS);
92 1
    }
93
94 1
    public function spin(): void
95
    {
96 1
        $this->driver->write(
97 1
            $this->driver->eraseSequence(),
98 1
            $this->driver->frameSequence(
99 1
                self::COLORS[$this->currentColorIdx],
100 1
                self::CHARS[$this->currentCharIdx]
101
            ),
102 1
            $this->driver->moveBackSequence()
103
        );
104 1
        $this->update();
105 1
    }
106
107 1
    public function erase(): void
108
    {
109 1
        $this->driver->write(
110 1
            $this->driver->eraseSequence()
111
        );
112 1
    }
113
114 1
    private function update(): void
115
    {
116 1
        if (++$this->currentCharIdx === $this->framesCount) {
117 1
            $this->currentCharIdx = 0;
118
        }
119 1
        if (++$this->currentColorIdx === $this->colorCount) {
120
            $this->currentColorIdx = 0;
121
        }
122 1
    }
123
124
    /**
125
     * Returns spinner refresh interval
126
     * @return float
127
     */
128 1
    public function interval(): float
129
    {
130 1
        return 0.1;
131
    }
132
133 1
    public function begin(): void
134
    {
135 1
        $this->driver->hideCursor();
136 1
    }
137
138 1
    public function end(): void
139
    {
140 1
        $this->erase();
141 1
        $this->driver->showCursor();
142 1
    }
143
144
    public function useStdOut(): void
145
    {
146
        $this->driver->disableStdErr();
147
    }
148
}
149