1
|
|
|
<?php declare(strict_types=1); |
2
|
|
|
|
3
|
|
|
namespace AlecRabbit\Tools\Spinner\Core; |
4
|
|
|
|
5
|
|
|
use AlecRabbit\Accessories\Circular; |
6
|
|
|
use AlecRabbit\ConsoleColour\Terminal; |
7
|
|
|
use AlecRabbit\Tools\Contracts\Strings; |
8
|
|
|
use AlecRabbit\Tools\Spinner\Contracts\SpinnerInterface; |
9
|
|
|
|
10
|
|
|
abstract class AbstractSpinner implements SpinnerInterface |
11
|
|
|
{ |
12
|
|
|
protected const PADDING_STR = ' '; |
13
|
|
|
|
14
|
|
|
/** @var Circular */ |
15
|
|
|
protected $spinnerSymbols; |
16
|
|
|
/** @var null|Circular */ |
17
|
|
|
protected $styles; |
18
|
|
|
/** @var string */ |
19
|
|
|
protected $message; |
20
|
|
|
/** @var string */ |
21
|
|
|
protected $resetStr; |
22
|
|
|
/** @var \Closure */ |
23
|
|
|
protected $style; |
24
|
|
|
|
25
|
|
|
|
26
|
5 |
|
public function __construct( |
27
|
|
|
string $message = SpinnerInterface::DEFAULT_MESSAGE, |
28
|
|
|
string $prefix = SpinnerInterface::DEFAULT_PREFIX, |
29
|
|
|
string $suffix = SpinnerInterface::DEFAULT_SUFFIX |
30
|
|
|
) { |
31
|
5 |
|
$this->spinnerSymbols = $this->getSymbols(); |
32
|
5 |
|
$this->styles = $this->getStyles(); |
33
|
|
|
|
34
|
5 |
|
$this->message = $this->refineStr($message, $prefix, $suffix); |
35
|
5 |
|
$strLen = strlen($this->message . static::PADDING_STR) + 2; |
36
|
5 |
|
$this->resetStr = Strings::ESC . "[{$strLen}D"; |
37
|
5 |
|
$this->style = $this->getStyle(); |
38
|
5 |
|
} |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* @return Circular |
42
|
|
|
*/ |
43
|
|
|
abstract protected function getSymbols(): Circular; |
44
|
|
|
|
45
|
3 |
|
protected function getStyles(): ?Circular |
46
|
|
|
{ |
47
|
3 |
|
$terminal = new Terminal(); |
48
|
3 |
|
if ($terminal->supports256Color()) { |
49
|
|
|
return new Circular([ |
50
|
|
|
'38;5;197', |
51
|
|
|
'38;5;198', |
52
|
|
|
'38;5;199', |
53
|
|
|
'38;5;200', |
54
|
|
|
'38;5;201', |
55
|
|
|
'38;5;202', |
56
|
|
|
'38;5;203', |
57
|
|
|
'38;5;204', |
58
|
|
|
'38;5;205', |
59
|
|
|
]); |
60
|
|
|
} |
61
|
|
|
// @codeCoverageIgnoreStart |
62
|
|
|
if ($terminal->supportsColor()) { |
63
|
|
|
return new Circular([ |
64
|
|
|
'96', |
65
|
|
|
]); |
66
|
|
|
} |
67
|
|
|
return null; |
68
|
|
|
// @codeCoverageIgnoreEnd |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* @param string $str |
73
|
|
|
* @param string $prefix |
74
|
|
|
* @param string $suffix |
75
|
|
|
* @return string |
76
|
|
|
*/ |
77
|
5 |
|
protected function refineStr(string $str, string $prefix, string $suffix): string |
78
|
|
|
{ |
79
|
5 |
|
return $prefix . $str . $suffix; |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* @return \Closure |
84
|
|
|
*/ |
85
|
5 |
|
protected function getStyle(): \Closure |
86
|
|
|
{ |
87
|
5 |
|
if (null === $this->styles) { |
88
|
|
|
return |
89
|
|
|
function (): string { |
90
|
2 |
|
return static::PADDING_STR . $this->spinnerSymbols->value(); |
|
|
|
|
91
|
2 |
|
}; |
92
|
|
|
} |
93
|
|
|
return |
94
|
|
|
function (): string { |
95
|
|
|
return |
96
|
3 |
|
static::PADDING_STR . |
97
|
3 |
|
Strings::ESC . |
98
|
3 |
|
"[{$this->styles->value()}m{$this->spinnerSymbols->value()}" . |
99
|
3 |
|
Strings::ESC . '[0m'; |
100
|
3 |
|
}; |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
/** {@inheritDoc} */ |
104
|
5 |
|
public function begin(): string |
105
|
|
|
{ |
106
|
5 |
|
return $this->work() . $this->resetStr; |
107
|
|
|
} |
108
|
|
|
|
109
|
5 |
|
protected function work(): string |
110
|
|
|
{ |
111
|
5 |
|
return ($this->style)() . $this->message; |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
/** {@inheritDoc} */ |
115
|
5 |
|
public function spin(): string |
116
|
|
|
{ |
117
|
5 |
|
return $this->work() . $this->resetStr; |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
/** {@inheritDoc} */ |
121
|
5 |
|
public function end(): string |
122
|
|
|
{ |
123
|
5 |
|
return $this->resetStr . Strings::ESC . '[K'; |
124
|
|
|
} |
125
|
|
|
} |
126
|
|
|
|