Skip to content

Commit 314a887

Browse files
committed
feat(helper): support grouped sorting for show help
1 parent 29b09ce commit 314a887

File tree

1 file changed

+15
-5
lines changed

1 file changed

+15
-5
lines changed

src/Helper/OutputHelper.php

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -164,11 +164,16 @@ protected function showHelp(string $for, array $items, string $header = '', stri
164164
}
165165

166166
$space = 4;
167+
$group = $lastGroup = null;
167168
foreach ($this->sortItems($items, $padLen) as $item) {
168-
$name = $this->getName($item);
169-
$desc = \str_replace(["\r\n", "\n"], \str_pad("\n", $padLen + $space + 3), $item->desc());
169+
$name = $this->getName($item);
170+
if ($for === 'Commands' && $lastGroup !== $group = $item->group()) {
171+
$this->writer->boldYellow($group ?: '*', true);
172+
$lastGroup = $group;
173+
}
174+
$desc = str_replace(["\r\n", "\n"], str_pad("\n", $padLen + $space + 3), $item->desc());
170175

171-
$this->writer->bold(' ' . \str_pad($name, $padLen + $space));
176+
$this->writer->bold(' ' . str_pad($name, $padLen + $space));
172177
$this->writer->comment($desc, true);
173178
}
174179

@@ -239,9 +244,14 @@ public function showCommandNotFound(string $attempted, array $available): self
239244
*/
240245
protected function sortItems(array $items, &$max = 0): array
241246
{
242-
$max = \max(\array_map(fn ($item) => \strlen($this->getName($item)), $items));
247+
$max = max(array_map(fn ($item) => strlen($this->getName($item)), $items));
248+
249+
uasort($items, static function ($a, $b) {
250+
$aName = $a instanceof Groupable ? $a->group() . $a->name() : $a->name();
251+
$bName = $b instanceof Groupable ? $b->group() . $b->name() : $b->name();
243252

244-
\uasort($items, fn ($a, $b) => $a->name() <=> $b->name());
253+
return $aName <=> $bName;
254+
});
245255

246256
return $items;
247257
}

0 commit comments

Comments
 (0)