|
1
|
|
|
<?php declare(strict_types=1); |
|
2
|
|
|
|
|
3
|
|
|
namespace AlecRabbit\Spinner\Core; |
|
4
|
|
|
|
|
5
|
|
|
use AlecRabbit\Accessories\Circular; |
|
6
|
|
|
use AlecRabbit\ConsoleColour\ConsoleColor; |
|
7
|
|
|
use AlecRabbit\ConsoleColour\Terminal; |
|
8
|
|
|
use AlecRabbit\Spinner\Contracts\SpinnerInterface; |
|
9
|
|
|
|
|
10
|
|
|
abstract class AbstractSpinner implements SpinnerInterface |
|
11
|
|
|
{ |
|
12
|
|
|
protected const ESC = ConsoleColor::ESC_CHAR; |
|
13
|
|
|
protected const ERASING_SHIFT = 1; |
|
14
|
|
|
|
|
15
|
|
|
/** @var Circular */ |
|
16
|
|
|
protected $spinnerSymbols; |
|
17
|
|
|
/** @var null|Circular */ |
|
18
|
|
|
protected $styles; |
|
19
|
|
|
/** @var string */ |
|
20
|
|
|
protected $message; |
|
21
|
|
|
/** @var string */ |
|
22
|
|
|
protected $moveBackStr; |
|
23
|
|
|
/** @var \Closure */ |
|
24
|
|
|
protected $style; |
|
25
|
|
|
/** @var string */ |
|
26
|
|
|
protected $paddingStr; |
|
27
|
|
|
/** @var string */ |
|
28
|
|
|
protected $eraseBySpacesStr; |
|
29
|
|
|
|
|
30
|
|
|
|
|
31
|
9 |
|
public function __construct( |
|
32
|
|
|
?string $message = null, |
|
33
|
|
|
?string $prefix = null, |
|
34
|
|
|
?string $suffix = null, |
|
35
|
|
|
?string $paddingStr = null |
|
36
|
|
|
) { |
|
37
|
9 |
|
$this->spinnerSymbols = $this->getSymbols(); |
|
38
|
9 |
|
$this->styles = $this->getStyles(); |
|
39
|
9 |
|
$this->paddingStr = $paddingStr ?? SpinnerInterface::PADDING_NEXT_LINE; |
|
40
|
9 |
|
$this->message = $this->refineMessage($message, $prefix, $suffix); |
|
41
|
9 |
|
$this->setFields(); |
|
42
|
9 |
|
$this->style = $this->getStyle(); |
|
43
|
9 |
|
} |
|
44
|
|
|
|
|
45
|
|
|
/** |
|
46
|
|
|
* @return Circular |
|
47
|
|
|
*/ |
|
48
|
|
|
abstract protected function getSymbols(): Circular; |
|
49
|
|
|
|
|
50
|
5 |
|
protected function getStyles(): ?Circular |
|
51
|
|
|
{ |
|
52
|
5 |
|
$terminal = new Terminal(); |
|
53
|
5 |
|
if ($terminal->supports256Color()) { |
|
54
|
|
|
$a = [ |
|
55
|
|
|
'196', |
|
56
|
|
|
'202', |
|
57
|
|
|
'208', |
|
58
|
|
|
'214', |
|
59
|
|
|
'220', |
|
60
|
|
|
'226', |
|
61
|
|
|
'190', |
|
62
|
|
|
'154', |
|
63
|
|
|
'118', |
|
64
|
|
|
'82', |
|
65
|
|
|
'46', |
|
66
|
|
|
'47', |
|
67
|
|
|
'48', |
|
68
|
|
|
'49', |
|
69
|
|
|
'50', |
|
70
|
|
|
'51', |
|
71
|
|
|
'45', |
|
72
|
|
|
'39', |
|
73
|
|
|
'33', |
|
74
|
|
|
'27', |
|
75
|
|
|
'21', |
|
76
|
|
|
'57', |
|
77
|
|
|
'93', |
|
78
|
|
|
'129', |
|
79
|
|
|
'165', |
|
80
|
|
|
'201', |
|
81
|
|
|
'200', |
|
82
|
|
|
'199', |
|
83
|
|
|
'198', |
|
84
|
|
|
'197', |
|
85
|
|
|
]; |
|
86
|
|
|
return |
|
87
|
|
|
new Circular( |
|
88
|
|
|
array_map( |
|
89
|
|
|
static function ($value) { |
|
90
|
|
|
return '38;5;' . $value; |
|
91
|
|
|
}, |
|
92
|
|
|
$a |
|
93
|
|
|
) |
|
94
|
|
|
); |
|
95
|
|
|
} |
|
96
|
5 |
|
if ($terminal->supportsColor()) { |
|
97
|
5 |
|
return new Circular([ |
|
98
|
5 |
|
'96', |
|
99
|
|
|
]); |
|
100
|
|
|
} |
|
101
|
|
|
return null; |
|
102
|
|
|
} |
|
103
|
|
|
|
|
104
|
|
|
/** |
|
105
|
|
|
* @param null|string $message |
|
106
|
|
|
* @param null|string $prefix |
|
107
|
|
|
* @param null|string $suffix |
|
108
|
|
|
* @return string |
|
109
|
|
|
*/ |
|
110
|
9 |
|
protected function refineMessage(?string $message, ?string $prefix, ?string $suffix): string |
|
111
|
|
|
{ |
|
112
|
9 |
|
$message = ucfirst($message ?? SpinnerInterface::DEFAULT_MESSAGE); |
|
113
|
9 |
|
$prefix = $prefix ?? SpinnerInterface::DEFAULT_PREFIX; |
|
114
|
9 |
|
$suffix = $suffix ?? (empty($message) ? '' : SpinnerInterface::DEFAULT_SUFFIX); |
|
115
|
9 |
|
return $prefix . $message . $suffix; |
|
116
|
|
|
} |
|
117
|
|
|
|
|
118
|
9 |
|
protected function setFields(): void |
|
119
|
|
|
{ |
|
120
|
9 |
|
$strLen = strlen($this->message . $this->paddingStr) + static::ERASING_SHIFT; |
|
121
|
9 |
|
$this->moveBackStr = self::ESC . "[{$strLen}D"; |
|
122
|
9 |
|
$this->eraseBySpacesStr = str_repeat(' ', $strLen); |
|
123
|
9 |
|
} |
|
124
|
|
|
|
|
125
|
|
|
/** |
|
126
|
|
|
* @return \Closure |
|
127
|
|
|
*/ |
|
128
|
9 |
|
protected function getStyle(): \Closure |
|
129
|
|
|
{ |
|
130
|
9 |
|
if (null === $this->styles) { |
|
131
|
|
|
return |
|
132
|
|
|
function (): string { |
|
133
|
2 |
|
$value = (string)$this->spinnerSymbols->value(); |
|
134
|
2 |
|
return $this->paddingStr . $value; |
|
135
|
2 |
|
}; |
|
136
|
|
|
} |
|
137
|
|
|
return |
|
138
|
|
|
function (): string { |
|
139
|
6 |
|
$symbol = (string)$this->spinnerSymbols->value(); |
|
140
|
6 |
|
$style = $this->styles ? (string)$this->styles->value() : ''; |
|
141
|
|
|
return |
|
142
|
6 |
|
$this->paddingStr . |
|
143
|
6 |
|
self::ESC . |
|
144
|
6 |
|
"[{$style}m{$symbol}" . |
|
145
|
6 |
|
self::ESC . '[0m'; |
|
146
|
7 |
|
}; |
|
147
|
|
|
} |
|
148
|
|
|
|
|
149
|
1 |
|
public function inline(bool $inline): SpinnerInterface |
|
150
|
|
|
{ |
|
151
|
1 |
|
$this->paddingStr = $inline ? SpinnerInterface::PADDING_INLINE : SpinnerInterface::PADDING_NEXT_LINE; |
|
152
|
1 |
|
$this->setFields(); |
|
153
|
|
|
|
|
154
|
1 |
|
return $this; |
|
155
|
|
|
} |
|
156
|
|
|
|
|
157
|
|
|
/** {@inheritDoc} */ |
|
158
|
8 |
|
public function begin(): string |
|
159
|
|
|
{ |
|
160
|
8 |
|
return $this->spin(); |
|
161
|
|
|
} |
|
162
|
|
|
|
|
163
|
|
|
/** {@inheritDoc} */ |
|
164
|
8 |
|
public function spin(): string |
|
165
|
|
|
{ |
|
166
|
8 |
|
return $this->work() . $this->moveBackStr; |
|
167
|
|
|
} |
|
168
|
|
|
|
|
169
|
8 |
|
protected function work(): string |
|
170
|
|
|
{ |
|
171
|
8 |
|
return ($this->style)() . $this->message; |
|
172
|
|
|
} |
|
173
|
|
|
|
|
174
|
|
|
/** {@inheritDoc} */ |
|
175
|
8 |
|
public function end(): string |
|
176
|
|
|
{ |
|
177
|
8 |
|
return $this->erase(); |
|
178
|
|
|
// return self::ESC . '[K'; |
|
179
|
|
|
} |
|
180
|
|
|
|
|
181
|
|
|
/** {@inheritDoc} */ |
|
182
|
8 |
|
public function erase(): string |
|
183
|
|
|
{ |
|
184
|
8 |
|
return $this->eraseBySpacesStr . $this->moveBackStr; |
|
185
|
|
|
} |
|
186
|
|
|
} |
|
187
|
|
|
|