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