Skip to content

Commit

Permalink
Allows showing commands separated by groups
Browse files Browse the repository at this point in the history
  • Loading branch information
natanfelles committed May 13, 2024
1 parent e8000aa commit b3ab8b0
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 8 deletions.
52 changes: 44 additions & 8 deletions src/Commands/Index.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,25 +51,61 @@ public function getOptions() : array

protected function listCommands() : void
{
$width = 0;
$lengths = [];
foreach (\array_keys($this->console->getCommands()) as $name) {
$lengths[$name] = \mb_strlen($name);
if ($lengths[$name] > $width) {
$width = $lengths[$name];
$groupDefault = [];
$groups = [];
foreach ($this->console->getCommands() as $name => $command) {
$group = $command->getGroup();
if ($group === null) {
$groupDefault[$name] = $command;
continue;
}
$groups[$group][$name] = $command;
}
CLI::write(
$this->console->getLanguage()->render('cli', 'commands') . ':',
$this->console->getLanguage()->render('cli', 'availableCommands') . ':',
ForegroundColor::yellow
);
foreach ($this->console->getCommands() as $name => $command) {
[$width, $lengths] = $this->getWidthAndLengths($groupDefault);
foreach ($groupDefault as $name => $command) {
CLI::write(
' ' . CLI::style($name, ForegroundColor::green) . ' '
// @phpstan-ignore-next-line
. \str_repeat(' ', $width - $lengths[$name])
. $command->getDescription()
);
}
\ksort($groups);
foreach ($groups as $groupName => $commands) {
CLI::newLine();
CLI::write(' ' . $groupName . ':', ForegroundColor::brightYellow);
[$width, $lengths] = $this->getWidthAndLengths($commands);
foreach ($commands as $name => $command) {
CLI::write(
' ' . CLI::style($name, ForegroundColor::green) . ' '
// @phpstan-ignore-next-line
. \str_repeat(' ', $width - $lengths[$name])
. $command->getDescription()
);
}
}
}

/**
* @param array<string,Command> $commands
*
* @return array<array<string,int>|int>
*/
protected function getWidthAndLengths(array $commands) : array
{
$width = 0;
$lengths = [];
foreach (\array_keys($commands) as $name) {
$lengths[$name] = \mb_strlen($name);
if ($lengths[$name] > $width) {
$width = $lengths[$name];
}
}
return [$width, $lengths];
}

protected function showHeader() : void
Expand Down
17 changes: 17 additions & 0 deletions tests/Commands/IndexTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,4 +38,21 @@ public function testOptionGreet() : void
$console->exec('index -g');
self::assertStringContainsString('Good ', Stdout::getContents());
}

public function testManyGroups() : void
{
$console = new Console();
$foo = new Foo();
$foo->setName('fooCommand')->setGroup('Group 2');
$bar = new Foo();
$bar->setName('barCommand')->setGroup('Group 1');
$console->addCommand($foo)->addCommand($bar);
Stdout::init();
$console->exec('index');
self::assertStringContainsString('Group 2', Stdout::getContents());
self::assertStringContainsString('Group 1', Stdout::getContents());
$group2Pos = \strpos(Stdout::getContents(), 'Group 2');
$group1Pos = \strpos(Stdout::getContents(), 'Group 1');
self::assertTrue($group2Pos > $group1Pos);
}
}

0 comments on commit b3ab8b0

Please sign in to comment.