Skip to content

Commit 4e0e133

Browse files
committed
[cli] give clean output
1 parent 3219f6d commit 4e0e133

File tree

3 files changed

+95
-1
lines changed

3 files changed

+95
-1
lines changed
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Rector\Jack\Console\Command;
6+
7+
use Symfony\Component\Console\Application;
8+
use Symfony\Component\Console\Command\Command;
9+
use Symfony\Component\Console\Command\ListCommand;
10+
use Symfony\Component\Console\Descriptor\ApplicationDescription;
11+
use Symfony\Component\Console\Input\InputInterface;
12+
use Symfony\Component\Console\Output\OutputInterface;
13+
use Webmozart\Assert\Assert;
14+
15+
/**
16+
* Simple command list, without bloated options
17+
*/
18+
final class CleanListCommand extends ListCommand
19+
{
20+
protected function execute(InputInterface $input, OutputInterface $output): int
21+
{
22+
Assert::isInstanceOf($this->getApplication(), Application::class);
23+
24+
$output->writeln($this->getApplication()->getName());
25+
$output->writeln('');
26+
$output->writeln('<comment>Available commands:</>');
27+
28+
$applicationDescription = new ApplicationDescription($this->getApplication());
29+
$this->describeCommands($applicationDescription, $output);
30+
31+
return self::SUCCESS;
32+
}
33+
34+
/**
35+
* @param non-empty-array<Command> $commands
36+
*/
37+
private function resolveCommandNameColumnWidth(array $commands): int
38+
{
39+
$commandNameLengths = [];
40+
foreach ($commands as $command) {
41+
$commandNameLengths[] = strlen((string) $command->getName());
42+
}
43+
44+
return max($commandNameLengths) + 4;
45+
}
46+
47+
private function describeCommands(ApplicationDescription $applicationDescription, OutputInterface $output): void
48+
{
49+
if ($applicationDescription->getCommands() === []) {
50+
return;
51+
}
52+
53+
$commands = $applicationDescription->getCommands();
54+
$commandNameColumnWidth = $this->resolveCommandNameColumnWidth($commands);
55+
56+
foreach ($commands as $command) {
57+
$spacingWidth = $commandNameColumnWidth - strlen((string) $command->getName());
58+
59+
$output->writeln(sprintf(
60+
' <info>%s</>%s%s',
61+
$command->getName(),
62+
str_repeat(' ', $spacingWidth),
63+
$command->getDescription()
64+
));
65+
}
66+
}
67+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Rector\Jack\Console;
6+
7+
use Rector\Jack\Console\Command\CleanListCommand;
8+
use Symfony\Component\Console\Application;
9+
use Symfony\Component\Console\Command\CompleteCommand;
10+
use Symfony\Component\Console\Command\DumpCompletionCommand;
11+
use Symfony\Component\Console\Command\HelpCommand;
12+
13+
final class JackConsoleApplication extends Application
14+
{
15+
protected function getDefaultCommands(): array
16+
{
17+
return [
18+
new HelpCommand(),
19+
new CompleteCommand(),
20+
new DumpCompletionCommand(),
21+
22+
// clean list, without bloated options
23+
new CleanListCommand(),
24+
];
25+
}
26+
}

src/DependencyInjection/ContainerFactory.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
namespace Rector\Jack\DependencyInjection;
66

77
use Illuminate\Container\Container;
8+
use Rector\Jack\Console\JackConsoleApplication;
89
use Symfony\Component\Console\Application;
910
use Symfony\Component\Console\Input\ArrayInput;
1011
use Symfony\Component\Console\Output\ConsoleOutput;
@@ -23,7 +24,7 @@ public function create(): Container
2324

2425
// console
2526
$container->singleton(Application::class, function (Container $container): Application {
26-
$application = new Application('Rector Jack');
27+
$application = new JackConsoleApplication('Rector Jack');
2728

2829
$commandClasses = $this->findCommandClasses();
2930

0 commit comments

Comments
 (0)