forked from adhocore/php-cli
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathOutputHelper.php
More file actions
387 lines (327 loc) · 11.1 KB
/
OutputHelper.php
File metadata and controls
387 lines (327 loc) · 11.1 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
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
<?php
/*
* This file is part of the PHP-CLI package.
*
* (c) Jitendra Adhikari <jiten.adhikary@gmail.com>
* <https://github.com/adhocore>
*
* Licensed under MIT license.
*/
namespace Ahc\Cli\Helper;
use Ahc\Cli\Exception;
use Ahc\Cli\Input\Argument;
use Ahc\Cli\Input\Command;
use Ahc\Cli\Input\Groupable;
use Ahc\Cli\Input\Option;
use Ahc\Cli\Input\Parameter;
use Ahc\Cli\Output\Writer;
use Throwable;
use function Ahc\Cli\t;
use function array_map;
use function array_shift;
use function asort;
use function explode;
use function get_class;
use function gettype;
use function implode;
use function is_array;
use function is_object;
use function is_scalar;
use function key;
use function levenshtein;
use function max;
use function method_exists;
use function preg_replace;
use function preg_replace_callback;
use function realpath;
use function str_contains;
use function str_pad;
use function str_replace;
use function strlen;
use function strrpos;
use function trim;
use function uasort;
use function var_export;
use const STR_PAD_LEFT;
/**
* This helper helps you by showing you help information :).
*
* @author Jitendra Adhikari <jiten.adhikary@gmail.com>
* @license MIT
*
* @link https://github.com/adhocore/cli
*/
class OutputHelper
{
use InflectsString;
/**
* The output writer instance used to write formatted output.
*
* @var Writer
*/
protected Writer $writer;
/**
* Max width of command name.
*
* @var int
*/
protected int $maxCmdName = 0;
/**
* Class constructor.
*
* @param Writer|null $writer The output writer instance used to write formatted output.
*/
public function __construct(?Writer $writer = null)
{
$this->writer = $writer ?? new Writer;
}
/**
* Print stack trace and error msg of an exception.
*/
public function printTrace(Throwable $e): void
{
$eClass = get_class($e);
$this->writer->colors(
"$eClass <red>{$e->getMessage()}</end><eol/>" .
'(' . t('thrown in') . " <yellow>{$e->getFile()}</end><white>:{$e->getLine()})</end>"
);
// @codeCoverageIgnoreStart
if ($e instanceof Exception) {
// Internal exception traces are not printed.
return;
}
// @codeCoverageIgnoreEnd
$traceStr = '<eol/><eol/><bold>' . t('Stack Trace') . ':</end><eol/><eol/>';
foreach ($e->getTrace() as $i => $trace) {
$trace += ['class' => '', 'type' => '', 'function' => '', 'file' => '', 'line' => '', 'args' => []];
$symbol = $trace['class'] . $trace['type'] . $trace['function'];
$args = $this->stringifyArgs($trace['args']);
$traceStr .= " <comment>$i)</end> <red>$symbol</end><comment>($args)</end>";
if ('' !== $trace['file']) {
$file = realpath($trace['file']);
$traceStr .= '<eol/> <yellow>' . t('at') . " $file</end><white>:{$trace['line']}</end><eol/>";
}
}
$this->writer->colors($traceStr);
}
/**
* Converts an array of arguments into a string representation.
*
* Each array element is converted based on its type:
* - Scalar values (int, float, string, bool) are var_exported
* - Objects are converted using __toString() if available, otherwise class name is used
* - Arrays are recursively processed and wrapped in square brackets
* - Other types are converted to their type name
*
* @param array $args Array of arguments to be stringified
*
* @return string The comma-separated string representation of all arguments
*/
public function stringifyArgs(array $args): string
{
$holder = [];
foreach ($args as $arg) {
$holder[] = $this->stringifyArg($arg);
}
return implode(', ', $holder);
}
/**
* Converts the provided argument into a string representation.
*
* @param mixed $arg The argument to be converted into a string. This can be of any type.
*
* @return string A string representation of the provided argument.
*/
protected function stringifyArg(mixed $arg): string
{
if (is_scalar($arg)) {
return var_export($arg, true);
}
if (is_object($arg)) {
return method_exists($arg, '__toString') ? (string) $arg : get_class($arg);
}
if (is_array($arg)) {
return '[' . $this->stringifyArgs($arg) . ']';
}
return gettype($arg);
}
/**
* @param Argument[] $arguments
* @param string $header
* @param string $footer
*
* @return self
*/
public function showArgumentsHelp(array $arguments, string $header = '', string $footer = ''): self
{
$this->showHelp('Arguments', $arguments, $header, $footer);
return $this;
}
/**
* @param Option[] $options
* @param string $header
* @param string $footer
*
* @return self
*/
public function showOptionsHelp(array $options, string $header = '', string $footer = ''): self
{
$this->showHelp('Options', $options, $header, $footer);
return $this;
}
/**
* @param Command[] $commands
* @param string $header
* @param string $footer
*
* @return self
*/
public function showCommandsHelp(array $commands, string $header = '', string $footer = ''): self
{
$this->maxCmdName = $commands ? max(array_map(static fn (Command $cmd) => strlen($cmd->name()), $commands)) : 0;
$this->showHelp('Commands', $commands, $header, $footer);
return $this;
}
/**
* Show help with headers and footers.
*/
protected function showHelp(string $for, array $items, string $header = '', string $footer = ''): void
{
if ($header) {
$this->writer->help_header($header, true);
}
$this->writer->eol()->help_category(t($for) . ':', true);
if (empty($items)) {
$this->writer->help_text(' (n/a)', true);
return;
}
$space = 4;
$lastGroup = null;
$withDefault = $for === 'Options' || $for === 'Arguments';
foreach (array_values($this->sortItems($items, $padLen, $for)) as $idx => $item) {
$name = $this->getName($item);
if ($for === 'Commands' && $lastGroup !== $group = $item->group()) {
$lastGroup = $group;
if ($group !== '') {
$this->writer->help_group($group, true);
}
}
$desc = str_replace(["\r\n", "\n"], str_pad("\n", $padLen + $space + 3), $item->desc($withDefault));
if ($idx % 2 == 0) {
$this->writer->help_item_even(' ' . str_pad($name, $padLen + $space));
$this->writer->help_description_even($desc, true);
} else {
$this->writer->help_item_odd(' ' . str_pad($name, $padLen + $space));
$this->writer->help_description_odd($desc, true);
}
}
if ($footer) {
$this->writer->eol()->help_footer($footer, true);
}
}
/**
* Show usage examples of a Command.
*
* It replaces $0 with actual command name and properly pads ` ## ` segments.
*/
public function showUsage(string $usage): self
{
$usage = str_replace('$0', $_SERVER['argv'][0] ?? '[cmd]', $usage);
if (!str_contains($usage, ' ## ')) {
$this->writer->eol()->help_category(t('Usage Examples') . ':', true)->colors($usage)->eol();
return $this;
}
$lines = explode("\n", str_replace(['<eol>', '<eol/>', '</eol>', "\r\n"], "\n", $usage));
foreach ($lines as $i => &$pos) {
if (false === $pos = strrpos(preg_replace('~</?\w+/?>~', '', $pos), ' ##')) {
unset($lines[$i]);
}
}
$maxlen = ($lines ? max($lines) : 0) + 4;
$usage = preg_replace_callback('~ ## ~', static function () use (&$lines, $maxlen) {
return str_pad('# ', $maxlen - array_shift($lines), ' ', STR_PAD_LEFT);
}, $usage);
$this->writer->eol()->help_category(t('Usage Examples') . ':', true)->colors($usage)->eol();
return $this;
}
/**
* Shows an error message when a command is not found and suggests similar commands.
* Uses levenshtein distance to find commands that are similar to the attempted one.
*
* @param string $attempted The command name that was attempted to be executed
* @param array $available List of available command names
*
* @return OutputHelper For method chaining
*/
public function showCommandNotFound(string $attempted, array $available): self
{
$closest = [];
foreach ($available as $cmd) {
$lev = levenshtein($attempted, $cmd);
if ($lev > 0 && $lev < 5) {
$closest[$cmd] = $lev;
}
}
$this->writer->error(t('Command %s not found', [$attempted]), true);
if ($closest) {
asort($closest);
$closest = key($closest);
$this->writer->bgRed(t('Did you mean %s?', [$closest]), true);
}
return $this;
}
/**
* Sort items by name. As a side effect sets max length of all names.
*
* @param Parameter[]|Command[] $items
* @param int|null $max
* @param string $for
*
* @return array
*/
protected function sortItems(array $items, ?int &$max = 0, string $for = ''): array
{
$max = max(array_map(fn ($item) => strlen($this->getName($item)), $items));
if ($for === 'Arguments') { // Arguments are positional so must not be sorted
return $items;
}
uasort($items, static function ($a, $b) {
// Items in the default group (where group() returns empty/falsy) are prefixed with '__'
// to ensure they appear at the top of the sorted list, whilst grouped items follow after
$aName = $a instanceof Groupable ? ($a->group() ?: '__') . $a->name() : $a->name();
$bName = $b instanceof Groupable ? ($b->group() ?: '__') . $b->name() : $b->name();
return $aName <=> $bName;
});
return $items;
}
/**
* Prepare name for different items.
*
* @param Parameter|Command $item
*
* @return string
*/
protected function getName(Parameter|Command $item): string
{
$name = $item->name();
if ($item instanceof Command) {
return trim(str_pad($name, $this->maxCmdName) . ' ' . $item->alias());
}
return $this->label($item);
}
/**
* Get parameter label for humans.
*/
protected function label(Parameter $item): string
{
$name = $item->name();
if ($item instanceof Option) {
$name = $item->short() . '|' . $item->long();
}
$variad = $item->variadic() ? '...' : '';
if ($item->required()) {
return "<$name$variad>";
}
return "[$name$variad]";
}
}