-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexample.php
More file actions
287 lines (240 loc) · 10.4 KB
/
example.php
File metadata and controls
287 lines (240 loc) · 10.4 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
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
<?php
require_once __DIR__ . '/vendor/autoload.php';
use PhpCliToolkit\Command\Application;
use PhpCliToolkit\Command\Command;
use PhpCliToolkit\Output\Text;
use PhpCliToolkit\Output\Table;
use PhpCliToolkit\Output\Grid;
use PhpCliToolkit\Output\Cell;
use PhpCliToolkit\Output\ProgressBar;
use PhpCliToolkit\Input\Prompt;
use PhpCliToolkit\Output\Styles\Colors;
use PhpCliToolkit\Output\Styles\BgColors;
use PhpCliToolkit\Output\Styles\Formattings;
class TextCommand extends Command {
public function name(): string { return 'text'; }
public function description(): string { return 'Showcase the Text component with padding, margin, and styles'; }
public function handle(): int {
(new Text('Full-width banner'))
->style([Formattings::BOLD, Colors::BRIGHT_WHITE, BgColors::BLUE])
->margin(1, 0, 1, 0, [BgColors::BLUE])
->write();
(new Text('Padded box — inner spacing on all sides'))
->style([Colors::WHITE, BgColors::MAGENTA])
->padding(1, 0, 1, 4, [BgColors::MAGENTA])
->margin(0, 0, 1, 0)
->write();
(new Text('Success ✔'))
->style([Formattings::BOLD, Colors::BLACK, BgColors::GREEN])
->padding(0, 0, 0, 2, [BgColors::GREEN])
->write();
(new Text('Warning ⚠'))
->style([Formattings::BOLD, Colors::BLACK, BgColors::YELLOW])
->padding(0, 0, 0, 2, [BgColors::YELLOW])
->write();
(new Text('Error ✖'))
->style([Formattings::BOLD, Colors::WHITE, BgColors::RED])
->padding(0, 0, 0, 2, [BgColors::RED])
->write();
(new Text('Dim italic note — subtle helper text'))
->style([Formattings::DIM, Formattings::ITALIC, Colors::BRIGHT_BLACK])
->margin(1, 0, 1, 0)
->write();
return 0;
}
}
class TableCommand extends Command {
public function name(): string { return 'table'; }
public function description(): string { return 'Showcase the Table component'; }
public function handle(): int {
(new Text(' Users'))
->style([Formattings::BOLD, Colors::BRIGHT_WHITE])
->margin(1, 0, 0, 0)
->write();
(new Table(
headers: ['Name', 'Role', 'Status', 'Joined'],
rows: [
['Alice', 'Admin', 'Active', '2022-01-15'],
['Bob', 'Editor', 'Inactive', '2022-03-22'],
['Carol', 'Viewer', 'Active', '2023-07-01'],
['Dave', 'Moderator', 'Active', '2021-11-30'],
]
))->render();
echo "\n";
return 0;
}
}
class GridCommand extends Command {
public function name(): string { return 'grid'; }
public function description(): string { return 'Showcase Grid and Cell with various layouts'; }
public function handle(): int {
(new Text(' 2-column equal'))
->style([Formattings::BOLD, Colors::BRIGHT_WHITE])
->margin(1, 0, 0, 0)
->write();
(new Grid(columns: 2, gap: 2))
->cell('Left column', style: [Colors::WHITE, BgColors::BLUE], paddingTop: 1, paddingBottom: 1, paddingLeft: 2, paddingRight: 2, paddingStyle: [BgColors::BLUE])
->cell('Right column', style: [Colors::WHITE, BgColors::CYAN], paddingTop: 1, paddingBottom: 1, paddingLeft: 2, paddingRight: 2, paddingStyle: [BgColors::CYAN])
->render();
(new Text(' 3-column fixed widths — long content wraps in the centre'))
->style([Formattings::BOLD, Colors::BRIGHT_WHITE])
->margin(1, 0, 0, 0)
->write();
(new Grid(columns: [20, 36, 20], gap: 1))
->add(new Cell('Narrow left', [Colors::WHITE, BgColors::MAGENTA], 1, 1, 1, 1, [BgColors::MAGENTA]))
->add(new Cell('Wide centre — this column has more room for content and will wrap gracefully', [Colors::WHITE, BgColors::BLUE], 1, 1, 1, 1, [BgColors::BLUE]))
->add(new Cell('Narrow right', [Colors::WHITE, BgColors::GREEN], 1, 1, 1, 1, [BgColors::GREEN]))
->render();
(new Text(' 3-column auto with 6 cells (wraps into 2 rows)'))
->style([Formattings::BOLD, Colors::BRIGHT_WHITE])
->margin(1, 0, 0, 0)
->write();
(new Grid(columns: 3, gap: 2))
->cell('Alpha', style: [Colors::WHITE, BgColors::BLUE])
->cell('Beta', style: [Colors::WHITE, BgColors::GREEN])
->cell('Gamma', style: [Colors::WHITE, BgColors::RED])
->cell('Delta', style: [Colors::WHITE, BgColors::YELLOW])
->cell('Epsilon', style: [Colors::BLACK, BgColors::BRIGHT_WHITE])
->cell('Zeta', style: [Colors::WHITE, BgColors::MAGENTA])
->render();
echo "\n";
return 0;
}
}
class ProgressCommand extends Command {
public function name(): string { return 'progress'; }
public function description(): string { return 'Showcase the ProgressBar component'; }
protected function setup(): void {
$this->parser->registerOption('steps|s', 'Number of steps to simulate', default: 30);
}
public function handle(): int {
$steps = (int) ($this->parser->getOption('steps') ?? 30);
(new Text(" Simulating {$steps} steps…"))
->style([Formattings::DIM, Colors::BRIGHT_WHITE])
->margin(1, 0, 1, 0)
->write();
$bar = new ProgressBar($steps);
for ($i = 0; $i < $steps; $i++) {
usleep(60_000);
$bar->advance();
}
$bar->finish();
(new Text(' Done!'))
->style([Formattings::BOLD, Colors::BLACK, BgColors::GREEN])
->padding(0, 0, 0, 2, [BgColors::GREEN])
->margin(1, 0, 1, 0)
->write();
return 0;
}
}
class PromptCommand extends Command {
public function name(): string { return 'prompt'; }
public function description(): string { return 'Showcase all Prompt interactions (ask, confirm, secret, select)'; }
public function handle(): int {
(new Text(' Interactive Prompts'))
->style([Formattings::BOLD, Colors::BRIGHT_WHITE, BgColors::BLUE])
->margin(1, 0, 1, 0, [BgColors::BLUE])
->write();
$name = Prompt::ask('Your name', 'World');
$lang = Prompt::select('Favourite language', ['PHP', 'Python', 'Go', 'Rust']);
$confirmed = Prompt::confirm('Continue?', true);
$secret = Prompt::secret('Enter a secret (input hidden)');
echo "\n";
(new Table(
headers: ['Field', 'Value'],
rows: [
['name', $name],
['language', $lang],
['confirmed', $confirmed ? 'yes' : 'no'],
['secret', str_repeat('*', strlen($secret)) ?: '(empty)'],
]
))->render();
echo "\n";
return 0;
}
}
class StylesCommand extends Command {
public function name(): string { return 'styles'; }
public function description(): string { return 'Display all foreground colors, background colors, and text formats'; }
public function handle(): int {
$esc = static fn(array $codes, string $text): string =>
"\033[" . implode(';', $codes) . "m{$text}\033[0m";
(new Text(' Foreground Colors'))
->style([Formattings::BOLD, Colors::BRIGHT_WHITE])
->margin(1, 0, 0, 0)
->write();
$fgColors = [
'BLACK' => Colors::BLACK,
'RED' => Colors::RED,
'GREEN' => Colors::GREEN,
'YELLOW' => Colors::YELLOW,
'BLUE' => Colors::BLUE,
'MAGENTA' => Colors::MAGENTA,
'CYAN' => Colors::CYAN,
'WHITE' => Colors::WHITE,
'BRIGHT_BLACK' => Colors::BRIGHT_BLACK,
'BRIGHT_RED' => Colors::BRIGHT_RED,
'BRIGHT_GREEN' => Colors::BRIGHT_GREEN,
'BRIGHT_YELLOW' => Colors::BRIGHT_YELLOW,
'BRIGHT_BLUE' => Colors::BRIGHT_BLUE,
'BRIGHT_MAGENTA' => Colors::BRIGHT_MAGENTA,
'BRIGHT_CYAN' => Colors::BRIGHT_CYAN,
'BRIGHT_WHITE' => Colors::BRIGHT_WHITE,
];
foreach ($fgColors as $name => $code) {
echo ' ' . $esc([$code], str_pad($name, 20)) . "\n";
}
(new Text(' Background Colors'))
->style([Formattings::BOLD, Colors::BRIGHT_WHITE])
->margin(1, 0, 0, 0)
->write();
$bgColors = [
'BLACK' => BgColors::BLACK,
'RED' => BgColors::RED,
'GREEN' => BgColors::GREEN,
'YELLOW' => BgColors::YELLOW,
'BLUE' => BgColors::BLUE,
'MAGENTA' => BgColors::MAGENTA,
'CYAN' => BgColors::CYAN,
'WHITE' => BgColors::WHITE,
'BRIGHT_BLACK' => BgColors::BRIGHT_BLACK,
'BRIGHT_RED' => BgColors::BRIGHT_RED,
'BRIGHT_GREEN' => BgColors::BRIGHT_GREEN,
'BRIGHT_YELLOW' => BgColors::BRIGHT_YELLOW,
'BRIGHT_BLUE' => BgColors::BRIGHT_BLUE,
'BRIGHT_MAGENTA' => BgColors::BRIGHT_MAGENTA,
'BRIGHT_CYAN' => BgColors::BRIGHT_CYAN,
'BRIGHT_WHITE' => BgColors::BRIGHT_WHITE,
];
foreach ($bgColors as $name => $code) {
echo ' ' . $esc([$code, Colors::BLACK], str_pad($name, 20)) . "\n";
}
(new Text(' Text Formats'))
->style([Formattings::BOLD, Colors::BRIGHT_WHITE])
->margin(1, 0, 0, 0)
->write();
$formats = [
'BOLD' => [Formattings::BOLD],
'DIM' => [Formattings::DIM],
'ITALIC' => [Formattings::ITALIC],
'UNDERLINE' => [Formattings::UNDERLINE],
'BLINK' => [Formattings::BLINK],
'INVERT' => [Formattings::INVERT],
];
foreach ($formats as $name => $codes) {
echo ' ' . $esc($codes, str_pad($name, 20)) . "\n";
}
echo "\n";
return 0;
}
}
$app = new Application('toolkit-demo', '1.0.0');
$app->register(
new TextCommand(),
new TableCommand(),
new GridCommand(),
new ProgressCommand(),
new PromptCommand(),
new StylesCommand(),
);
$app->run();