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 function AlecRabbit\typeOf; |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* Class Styling |
12
|
|
|
*/ |
13
|
|
|
class Styling |
14
|
|
|
{ |
15
|
|
|
public const COLOR256_SPINNER_STYLES = '256_color_spinner_styles'; |
16
|
|
|
public const COLOR_SPINNER_STYLES = 'color_spinner_styles'; |
17
|
|
|
|
18
|
|
|
public const COLOR_MESSAGE_STYLES = 'color_message_styles'; |
19
|
|
|
public const DEFAULT_MESSAGE_STYLES = [2]; |
20
|
|
|
|
21
|
|
|
public const MAX_SYMBOLS_COUNT = 50; |
22
|
|
|
|
23
|
|
|
/** @var Circular */ |
24
|
|
|
protected $symbolStyles; |
25
|
|
|
/** @var Circular */ |
26
|
|
|
protected $messageStyles; |
27
|
|
|
/** @var Circular */ |
28
|
|
|
private $symbols; |
29
|
9 |
|
|
30
|
|
|
public function __construct(array $symbols, array $styles) |
31
|
9 |
|
{ |
32
|
9 |
|
$this->assertSymbols($symbols); |
33
|
9 |
|
$this->assertStyles($styles); |
34
|
9 |
|
$this->symbols = new Circular($symbols); |
35
|
9 |
|
$this->symbolStyles = $this->symbolStyles($styles); |
36
|
9 |
|
$this->messageStyles = $this->messageStyles($styles); |
37
|
|
|
} |
38
|
9 |
|
|
39
|
|
|
protected function assertSymbols(array $symbols): void |
40
|
9 |
|
{ |
41
|
|
|
if (self::MAX_SYMBOLS_COUNT < count($symbols)) { |
42
|
|
|
throw new \InvalidArgumentException('Symbols array is too big.'); |
43
|
9 |
|
} |
44
|
|
|
} |
45
|
9 |
|
|
46
|
|
|
protected function assertStyles(array $styles): void |
47
|
9 |
|
{ |
48
|
|
|
if (!\array_key_exists(self::COLOR256_SPINNER_STYLES, $styles)) { |
49
|
|
|
throw new \InvalidArgumentException($this->errorMsg('Styles array does not have', 'COLOR256_STYLES')); |
50
|
9 |
|
} |
51
|
|
|
$value = $styles[self::COLOR256_SPINNER_STYLES]; |
52
|
|
|
if (!\is_array($value) && null !== $value) { |
53
|
9 |
|
throw new \InvalidArgumentException($this->errorMsg('Styles should be type of array or NULL in', 'COLOR256_STYLES')); |
54
|
|
|
} |
55
|
|
|
if (!\array_key_exists(self::COLOR_SPINNER_STYLES, $styles)) { |
56
|
|
|
throw new \InvalidArgumentException($this->errorMsg('Styles array does not have', 'COLOR_STYLES')); |
57
|
|
|
} |
58
|
|
|
$value = $styles[self::COLOR_SPINNER_STYLES]; |
59
|
|
|
if (!is_array($value) && null !== $value) { |
60
|
|
|
throw new \InvalidArgumentException($this->errorMsg('Styles should be type of array or NULL in', 'COLOR_SPINNER_STYLES')); |
61
|
|
|
} |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* @param string $str |
66
|
|
|
* @param string $constant |
67
|
|
|
* @return string |
68
|
9 |
|
*/ |
69
|
|
|
private function errorMsg(string $str, string $constant): string |
70
|
9 |
|
{ |
71
|
|
|
return $str . ' ' . $constant . ' key.'; |
72
|
|
|
} |
73
|
9 |
|
|
74
|
9 |
|
/** |
75
|
|
|
* @param array $styles |
76
|
|
|
* @return Circular |
77
|
|
|
*/ |
78
|
|
|
protected function symbolStyles(array $styles): Circular |
79
|
|
|
{ |
80
|
|
|
$value = $styles[self::COLOR256_SPINNER_STYLES]; |
81
|
|
|
/** @noinspection NotOptimalIfConditionsInspection */ |
82
|
|
|
if (($terminal = new Terminal())->supports256Color() && null !== $value) { |
83
|
|
|
return $this->circular256Color($value); |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
$value = $styles[self::COLOR_SPINNER_STYLES]; |
87
|
|
|
/** @noinspection NotOptimalIfConditionsInspection */ |
88
|
|
|
if ($terminal->supportsColor() && null !== $value) { |
89
|
|
|
return $this->circularColor($value); |
90
|
|
|
} |
91
|
|
|
return $this->circularNoColor(); |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
protected function circular256Color(array $styles): Circular |
95
|
9 |
|
{ |
96
|
|
|
return |
97
|
9 |
|
new Circular( |
98
|
3 |
|
array_map( |
99
|
|
|
static function (string $value): string { |
100
|
|
|
return ConsoleColor::ESC_CHAR . "[38;5;{$value}m%s" . ConsoleColor::ESC_CHAR . '[0m'; |
101
|
6 |
|
}, |
102
|
6 |
|
$styles |
103
|
|
|
) |
104
|
6 |
|
); |
105
|
6 |
|
} |
106
|
6 |
|
|
107
|
|
|
protected function circularColor(array $styles): Circular |
108
|
|
|
{ |
109
|
|
|
return |
110
|
|
|
new Circular( |
111
|
3 |
|
array_map( |
112
|
|
|
static function (string $value): string { |
113
|
3 |
|
return ConsoleColor::ESC_CHAR . "[{$value}m%s" . ConsoleColor::ESC_CHAR . '[0m'; |
114
|
|
|
}, |
115
|
|
|
$styles |
116
|
8 |
|
) |
117
|
|
|
); |
118
|
8 |
|
} |
119
|
|
|
|
120
|
|
|
protected function circularNoColor(): Circular |
121
|
8 |
|
{ |
122
|
|
|
return new Circular(['%s',]); |
123
|
8 |
|
} |
124
|
|
|
|
125
|
|
|
public function spinner(): string |
126
|
|
|
{ |
127
|
|
|
return sprintf((string)$this->symbolStyles->value(), (string)$this->symbols->value()); |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
public function message(string $message): string |
131
|
|
|
{ |
132
|
|
|
return |
133
|
|
|
sprintf( |
134
|
|
|
(string)$this->messageStyles->value(), |
135
|
|
|
$message |
136
|
|
|
); |
137
|
|
|
// return |
138
|
|
|
// sprintf( |
139
|
|
|
// ConsoleColor::ESC_CHAR . '[2m%s' . ConsoleColor::ESC_CHAR . '[0m', |
140
|
|
|
// $message |
141
|
|
|
// ); |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
/** |
145
|
|
|
* @param array $styles |
146
|
|
|
* @return Circular |
147
|
|
|
*/ |
148
|
|
|
protected function messageStyles(array $styles): Circular |
149
|
|
|
{ |
150
|
|
|
if (!\array_key_exists(self::COLOR_MESSAGE_STYLES, $styles)) { |
151
|
|
|
$styles[self::COLOR_MESSAGE_STYLES] = self::DEFAULT_MESSAGE_STYLES; |
152
|
|
|
} |
153
|
|
|
if ((new Terminal())->supportsColor()) { |
154
|
|
|
$value = $styles[self::COLOR_MESSAGE_STYLES]; |
155
|
|
|
if (null === $value) { |
156
|
|
|
return $this->circularNoColor(); |
157
|
|
|
} |
158
|
|
|
return $this->circularColor($value); |
159
|
|
|
} |
160
|
|
|
return $this->circularNoColor(); |
161
|
|
|
} |
162
|
|
|
} |
163
|
|
|
|