|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace PhpSchool\CliMenu\Style; |
|
4
|
|
|
|
|
5
|
|
|
use PhpSchool\CliMenu\Util\ColourUtil; |
|
6
|
|
|
use PhpSchool\Terminal\Terminal; |
|
7
|
|
|
|
|
8
|
|
|
trait ItemStyleTrait |
|
9
|
|
|
{ |
|
10
|
|
|
/** |
|
11
|
|
|
* @var Terminal |
|
12
|
|
|
*/ |
|
13
|
|
|
protected $terminal; |
|
14
|
|
|
|
|
15
|
|
|
/** |
|
16
|
|
|
* @var string |
|
17
|
|
|
*/ |
|
18
|
|
|
protected $fg; |
|
19
|
|
|
|
|
20
|
|
|
/** |
|
21
|
|
|
* @var string |
|
22
|
|
|
*/ |
|
23
|
|
|
protected $bg; |
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* @var string |
|
27
|
|
|
*/ |
|
28
|
|
|
protected $markerOn; |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* @var string |
|
32
|
|
|
*/ |
|
33
|
|
|
protected $markerOff; |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* @var string |
|
37
|
|
|
*/ |
|
38
|
|
|
protected $itemExtra; |
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* @var bool |
|
42
|
|
|
*/ |
|
43
|
|
|
protected $displaysExtra; |
|
44
|
|
|
|
|
45
|
|
|
/** |
|
46
|
|
|
* @var string |
|
47
|
|
|
*/ |
|
48
|
|
|
protected $coloursSetCode; |
|
49
|
|
|
|
|
50
|
|
|
protected $custom = false; |
|
51
|
|
|
|
|
52
|
|
|
public function getIsCustom() : bool |
|
53
|
|
|
{ |
|
54
|
|
|
return $this->custom; |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
/** |
|
58
|
|
|
* Get the colour code for Bg and Fg |
|
59
|
|
|
*/ |
|
60
|
|
|
public function getColoursSetCode() : string |
|
61
|
|
|
{ |
|
62
|
|
|
return $this->coloursSetCode; |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
public function getFg() |
|
66
|
|
|
{ |
|
67
|
|
|
return $this->fg; |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
public function setFg(string $fg, string $fallback = null) : self |
|
71
|
|
|
{ |
|
72
|
|
|
$this->custom = true; |
|
73
|
|
|
|
|
74
|
|
|
$this->fg = ColourUtil::validateColour( |
|
75
|
|
|
$this->terminal, |
|
76
|
|
|
$fg, |
|
77
|
|
|
$fallback |
|
78
|
|
|
); |
|
79
|
|
|
$this->generateColoursSetCode(); |
|
80
|
|
|
|
|
81
|
|
|
return $this; |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
|
|
public function getBg() |
|
85
|
|
|
{ |
|
86
|
|
|
return $this->bg; |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
|
|
public function setBg(string $bg, string $fallback = null) : self |
|
90
|
|
|
{ |
|
91
|
|
|
$this->custom = true; |
|
92
|
|
|
|
|
93
|
|
|
$this->bg = ColourUtil::validateColour( |
|
94
|
|
|
$this->terminal, |
|
95
|
|
|
$bg, |
|
96
|
|
|
$fallback |
|
97
|
|
|
); |
|
98
|
|
|
|
|
99
|
|
|
$this->generateColoursSetCode(); |
|
100
|
|
|
|
|
101
|
|
|
return $this; |
|
102
|
|
|
} |
|
103
|
|
|
|
|
104
|
|
|
public function getMarker(bool $selected) : string |
|
105
|
|
|
{ |
|
106
|
|
|
return $selected ? $this->markerOn : $this->markerOff; |
|
107
|
|
|
} |
|
108
|
|
|
|
|
109
|
|
|
public function getMarkerOn() : string |
|
110
|
|
|
{ |
|
111
|
|
|
return $this->markerOn; |
|
112
|
|
|
} |
|
113
|
|
|
|
|
114
|
|
|
public function setMarkerOn(string $marker) : self |
|
115
|
|
|
{ |
|
116
|
|
|
$this->custom = true; |
|
117
|
|
|
|
|
118
|
|
|
$this->markerOn = $marker; |
|
119
|
|
|
|
|
120
|
|
|
return $this; |
|
121
|
|
|
} |
|
122
|
|
|
|
|
123
|
|
|
public function getMarkerOff() : string |
|
124
|
|
|
{ |
|
125
|
|
|
return $this->markerOff; |
|
126
|
|
|
} |
|
127
|
|
|
|
|
128
|
|
|
public function setMarkerOff(string $marker) : self |
|
129
|
|
|
{ |
|
130
|
|
|
$this->custom = true; |
|
131
|
|
|
|
|
132
|
|
|
$this->markerOff = $marker; |
|
133
|
|
|
|
|
134
|
|
|
return $this; |
|
135
|
|
|
} |
|
136
|
|
|
|
|
137
|
|
|
public function getItemExtra() : string |
|
138
|
|
|
{ |
|
139
|
|
|
return $this->itemExtra; |
|
140
|
|
|
} |
|
141
|
|
|
|
|
142
|
|
|
public function setItemExtra(string $itemExtra) : self |
|
143
|
|
|
{ |
|
144
|
|
|
$this->custom = true; |
|
145
|
|
|
|
|
146
|
|
|
$this->itemExtra = $itemExtra; |
|
147
|
|
|
|
|
148
|
|
|
return $this; |
|
149
|
|
|
} |
|
150
|
|
|
|
|
151
|
|
|
public function getDisplaysExtra() : bool |
|
152
|
|
|
{ |
|
153
|
|
|
return $this->displaysExtra; |
|
154
|
|
|
} |
|
155
|
|
|
|
|
156
|
|
|
public function setDisplaysExtra(bool $displaysExtra) : self |
|
157
|
|
|
{ |
|
158
|
|
|
$this->custom = true; |
|
159
|
|
|
|
|
160
|
|
|
$this->displaysExtra = $displaysExtra; |
|
161
|
|
|
|
|
162
|
|
|
return $this; |
|
163
|
|
|
} |
|
164
|
|
|
|
|
165
|
|
|
/** |
|
166
|
|
|
* Generates the ansi escape sequence to set the colours |
|
167
|
|
|
*/ |
|
168
|
|
|
private function generateColoursSetCode() : void |
|
169
|
|
|
{ |
|
170
|
|
|
if (!ctype_digit($this->fg)) { |
|
171
|
|
|
$fgCode = Colour::AVAILABLE_FOREGROUND_COLOURS[$this->fg]; |
|
172
|
|
|
} else { |
|
173
|
|
|
$fgCode = sprintf("38;5;%s", $this->fg); |
|
174
|
|
|
} |
|
175
|
|
|
|
|
176
|
|
|
if (!ctype_digit($this->bg)) { |
|
177
|
|
|
$bgCode = Colour::AVAILABLE_BACKGROUND_COLOURS[$this->bg]; |
|
178
|
|
|
} else { |
|
179
|
|
|
$bgCode = sprintf("48;5;%s", $this->bg); |
|
180
|
|
|
} |
|
181
|
|
|
|
|
182
|
|
|
$this->coloursSetCode = sprintf("\033[%s;%sm", $fgCode, $bgCode); |
|
183
|
|
|
} |
|
184
|
|
|
} |
|
185
|
|
|
|