|
1
|
|
|
<?php declare(strict_types=1); |
|
2
|
|
|
|
|
3
|
|
|
namespace AlecRabbit\Spinner\Core; |
|
4
|
|
|
|
|
5
|
|
|
use AlecRabbit\Accessories\Pretty; |
|
6
|
|
|
use AlecRabbit\ConsoleColour\ConsoleColor; |
|
7
|
|
|
use AlecRabbit\Spinner\Contracts\SpinnerInterface; |
|
8
|
|
|
|
|
9
|
|
|
abstract class AbstractSpinner implements SpinnerInterface |
|
10
|
|
|
{ |
|
11
|
|
|
protected const ERASING_SHIFT = 1; |
|
12
|
|
|
|
|
13
|
|
|
/** @var string */ |
|
14
|
|
|
protected $messageStr; |
|
15
|
|
|
/** @var string */ |
|
16
|
|
|
protected $percentStr = ''; |
|
17
|
|
|
/** @var string */ |
|
18
|
|
|
protected $moveBackSequenceStr; |
|
19
|
|
|
/** @var string */ |
|
20
|
|
|
protected $paddingStr; |
|
21
|
|
|
/** @var string */ |
|
22
|
|
|
protected $eraseBySpacesStr; |
|
23
|
|
|
/** @var Styling */ |
|
24
|
|
|
protected $styled; |
|
25
|
|
|
|
|
26
|
9 |
|
public function __construct( |
|
27
|
|
|
?string $message = null, |
|
28
|
|
|
?string $prefix = null, |
|
29
|
|
|
?string $suffix = null, |
|
30
|
|
|
?string $paddingStr = null |
|
31
|
|
|
) { |
|
32
|
9 |
|
$this->paddingStr = $paddingStr ?? SpinnerInterface::PADDING_EMPTY; |
|
33
|
9 |
|
$this->messageStr = $this->refineMessage($message, $prefix, $suffix); |
|
34
|
9 |
|
$this->setFields(); |
|
35
|
9 |
|
$this->styled = new Styling($this->getSymbols(), $this->getStyles(), $this->messageStr); |
|
36
|
9 |
|
} |
|
37
|
|
|
|
|
38
|
|
|
/** |
|
39
|
|
|
* @param null|string $message |
|
40
|
|
|
* @param null|string $prefix |
|
41
|
|
|
* @param null|string $suffix |
|
42
|
|
|
* @return string |
|
43
|
|
|
*/ |
|
44
|
9 |
|
protected function refineMessage(?string $message, ?string $prefix, ?string $suffix): string |
|
45
|
|
|
{ |
|
46
|
9 |
|
$message = ucfirst($message ?? SpinnerInterface::DEFAULT_MESSAGE); |
|
47
|
9 |
|
$prefix = empty($message) ? '' : $prefix ?? SpinnerInterface::DEFAULT_PREFIX; |
|
48
|
9 |
|
$suffix = $suffix ?? (empty($message) ? '' : SpinnerInterface::DEFAULT_SUFFIX); |
|
49
|
9 |
|
return $prefix . $message . $suffix; |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
9 |
|
protected function setFields(): void |
|
53
|
|
|
{ |
|
54
|
9 |
|
$strLen = strlen($this->message()) + strlen($this->paddingStr) + static::ERASING_SHIFT; |
|
55
|
9 |
|
$this->moveBackSequenceStr = ConsoleColor::ESC_CHAR . "[{$strLen}D"; |
|
56
|
9 |
|
$this->eraseBySpacesStr = str_repeat(' ', $strLen); |
|
57
|
9 |
|
} |
|
58
|
|
|
|
|
59
|
|
|
/** |
|
60
|
|
|
* @return string |
|
61
|
|
|
*/ |
|
62
|
9 |
|
protected function message(): string |
|
63
|
|
|
{ |
|
64
|
9 |
|
return $this->messageStr . $this->percentStr; |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
/** |
|
68
|
|
|
* @return array |
|
69
|
|
|
*/ |
|
70
|
|
|
abstract protected function getSymbols(): array; |
|
71
|
|
|
|
|
72
|
4 |
|
protected function getStyles(): array |
|
73
|
|
|
{ |
|
74
|
|
|
return [ |
|
75
|
4 |
|
Styling::COLOR256_SPINNER_STYLES => [ |
|
76
|
|
|
'203', |
|
77
|
|
|
'209', |
|
78
|
|
|
'215', |
|
79
|
|
|
'221', |
|
80
|
|
|
'227', |
|
81
|
|
|
'191', |
|
82
|
|
|
'155', |
|
83
|
|
|
'119', |
|
84
|
|
|
'83', |
|
85
|
|
|
'84', |
|
86
|
|
|
'85', |
|
87
|
|
|
'86', |
|
88
|
|
|
'87', |
|
89
|
|
|
'81', |
|
90
|
|
|
'75', |
|
91
|
|
|
'69', |
|
92
|
|
|
'63', |
|
93
|
|
|
'99', |
|
94
|
|
|
'135', |
|
95
|
|
|
'171', |
|
96
|
|
|
'207', |
|
97
|
|
|
'206', |
|
98
|
|
|
'205', |
|
99
|
|
|
'204', |
|
100
|
|
|
], |
|
101
|
4 |
|
Styling::COLOR_SPINNER_STYLES => ['96'], |
|
102
|
|
|
]; |
|
103
|
|
|
} |
|
104
|
|
|
|
|
105
|
2 |
|
public function inline(bool $inline): SpinnerInterface |
|
106
|
|
|
{ |
|
107
|
2 |
|
$this->paddingStr = $inline ? SpinnerInterface::PADDING_SPACE_SYMBOL : SpinnerInterface::PADDING_EMPTY; |
|
108
|
2 |
|
$this->setFields(); |
|
109
|
2 |
|
return $this; |
|
110
|
|
|
} |
|
111
|
|
|
|
|
112
|
|
|
/** {@inheritDoc} */ |
|
113
|
8 |
|
public function begin(): string |
|
114
|
|
|
{ |
|
115
|
8 |
|
return $this->spin(); |
|
116
|
|
|
} |
|
117
|
|
|
|
|
118
|
|
|
/** {@inheritDoc} */ |
|
119
|
8 |
|
public function spin(?float $percent = null): string |
|
120
|
|
|
{ |
|
121
|
8 |
|
if (null !== $percent) { |
|
122
|
|
|
$this->updatePercent($percent); |
|
123
|
|
|
} |
|
124
|
|
|
return |
|
125
|
8 |
|
$this->paddingStr . |
|
126
|
8 |
|
$this->styled->spinner() . |
|
127
|
8 |
|
$this->styled->message( |
|
128
|
8 |
|
$this->message() |
|
129
|
|
|
) . |
|
130
|
8 |
|
$this->moveBackSequenceStr; |
|
131
|
|
|
} |
|
132
|
|
|
|
|
133
|
|
|
/** |
|
134
|
|
|
* @param float $percent |
|
135
|
|
|
*/ |
|
136
|
|
|
protected function updatePercent(float $percent): void |
|
137
|
|
|
{ |
|
138
|
|
|
if (0 === (int)($percent * 1000) % 10) { |
|
139
|
|
|
$this->percentStr = Pretty::percent($percent, 0, ' '); |
|
140
|
|
|
$this->setFields(); |
|
141
|
|
|
} |
|
142
|
|
|
} |
|
143
|
|
|
|
|
144
|
|
|
/** {@inheritDoc} */ |
|
145
|
8 |
|
public function end(): string |
|
146
|
|
|
{ |
|
147
|
8 |
|
return $this->erase(); |
|
148
|
|
|
} |
|
149
|
|
|
|
|
150
|
|
|
/** {@inheritDoc} */ |
|
151
|
8 |
|
public function erase(): string |
|
152
|
|
|
{ |
|
153
|
8 |
|
return $this->eraseBySpacesStr . $this->moveBackSequenceStr; |
|
154
|
|
|
} |
|
155
|
|
|
} |
|
156
|
|
|
|