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 string */ |
18
|
|
|
protected $message; |
19
|
|
|
/** @var string */ |
20
|
|
|
protected $moveBackStr; |
21
|
|
|
/** @var string */ |
22
|
|
|
protected $paddingStr; |
23
|
|
|
/** @var string */ |
24
|
|
|
protected $eraseBySpacesStr; |
25
|
|
|
/** @var Styling */ |
26
|
|
|
protected $styled; |
27
|
|
|
|
28
|
|
|
|
29
|
10 |
|
public function __construct( |
30
|
|
|
?string $message = null, |
31
|
|
|
?string $prefix = null, |
32
|
|
|
?string $suffix = null, |
33
|
|
|
?string $paddingStr = null |
34
|
|
|
) { |
35
|
10 |
|
$this->spinnerSymbols = $this->getSymbols(); |
36
|
10 |
|
$this->paddingStr = $paddingStr ?? SpinnerInterface::PADDING_NEXT_LINE; |
37
|
10 |
|
$this->message = $this->refineMessage($message, $prefix, $suffix); |
38
|
10 |
|
$this->setFields(); |
39
|
10 |
|
$this->styled = new Styling($this->getSymbols(), $this->getStyles(), $this->message); |
40
|
10 |
|
} |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* @return Circular |
44
|
|
|
*/ |
45
|
|
|
abstract protected function getSymbols(): Circular; |
46
|
|
|
|
47
|
5 |
|
protected function getStyles(): array |
48
|
|
|
{ |
49
|
|
|
return [ |
50
|
5 |
|
Styling::COLOR256_SPINNER_STYLES => [ |
51
|
|
|
'203', |
52
|
|
|
'209', |
53
|
|
|
'215', |
54
|
|
|
'221', |
55
|
|
|
'227', |
56
|
|
|
'191', |
57
|
|
|
'155', |
58
|
|
|
'119', |
59
|
|
|
'83', |
60
|
|
|
'84', |
61
|
|
|
'85', |
62
|
|
|
'86', |
63
|
|
|
'87', |
64
|
|
|
'81', |
65
|
|
|
'75', |
66
|
|
|
'69', |
67
|
|
|
'63', |
68
|
|
|
'99', |
69
|
|
|
'135', |
70
|
|
|
'171', |
71
|
|
|
'207', |
72
|
|
|
'206', |
73
|
|
|
'205', |
74
|
|
|
'204', |
75
|
|
|
], |
76
|
5 |
|
Styling::COLOR_SPINNER_STYLES => ['96'], |
77
|
|
|
]; |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* @param null|string $message |
82
|
|
|
* @param null|string $prefix |
83
|
|
|
* @param null|string $suffix |
84
|
|
|
* @return string |
85
|
|
|
*/ |
86
|
10 |
|
protected function refineMessage(?string $message, ?string $prefix, ?string $suffix): string |
87
|
|
|
{ |
88
|
10 |
|
$message = ucfirst($message ?? SpinnerInterface::DEFAULT_MESSAGE); |
89
|
10 |
|
$prefix = $prefix ?? SpinnerInterface::DEFAULT_PREFIX; |
90
|
10 |
|
$suffix = $suffix ?? (empty($message) ? '' : SpinnerInterface::DEFAULT_SUFFIX); |
91
|
10 |
|
return $prefix . $message . $suffix; |
92
|
|
|
} |
93
|
|
|
|
94
|
10 |
|
protected function setFields(): void |
95
|
|
|
{ |
96
|
10 |
|
$strLen = strlen($this->message . $this->paddingStr) + static::ERASING_SHIFT; |
97
|
10 |
|
$this->moveBackStr = self::ESC . "[{$strLen}D"; |
98
|
10 |
|
$this->eraseBySpacesStr = str_repeat(' ', $strLen); |
99
|
10 |
|
} |
100
|
|
|
|
101
|
2 |
|
public function inline(bool $inline): SpinnerInterface |
102
|
|
|
{ |
103
|
2 |
|
$this->paddingStr = $inline ? SpinnerInterface::PADDING_INLINE : SpinnerInterface::PADDING_NEXT_LINE; |
104
|
2 |
|
$this->setFields(); |
105
|
|
|
|
106
|
2 |
|
return $this; |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
/** {@inheritDoc} */ |
110
|
9 |
|
public function begin(): string |
111
|
|
|
{ |
112
|
9 |
|
return $this->spin(); |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
/** {@inheritDoc} */ |
116
|
9 |
|
public function spin(): string |
117
|
|
|
{ |
118
|
9 |
|
return $this->paddingStr . $this->styled->spinner() . $this->styled->message() . $this->moveBackStr; |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
/** {@inheritDoc} */ |
122
|
9 |
|
public function end(): string |
123
|
|
|
{ |
124
|
9 |
|
return $this->erase(); |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
/** {@inheritDoc} */ |
128
|
9 |
|
public function erase(): string |
129
|
|
|
{ |
130
|
9 |
|
return $this->eraseBySpacesStr . $this->moveBackStr; |
131
|
|
|
} |
132
|
|
|
} |
133
|
|
|
|