Skip to content

Commit b3ab8b0

Browse files
committed
Allows showing commands separated by groups
1 parent e8000aa commit b3ab8b0

File tree

2 files changed

+61
-8
lines changed

2 files changed

+61
-8
lines changed

src/Commands/Index.php

Lines changed: 44 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -51,25 +51,61 @@ public function getOptions() : array
5151

5252
protected function listCommands() : void
5353
{
54-
$width = 0;
55-
$lengths = [];
56-
foreach (\array_keys($this->console->getCommands()) as $name) {
57-
$lengths[$name] = \mb_strlen($name);
58-
if ($lengths[$name] > $width) {
59-
$width = $lengths[$name];
54+
$groupDefault = [];
55+
$groups = [];
56+
foreach ($this->console->getCommands() as $name => $command) {
57+
$group = $command->getGroup();
58+
if ($group === null) {
59+
$groupDefault[$name] = $command;
60+
continue;
6061
}
62+
$groups[$group][$name] = $command;
6163
}
6264
CLI::write(
63-
$this->console->getLanguage()->render('cli', 'commands') . ':',
65+
$this->console->getLanguage()->render('cli', 'availableCommands') . ':',
6466
ForegroundColor::yellow
6567
);
66-
foreach ($this->console->getCommands() as $name => $command) {
68+
[$width, $lengths] = $this->getWidthAndLengths($groupDefault);
69+
foreach ($groupDefault as $name => $command) {
6770
CLI::write(
6871
' ' . CLI::style($name, ForegroundColor::green) . ' '
72+
// @phpstan-ignore-next-line
6973
. \str_repeat(' ', $width - $lengths[$name])
7074
. $command->getDescription()
7175
);
7276
}
77+
\ksort($groups);
78+
foreach ($groups as $groupName => $commands) {
79+
CLI::newLine();
80+
CLI::write(' ' . $groupName . ':', ForegroundColor::brightYellow);
81+
[$width, $lengths] = $this->getWidthAndLengths($commands);
82+
foreach ($commands as $name => $command) {
83+
CLI::write(
84+
' ' . CLI::style($name, ForegroundColor::green) . ' '
85+
// @phpstan-ignore-next-line
86+
. \str_repeat(' ', $width - $lengths[$name])
87+
. $command->getDescription()
88+
);
89+
}
90+
}
91+
}
92+
93+
/**
94+
* @param array<string,Command> $commands
95+
*
96+
* @return array<array<string,int>|int>
97+
*/
98+
protected function getWidthAndLengths(array $commands) : array
99+
{
100+
$width = 0;
101+
$lengths = [];
102+
foreach (\array_keys($commands) as $name) {
103+
$lengths[$name] = \mb_strlen($name);
104+
if ($lengths[$name] > $width) {
105+
$width = $lengths[$name];
106+
}
107+
}
108+
return [$width, $lengths];
73109
}
74110

75111
protected function showHeader() : void

tests/Commands/IndexTest.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,4 +38,21 @@ public function testOptionGreet() : void
3838
$console->exec('index -g');
3939
self::assertStringContainsString('Good ', Stdout::getContents());
4040
}
41+
42+
public function testManyGroups() : void
43+
{
44+
$console = new Console();
45+
$foo = new Foo();
46+
$foo->setName('fooCommand')->setGroup('Group 2');
47+
$bar = new Foo();
48+
$bar->setName('barCommand')->setGroup('Group 1');
49+
$console->addCommand($foo)->addCommand($bar);
50+
Stdout::init();
51+
$console->exec('index');
52+
self::assertStringContainsString('Group 2', Stdout::getContents());
53+
self::assertStringContainsString('Group 1', Stdout::getContents());
54+
$group2Pos = \strpos(Stdout::getContents(), 'Group 2');
55+
$group1Pos = \strpos(Stdout::getContents(), 'Group 1');
56+
self::assertTrue($group2Pos > $group1Pos);
57+
}
4158
}

0 commit comments

Comments
 (0)