|
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
|
|
|
|
|
9
|
|
|
/** |
|
10
|
|
|
* Class Styling |
|
11
|
|
|
*/ |
|
12
|
|
|
class Styling |
|
13
|
|
|
{ |
|
14
|
|
|
public const COLOR256_SPINNER_STYLES = '256_color_spinner_styles'; |
|
15
|
|
|
public const COLOR_SPINNER_STYLES = 'color_spinner_styles'; |
|
16
|
|
|
|
|
17
|
|
|
public const COLOR256_MESSAGE_STYLES = '256_color_message_styles'; |
|
18
|
|
|
public const COLOR_MESSAGE_STYLES = 'color_message_styles'; |
|
19
|
|
|
|
|
20
|
|
|
/** @var Circular */ |
|
21
|
|
|
protected $styles; |
|
22
|
|
|
/** @var string */ |
|
23
|
|
|
private $message; |
|
24
|
|
|
/** @var Circular */ |
|
25
|
|
|
private $symbols; |
|
26
|
|
|
|
|
27
|
10 |
|
public function __construct(Circular $symbols, array $styles, string $message) |
|
28
|
|
|
{ |
|
29
|
10 |
|
$this->symbols = $symbols; |
|
30
|
10 |
|
$this->message = $message; |
|
31
|
10 |
|
$this->assertStyles($styles); |
|
32
|
10 |
|
$this->styles = $this->makeStyles($styles); |
|
33
|
10 |
|
} |
|
34
|
|
|
|
|
35
|
10 |
|
protected function assertStyles(array $styles): void |
|
36
|
|
|
{ |
|
37
|
10 |
|
if (!\array_key_exists(self::COLOR256_SPINNER_STYLES, $styles)) { |
|
38
|
|
|
throw new \InvalidArgumentException($this->errorMsg('COLOR256_STYLES')); |
|
39
|
|
|
} |
|
40
|
10 |
|
if (!\array_key_exists(self::COLOR_SPINNER_STYLES, $styles)) { |
|
41
|
|
|
throw new \InvalidArgumentException($this->errorMsg('COLOR_STYLES')); |
|
42
|
|
|
} |
|
43
|
10 |
|
} |
|
44
|
|
|
|
|
45
|
|
|
/** |
|
46
|
|
|
* @param string $constant |
|
47
|
|
|
* @return string |
|
48
|
|
|
*/ |
|
49
|
|
|
private function errorMsg(string $constant): string |
|
50
|
|
|
{ |
|
51
|
|
|
return 'Styles array does not have ' . static::class . '::' . $constant . 'key'; |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
/** |
|
55
|
|
|
* @param array $styles |
|
56
|
|
|
* @return Circular |
|
57
|
|
|
*/ |
|
58
|
10 |
|
protected function makeStyles(array $styles): Circular |
|
59
|
|
|
{ |
|
60
|
10 |
|
if (($terminal = new Terminal())->supports256Color()) { |
|
61
|
|
|
return $this->circular256Color($styles); |
|
62
|
|
|
} |
|
63
|
10 |
|
if ($terminal->supportsColor()) { |
|
64
|
10 |
|
return $this->circularColor($styles); |
|
65
|
|
|
} |
|
66
|
|
|
return $this->circularNoColor(); |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
protected function circular256Color(array $styles): Circular |
|
70
|
|
|
{ |
|
71
|
|
|
if (null === $value = $styles[self::COLOR256_SPINNER_STYLES]) { |
|
72
|
|
|
return $this->circularColor($styles); |
|
73
|
|
|
} |
|
74
|
|
|
return |
|
75
|
|
|
new Circular( |
|
76
|
|
|
array_map( |
|
77
|
|
|
static function (string $value): string { |
|
78
|
|
|
return ConsoleColor::ESC_CHAR . "[38;5;{$value}m%s" . ConsoleColor::ESC_CHAR . '[0m'; |
|
79
|
|
|
}, |
|
80
|
|
|
$value |
|
81
|
|
|
) |
|
82
|
|
|
); |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
10 |
|
protected function circularColor(array $styles): Circular |
|
86
|
|
|
{ |
|
87
|
10 |
|
if (null === $value = $styles[self::COLOR_SPINNER_STYLES]) { |
|
88
|
3 |
|
return $this->circularNoColor(); |
|
89
|
|
|
} |
|
90
|
|
|
return |
|
91
|
7 |
|
new Circular( |
|
92
|
7 |
|
array_map( |
|
93
|
|
|
static function (string $value): string { |
|
94
|
7 |
|
return ConsoleColor::ESC_CHAR . "[{$value}m%s" . ConsoleColor::ESC_CHAR . '[0m'; |
|
95
|
7 |
|
}, |
|
96
|
7 |
|
$value |
|
97
|
|
|
) |
|
98
|
|
|
); |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
3 |
|
protected function circularNoColor(): Circular |
|
102
|
|
|
{ |
|
103
|
3 |
|
return new Circular(['%s',]); |
|
104
|
|
|
} |
|
105
|
|
|
|
|
106
|
9 |
|
public function spinner(): string |
|
107
|
|
|
{ |
|
108
|
9 |
|
return sprintf((string)$this->styles->value(), (string)$this->symbols->value()); |
|
109
|
|
|
} |
|
110
|
|
|
|
|
111
|
9 |
|
public function message(): string |
|
112
|
|
|
{ |
|
113
|
9 |
|
return $this->message; |
|
114
|
|
|
} |
|
115
|
|
|
} |
|
116
|
|
|
|