forked from maplephp/prompts
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAnsi.php
More file actions
executable file
·246 lines (223 loc) · 4.71 KB
/
Ansi.php
File metadata and controls
executable file
·246 lines (223 loc) · 4.71 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
<?php
namespace MaplePHP\Prompts;
use InvalidArgumentException;
/**
* Class Ansi
* @package MaplePHP\Prompts
*/
class Ansi
{
public const NAV = [
'\033[A' => 'up',
'\033[B' => 'down',
'\n' => 'enter'
];
private static ?bool $hasAnsi = null;
/**
* Set one or more styles
*
* @param string|array $styles
* @param string $message
* @return string
*/
public function style(string|array $styles, string $message): string
{
if (is_string($styles)) {
$styles = [$styles];
}
foreach ($styles as $style) {
if (!method_exists($this, $style)) {
throw new InvalidArgumentException("The style $style does not exist!", 1);
}
$message = $this->{$style}($message);
}
return $message;
}
/**
* Set a custom ansi style
*
* @param int $ansiNum
* @param string $message
* @return string
*/
public function ansiStyle(int $ansiNum, string $message): string
{
if (self::isSupported()) {
return "\033[{$ansiNum}m{$message}\033[0m";
}
return $message;
}
/**
* Bold input
*
* @param string $message
* @return string
*/
public function bold(string $message): string
{
return $this->ansiStyle(1, $message);
}
/**
* Italic input
*
* @param string $message
* @return string
*/
public function italic(string $message): string
{
return $this->ansiStyle(3, $message);
}
/**
* Red input color
*
* @param string $message
* @return string
*/
public function red(string $message): string
{
return $this->ansiStyle(31, $message);
}
/**
* Green input color
*
* @param string $message
* @return string
*/
public function green(string $message): string
{
return $this->ansiStyle(32, $message);
}
/**
* Yellow input color
*
* @param string $message
* @return string
*/
public function yellow(string $message): string
{
return $this->ansiStyle(33, $message);
}
/**
* Blue input color
*
* @param string $message
* @return string
*/
public function blue(string $message): string
{
return $this->ansiStyle(34, $message);
}
/**
* Style selected item
*
* @param string $message
* @return string
*/
public function selectedItem(string $message): string
{
return $this->style(['blue', 'bold'], $message);
}
/**
* Clear line and move down
*
* @return string
*/
public function clearDown(): string
{
return $this->clearLine() . $this->cursorDown();
}
/**
* Clear line
*
* @return string
*/
public function clearLine(): string
{
return "\033[2K";
}
/**
* Move cursor to specified line
*
* @param int $line
* @return string
*/
public function moveCursorTo(int $line): string
{
return "\033[{$line}A";
}
/**
* Move cursor down
*
* @return string
*/
public function cursorDown(): string
{
return "\033[1B";
}
/**
* Arrow up key
*
* @return string
*/
public function keyUp(): string
{
return "\033[A";
}
/**
* Arrow down key
*
* @return string
*/
public function keyDown(): string
{
return "\033[B";
}
/**
* Enter key
*
* @return string
*/
public function keyEnter(): string
{
return "\n";
}
/**
* Escape key
*
* @return string
*/
public function keyEscape(): string
{
return "\033";
}
/**
* Symbol: checkbox
*
* @return string
*/
public function checkbox(): string
{
return "\xE2\x9C\x94";
}
/**
* Check if terminal is modern (Not foolproof)
* This function will tell if terminal support ANSI
*
* @return bool
*/
final public static function isSupported(): bool
{
if (is_null(self::$hasAnsi)) {
if (stripos(PHP_OS, 'WIN') === 0) {
$osVersion = php_uname('v');
if (preg_match('/build (\d+)/i', $osVersion, $matches)) {
$buildNumber = (int)$matches[1];
self::$hasAnsi = ($buildNumber >= 10586);
}
} else {
self::$hasAnsi = (getenv('TERM') && str_contains(getenv('TERM'), 'xterm'));
}
}
return self::$hasAnsi;
}
}