From b3ab8b08567c2af18f8d59917f22e254237d4aa1 Mon Sep 17 00:00:00 2001 From: Natan Felles Date: Mon, 13 May 2024 17:15:25 -0300 Subject: [PATCH] Allows showing commands separated by groups --- src/Commands/Index.php | 52 ++++++++++++++++++++++++++++++------ tests/Commands/IndexTest.php | 17 ++++++++++++ 2 files changed, 61 insertions(+), 8 deletions(-) diff --git a/src/Commands/Index.php b/src/Commands/Index.php index 2782689..d35416a 100644 --- a/src/Commands/Index.php +++ b/src/Commands/Index.php @@ -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 $commands + * + * @return array|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 diff --git a/tests/Commands/IndexTest.php b/tests/Commands/IndexTest.php index 0b511d3..6a0e81b 100644 --- a/tests/Commands/IndexTest.php +++ b/tests/Commands/IndexTest.php @@ -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); + } }