-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathHelpRenderer.php
More file actions
163 lines (126 loc) · 5.22 KB
/
HelpRenderer.php
File metadata and controls
163 lines (126 loc) · 5.22 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
<?php
declare(strict_types=1);
/*
* This file is part of MMLC - ModifiedModuleLoaderClient.
*
* (c) Robin Wieschendorf <mail@robinwieschendorf.de>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace RobinTheHood\ModifiedModuleLoaderClient\Cli;
class HelpRenderer
{
private const INDENT = ' ';
private string $description = '';
private array $sections = [];
public function setDescription(string $description): void
{
$this->description = $description;
}
public function setUsage(string $command, string $description): void
{
$this->sections['usage'][$command] = $description;
}
public function addArgument(string $argument, string $description): void
{
$this->sections['arguments'][$argument] = $description;
}
public function addOption(string $short, string $long, string $description): void
{
$option = [
'short' => TextRenderer::color($short ? '-' . $short : '', TextRenderer::COLOR_GREEN),
'separator' => TextRenderer::color($short && $long ? ', ' : '', TextRenderer::COLOR_GREEN),
'long' => TextRenderer::color($long ? '--' . $long : '', TextRenderer::COLOR_GREEN),
'description' => $description,
];
$this->sections['options'][] = $option;
}
public function render(): string
{
$render = '';
$render .= $this->renderDescription();
$render .= $this->renderUsage();
$render .= $this->renderArguments();
$render .= $this->renderOptions();
return $render;
}
private function renderDescription(): string
{
$description = \PHP_EOL;
$description .= TextRenderer::color('Description:', TextRenderer::COLOR_YELLOW) . \PHP_EOL;
$description .= self::INDENT . $this->description . \PHP_EOL;
return $description;
}
private function renderUsage(): string
{
if (empty($this->sections['usage'])) {
return '';
}
$items = \array_keys($this->sections['usage']);
$maxLength = TextRenderer::getMaxLength($items) + 1;
$usage = \PHP_EOL;
$usage .= TextRenderer::color('Usage:', TextRenderer::COLOR_YELLOW) . \PHP_EOL;
foreach ($this->sections['usage'] as $argument => $argumentDescription) {
$name = self::INDENT . TextRenderer::rightPad($argument, $maxLength);
$text = TextRenderer::color($name, TextRenderer::COLOR_GREEN) . $argumentDescription . \PHP_EOL;
$usage .= $text;
}
return $usage;
}
private function renderArguments(): string
{
if (empty($this->sections['arguments'])) {
return '';
}
$items = \array_keys($this->sections['arguments']);
$maxLength = TextRenderer::getMaxLength($items) + 1;
$arguments = \PHP_EOL;
$arguments .= TextRenderer::color('Arguments:', TextRenderer::COLOR_YELLOW) . \PHP_EOL;
foreach ($this->sections['arguments'] as $argumentName => $argumentDescription) {
$name = self::INDENT . TextRenderer::rightPad($argumentName, $maxLength);
$text = TextRenderer::color($name, TextRenderer::COLOR_GREEN) . $argumentDescription . \PHP_EOL;
$arguments .= $text;
}
return $arguments;
}
private function renderOptions(): string
{
if (empty($this->sections['options'])) {
return '';
}
$options = \PHP_EOL;
$options .= TextRenderer::color('Options:', TextRenderer::COLOR_YELLOW) . \PHP_EOL;
$optionsShort = self::getTableColumn($this->sections['options'], 'short');
$optionsShortLength = TextRenderer::getMaxLength($optionsShort);
$optionsSeparator = self::getTableColumn($this->sections['options'], 'separator');
$optionsSeparatorLength = TextRenderer::getMaxLength($optionsSeparator);
$optionsLong = self::getTableColumn($this->sections['options'], 'long');
$optionsLongLength = TextRenderer::getMaxLength($optionsLong);
$optionsDescription = self::getTableColumn($this->sections['options'], 'description');
$optionsDescriptionLength = TextRenderer::getMaxLength($optionsDescription);
foreach ($this->sections['options'] as $option) {
$short = $option['short'];
$separator = $option['separator'];
$long = $option['long'];
$description = $option['description'];
$options .= self::INDENT;
$options .= TextRenderer::rightPad($short, $optionsShortLength);
$options .= TextRenderer::rightPad($separator, $optionsSeparatorLength);
$options .= TextRenderer::rightPad($long, $optionsLongLength);
$options .= ' ';
$options .= TextRenderer::rightPad($description, $optionsDescriptionLength);
$options .= \PHP_EOL;
}
return $options;
}
private static function getTableColumn(array $table, string $column): array {
$tableColumn = \array_map(
function ($row) use ($column) {
return $row[$column];
},
$table
);
return $tableColumn;
}
}