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 $str; |
20
|
|
|
/** @var string */ |
21
|
|
|
protected $resetStr; |
22
|
|
|
/** @var \Closure */ |
23
|
|
|
protected $style; |
24
|
|
|
|
25
|
|
|
|
26
|
|
|
public function __construct(string $str = '', string $prefix = ' ', string $suffix = '...') |
27
|
|
|
{ |
28
|
|
|
$this->spinnerSymbols = $this->getSymbols(); |
29
|
|
|
$this->styles = $this->getStyles(); |
30
|
|
|
|
31
|
|
|
$this->str = $this->refineStr($str, $prefix, $suffix); |
32
|
|
|
$strLen = strlen($this->str . static::PADDING_STR) + 2; |
33
|
|
|
$this->resetStr = Strings::ESC . "[{$strLen}D"; |
34
|
|
|
$this->style = $this->getStyle(); |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* @return Circular |
39
|
|
|
*/ |
40
|
|
|
abstract protected function getSymbols(): Circular; |
41
|
|
|
|
42
|
|
|
protected function getStyles(): ?Circular |
43
|
|
|
{ |
44
|
|
|
$terminal = new Terminal(); |
45
|
|
|
if ($terminal->supports256Color()) { |
46
|
|
|
return new Circular([ |
47
|
|
|
'38;5;197', |
48
|
|
|
'38;5;198', |
49
|
|
|
'38;5;199', |
50
|
|
|
'38;5;200', |
51
|
|
|
'38;5;201', |
52
|
|
|
'38;5;202', |
53
|
|
|
'38;5;203', |
54
|
|
|
'38;5;204', |
55
|
|
|
'38;5;205', |
56
|
|
|
]); |
57
|
|
|
} |
58
|
|
|
if ($terminal->supportsColor()) { |
59
|
|
|
return new Circular([ |
60
|
|
|
'96', |
61
|
|
|
]); |
62
|
|
|
} |
63
|
|
|
return null; |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* @param string $str |
68
|
|
|
* @param string $prefix |
69
|
|
|
* @param string $suffix |
70
|
|
|
* @return string |
71
|
|
|
*/ |
72
|
|
|
protected function refineStr(string $str, string $prefix, string $suffix): string |
73
|
|
|
{ |
74
|
|
|
return $prefix . $str . $suffix; |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* @return \Closure |
79
|
|
|
*/ |
80
|
|
|
protected function getStyle(): \Closure |
81
|
|
|
{ |
82
|
|
|
if (null === $this->styles) { |
83
|
|
|
return |
84
|
|
|
function (): string { |
85
|
|
|
return static::PADDING_STR . $this->spinnerSymbols->value(); |
|
|
|
|
86
|
|
|
}; |
87
|
|
|
} else { |
88
|
|
|
return |
89
|
|
|
function (): string { |
90
|
|
|
return |
91
|
|
|
static::PADDING_STR . |
92
|
|
|
Strings::ESC . |
93
|
|
|
"[{$this->styles->value()}m{$this->spinnerSymbols->value()}" . |
|
|
|
|
94
|
|
|
Strings::ESC . '[0m'; |
95
|
|
|
}; |
96
|
|
|
} |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
/** {@inheritDoc} */ |
100
|
|
|
public function begin(): string |
101
|
|
|
{ |
102
|
|
|
return $this->work() . $this->resetStr; |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
protected function work(): string |
106
|
|
|
{ |
107
|
|
|
return ($this->style)() . $this->str; |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
/** {@inheritDoc} */ |
111
|
|
|
public function spin(): string |
112
|
|
|
{ |
113
|
|
|
return $this->work() . $this->resetStr; |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
/** {@inheritDoc} */ |
117
|
|
|
public function end(): string |
118
|
|
|
{ |
119
|
|
|
return $this->resetStr . Strings::ESC . '[K'; |
120
|
|
|
} |
121
|
|
|
} |
122
|
|
|
|