Skip to content

Commit 293ec64

Browse files
committed
Add 'list' command padding settings to EnvironmentConfig.
1 parent 2059581 commit 293ec64

File tree

12 files changed

+664
-103
lines changed

12 files changed

+664
-103
lines changed

docs/features-manual.md

Lines changed: 50 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -540,6 +540,52 @@ by `somewhere/launchers/launcher.php` main config.
540540

541541
### Available settings
542542

543+
#### listPaddingLeftMain
544+
545+
* Affects the padding from the left border of a terminal for `list` [built-in subcommand](#built-in-subcommands) output.
546+
The padding precedes each line (headers and command names).
547+
548+
* The setting is ignored (treated as `0`) if `--slim` output mode is enabled.
549+
* Controls the padding size as the number of space characters.
550+
* Possible values: non-negative `int`.
551+
* Values below `0` are silently treated as `0`.
552+
553+
#### listPaddingLeftCommand
554+
555+
* Affects the padding to the left of a command or subsequent header (in addition to
556+
[listPaddingLeftMain](#listpaddingleftmain)) for `list` [built-in subcommand](#built-in-subcommands) output.
557+
558+
* The setting is ignored (treated as `0`) if `--slim` output mode is enabled.
559+
* Controls the padding size as the number of space characters.
560+
* Possible values: non-negative `int`.
561+
* Values below `0` are silently treated as `0`.
562+
563+
Consider a command name `some:cool:command` with [listPaddingLeftMain](#listpaddingleftmain) = 1 and this setting = 4.
564+
The command contains 2 sections, so after the main header (the first section) 2 more lines will be shown - a subsection
565+
and a full command name:
566+
567+
```
568+
# This line is added here as a reference - it does not have any left padding.
569+
some:
570+
some:cool:
571+
some:cool:command
572+
```
573+
574+
The first (main) header is padded with [listPaddingLeftMain](#listpaddingleftmain), however subsequent header and
575+
the command itself are additionally padded with this setting, `listPaddingLeftCommand`.
576+
577+
#### listPaddingLeftCommandDescription
578+
579+
* Affects the minimum left padding for all commands descriptions outputted with `list`
580+
[built-in subcommand](#built-in-subcommands).
581+
582+
The maximum padding depends on the longest command name "column", which consists of
583+
[listPaddingLeftMain](#listpaddingleftmain), [listPaddingLeftCommand](#listpaddingleftcommand) for each section, and
584+
the command full name (including name sections).
585+
* Controls the padding size as the number of space characters.
586+
* Possible values: non-negative `int`.
587+
* Values below `0` are silently treated as `0`.
588+
543589
#### helpGeneratorPaddingLeftMain
544590

545591
* Affects the padding from the left border of a terminal. The padding precedes almost all lines from generated `help`
@@ -550,7 +596,10 @@ by `somewhere/launchers/launcher.php` main config.
550596

551597
#### helpGeneratorPaddingLeftParameterDescription
552598

553-
* Affects the left padding for parameters' descriptions.
599+
* Affects the minimum left padding for parameters' descriptions outputted on a generated `help` page.
600+
601+
The maximum padding depends on the longest parameter name "column", which consists of
602+
[helpGeneratorPaddingLeftMain](#helpgeneratorpaddingleftmain) and a parameter formatted name.
554603
* Controls the padding size as the number of space characters.
555604
* Possible values: non-negative `int`.
556605
* Values below `0` are silently treated as `0`.

docs/todo.md

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -112,8 +112,7 @@ The list of plans and ideas for future development.
112112
- ~~making `getConfigBuilder()` non-static, creating `ConfigBuilder` instance inside `__construct()`.~~
113113
1. - [x] Make creating buildable instances that are expected to be called by users (like detectors)
114114
with `create` static methods.
115-
1. - [ ] Move `ListSubcommands::PADDING_BLOCK` as 2 separate `EnvironmentConfig` settings - name and description
116-
paddings.
115+
1. - [x] Transform `ListSubcommands::PADDING_BLOCK` into separate `EnvironmentConfig` padding settings.
117116
1. - [ ] Place attributes after phpdoc blocks.
118117
1. - [x] Consider adding even more [backward incompatibilities](todo.md#next-major-release) ~~or delaying
119118
the next major release, see [already implemented backward incompatibilities](changelog.md#v300)~~.

src/Parametizer/EnvironmentConfig.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,10 @@ class EnvironmentConfig {
1515

1616
/* AVAILABLE PROPERTIES -> */
1717

18+
public int $listPaddingLeftMain = 1;
19+
public int $listPaddingLeftCommand = 2;
20+
public int $listPaddingLeftCommandDescription = 4;
21+
1822
public int $helpGeneratorPaddingLeftMain = 2;
1923
public int $helpGeneratorPaddingLeftParameterDescription = 4;
2024
public int $helpGeneratorShortDescriptionCharsMinBeforeFullStop = 40;

src/Parametizer/ScriptClass/BuiltinSubcommand/ListSubcommands.php

Lines changed: 18 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,6 @@
1515
use Override;
1616

1717
class ListSubcommands extends BuiltinSubcommandAbstract {
18-
protected const string PADDING_BLOCK = ' ';
19-
2018
protected const string HEADER_DEFAULT = '--';
2119

2220

@@ -27,6 +25,10 @@ class ListSubcommands extends BuiltinSubcommandAbstract {
2725
protected readonly bool $isSlim;
2826
protected readonly string $subcommandNamePart;
2927

28+
protected readonly int $paddingLeftMain;
29+
protected readonly int $paddingLeftCommand;
30+
protected readonly int $paddingLeftCommandDescription;
31+
3032
#[Override]
3133
public static function getScriptInnerName(): string {
3234
return 'list';
@@ -58,6 +60,10 @@ public function __construct(CliRequest $request) {
5860

5961
$this->isSlim = $request->getParamAsBool('slim');
6062
$this->subcommandNamePart = $request->getParamAsString('subcommand-name-part');
63+
64+
$this->paddingLeftMain = $this->isSlim ? 0 : max(0, $this->environmentConfig->listPaddingLeftMain);
65+
$this->paddingLeftCommand = max(0, $this->environmentConfig->listPaddingLeftCommand);
66+
$this->paddingLeftCommandDescription = max(0, $this->environmentConfig->listPaddingLeftCommandDescription);
6167
}
6268

6369
public function execute(): void {
@@ -89,7 +95,6 @@ public function execute(): void {
8995
$subcommandGroupsAuto = [];
9096

9197
$subcommandNameColumnWidthMax = 0;
92-
$padBlockWidth = mb_strlen(static::PADDING_BLOCK);
9398
foreach ($this->parentConfig->getBranches() as $subcommandName => $subcommandConfig) {
9499
$isBuiltInSubcommand = array_key_exists($subcommandName, $builtInSubcommands);
95100

@@ -111,8 +116,8 @@ public function execute(): void {
111116
} else {
112117
$nodeLevel = mb_substr_count($subcommandName, static::NAME_SECTION_SEPARATOR);
113118
}
114-
$subcommandNameColumnWidth = $padBlockWidth * max($this->isSlim ? 0 : 1, $nodeLevel)
115-
+ mb_strlen($subcommandName);
119+
$subcommandNameColumnWidth = mb_strlen($subcommandName)
120+
+ $this->paddingLeftCommand * max($this->isSlim ? 0 : 1, $nodeLevel);
116121
if ($subcommandNameColumnWidthMax < $subcommandNameColumnWidth) {
117122
$subcommandNameColumnWidthMax = $subcommandNameColumnWidth;
118123
}
@@ -223,7 +228,8 @@ protected function outputNode(
223228
echo PHP_EOL;
224229
}
225230

226-
$subcommandNameOutput = str_repeat(static::PADDING_BLOCK, $nodeLevel) . $elementName;
231+
$subcommandNameOutput = str_repeat(' ', $this->paddingLeftMain + $this->paddingLeftCommand * $nodeLevel)
232+
. $elementName;
227233
if ($elementValue instanceof Config) {
228234
$subcommandNameOutputFormatted = $subcommandNameOutput;
229235
if ('' !== $this->subcommandNamePart) {
@@ -235,18 +241,19 @@ protected function outputNode(
235241
}
236242
echo $this->formatter->paramValue($subcommandNameOutputFormatted);
237243
} else {
238-
if (0 === $nodeLevel) {
239-
echo ' ';
240-
}
241244
echo $this->formatter->helpNote($subcommandNameOutput);
242245
}
243246

244247
if ($elementValue instanceof Config) {
245248
$shortDescription = HelpGenerator::getScriptShortDescription($elementValue, $this->environmentConfig);
246249

247250
if ('' !== $shortDescription) {
248-
echo mb_str_pad('', $subcommandNameColumnWidthMax - mb_strlen($subcommandNameOutput))
249-
. static::PADDING_BLOCK . $shortDescription;
251+
echo str_repeat(
252+
' ',
253+
$this->paddingLeftMain + $this->paddingLeftCommandDescription
254+
+ $subcommandNameColumnWidthMax - mb_strlen($subcommandNameOutput),
255+
)
256+
. $shortDescription;
250257
}
251258
}
252259
echo PHP_EOL;

0 commit comments

Comments
 (0)