Skip to content

Commit 41db27d

Browse files
committed
fix: command help vars not replace as value
1 parent b2ee9dd commit 41db27d

File tree

5 files changed

+48
-9
lines changed

5 files changed

+48
-9
lines changed

src/Component/Formatter/MultiList.php

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ class MultiList extends MessageFormatter
1717
* Format and render multi list
1818
*
1919
* ```php
20-
* [
20+
* $data = [
2121
* 'list1 title' => [
2222
* 'name' => 'value text',
2323
* 'name2' => 'value text 2',
@@ -27,11 +27,15 @@ class MultiList extends MessageFormatter
2727
* 'name2' => 'value text 2',
2828
* ],
2929
* ... ...
30-
* ]
30+
* ];
31+
*
32+
* MultiList::show($data);
3133
* ```
3234
*
3335
* @param array $data
3436
* @param array $opts
37+
*
38+
* @psalm-param array{beforeWrite: callable, lastNewline: bool} $opts
3539
*/
3640
public static function show(array $data, array $opts = []): void
3741
{
@@ -45,6 +49,12 @@ public static function show(array $data, array $opts = []): void
4549
unset($opts['lastNewline']);
4650
}
4751

52+
$beforeWrite = null;
53+
if (isset($opts['beforeWrite'])) {
54+
$beforeWrite = $opts['beforeWrite'];
55+
unset($opts['beforeWrite']);
56+
}
57+
4858
foreach ($data as $title => $list) {
4959
if ($ignoreEmpty && !$list) {
5060
continue;
@@ -53,6 +63,13 @@ public static function show(array $data, array $opts = []): void
5363
$stringList[] = SingleList::show($list, (string)$title, $opts);
5464
}
5565

56-
Console::write(implode("\n", $stringList), $lastNewline);
66+
$str = implode("\n", $stringList);
67+
68+
// before write handler
69+
if ($beforeWrite) {
70+
$str = $beforeWrite($str);
71+
}
72+
73+
Console::write($str, $lastNewline);
5774
}
5875
}

src/Component/Formatter/Section.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -68,11 +68,11 @@ public static function show(string $title, $body, array $opts = []): void
6868
$border = Str::pad($char, $width, $char);
6969

7070
if ($showTBorder) {
71-
$topBorder = "{$indentStr}$border\n";
71+
$topBorder = "$indentStr$border\n";
7272
}
7373

7474
if ($showBBorder) {
75-
$bottomBorder = "{$indentStr}$border\n";
75+
$bottomBorder = "$indentStr$border\n";
7676
}
7777
}
7878

src/Component/MessageFormatter.php

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,11 @@ abstract class MessageFormatter implements FormatterInterface
3232
*/
3333
protected $config = [];
3434

35+
/**
36+
* @var callable
37+
*/
38+
protected $beforeWrite;
39+
3540
/**
3641
* @param array $config
3742
*
@@ -105,4 +110,20 @@ public function getConfig(): array
105110
{
106111
return $this->config;
107112
}
113+
114+
/**
115+
* @return callable
116+
*/
117+
public function getBeforeWrite(): callable
118+
{
119+
return $this->beforeWrite;
120+
}
121+
122+
/**
123+
* @param callable $beforeWrite
124+
*/
125+
public function setBeforeWrite(callable $beforeWrite): void
126+
{
127+
$this->beforeWrite = $beforeWrite;
128+
}
108129
}

src/Concern/CommandHelpTrait.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,7 @@ public function showHelpByFlagsParser(FlagsParser $fs, array $aliases = [], stri
158158
$this->output->mList($help, [
159159
'sepChar' => ' ',
160160
'lastNewline' => false,
161+
'beforeWrite' => [$this, 'parseCommentsVars'],
161162
]);
162163

163164
return 0;

src/Util/FormatUtil.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -94,20 +94,20 @@ public static function applyIndent(string $string, int $indent = 2, string $inde
9494
* amet.
9595
* ```
9696
*
97-
* @param string $text the text to be wrapped
97+
* @param string $text the text to be wrapped
9898
* @param integer $indent number of spaces to use for indentation.
9999
* @param integer $width
100100
*
101101
* @return string the wrapped text.
102102
* @from yii2
103103
*/
104-
public static function wrapText($text, $indent = 0, $width = 0): string
104+
public static function wrapText(string $text, int $indent = 0, int $width = 0): string
105105
{
106106
if (!$text) {
107107
return $text;
108108
}
109109

110-
if ((int)$width <= 0) {
110+
if ($width <= 0) {
111111
$size = Sys::getScreenSize();
112112

113113
if ($size === false || $size[0] <= $indent) {
@@ -156,8 +156,8 @@ public static function alignOptions(array $options): array
156156
continue;
157157
}
158158

159+
// padding length equals to '-h, '
159160
if (!strpos($name, ',')) {
160-
// padding length equals to '-h, '
161161
$name = ' ' . $name;
162162
} else {
163163
$name = str_replace([' ', ','], ['', ', '], $name);

0 commit comments

Comments
 (0)