1212
1313final readonly class RenderConsoleCommand
1414{
15- public function __construct (private Console $ console )
16- {
15+ public function __construct (
16+ private Console $ console ,
17+ private ?int $ longestCommandName = null ,
18+ private bool $ renderArguments = false ,
19+ private bool $ renderDescription = true ,
20+ ) {
1721 }
1822
1923 public function __invoke (ConsoleCommand $ consoleCommand ): void
2024 {
21- $ parts = [" <em><strong> { $ consoleCommand -> getName ()} </strong></em> " ];
25+ $ parts = [$ this -> renderName ( $ consoleCommand ) ];
2226
23- foreach ($ consoleCommand ->getArgumentDefinitions () as $ argument ) {
24- $ parts [] = $ this ->renderArgument ($ argument );
27+ if ($ this ->renderArguments ) {
28+ foreach ($ consoleCommand ->getArgumentDefinitions () as $ argument ) {
29+ $ parts [] = '<style="fg-gray"> ' . $ this ->renderArgument ($ argument ) . '</style> ' ;
30+ }
2531 }
2632
27- if ($ consoleCommand ->description !== null && $ consoleCommand ->description !== '' ) {
28- $ parts [] = "- {$ consoleCommand ->description }" ;
33+ if ($ this ->renderDescription ) {
34+ if ($ consoleCommand ->description !== null && $ consoleCommand ->description !== '' ) {
35+ $ parts [] = $ consoleCommand ->description ;
36+ }
2937 }
3038
31- $ this ->console ->writeln (' ' . implode (' ' , $ parts ));
39+ $ this ->console ->writeln (implode (' ' , $ parts ));
40+ }
41+
42+ private function renderName (ConsoleCommand $ consoleCommand ): string
43+ {
44+ return str ($ consoleCommand ->getName ())
45+ ->alignRight ($ this ->longestCommandName , padding: $ this ->longestCommandName ? 2 : 0 )
46+ ->wrap (before: '<style="fg-cyan"> ' , after: '</style> ' )
47+ ->toString ();
3248 }
3349
3450 private function renderArgument (ConsoleArgumentDefinition $ argument ): string
@@ -37,26 +53,32 @@ private function renderArgument(ConsoleArgumentDefinition $argument): string
3753 return $ this ->renderEnumArgument ($ argument );
3854 }
3955
40- $ name = str ($ argument ->name )
41- ->prepend ('<em> ' )
42- ->append ('</em> ' );
43-
44- $ asString = match ($ argument ->type ) {
45- 'bool ' => "<em>--</em> {$ name }" ,
46- default => $ name ,
56+ $ formattedArgumentName = match ($ argument ->type ) {
57+ 'bool ' => "-- {$ argument ->name }" ,
58+ default => $ argument ->name ,
4759 };
4860
61+ $ formattedArgumentName = str ($ formattedArgumentName )->wrap ('<style="fg-cyan"> ' , '</style> ' );
62+
4963 if (! $ argument ->hasDefault ) {
50- return " < { $ asString } > " ;
64+ return $ formattedArgumentName -> wrap ( ' <style="fg-gray dim"><</style> ' , ' <style="fg-gray dim">></style> ' )-> toString () ;
5165 }
5266
53- return match (true ) {
54- $ argument ->default === true => "[ { $ asString } = true] " ,
55- $ argument ->default === false => "[ { $ asString } = false] " ,
56- is_null ($ argument ->default ) => "[ { $ asString } = null] " ,
57- is_array ($ argument ->default ) => "[ { $ asString } = array] " ,
58- default => "[ { $ asString } = { $ argument ->default } ] "
67+ $ defaultValue = match (true ) {
68+ $ argument ->default === true => "true " ,
69+ $ argument ->default === false => "false " ,
70+ is_null ($ argument ->default ) => "null " ,
71+ is_array ($ argument ->default ) => "array " ,
72+ default => "$ argument ->default " ,
5973 };
74+
75+ return str ()
76+ ->append (str ('[ ' )->wrap ('<style="fg-gray dim"> ' , '</style> ' ))
77+ ->append ($ formattedArgumentName )
78+ ->append (str ('= ' )->wrap ('<style="fg-gray dim"> ' , '</style> ' ))
79+ ->append (str ($ defaultValue )->wrap ('<style="fg-gray"> ' , '</style> ' ))
80+ ->append (str ('] ' )->wrap ('<style="fg-gray dim"> ' , '</style> ' ))
81+ ->toString ();
6082 }
6183
6284 private function renderEnumArgument (ConsoleArgumentDefinition $ argument ): string
@@ -66,8 +88,8 @@ private function renderEnumArgument(ConsoleArgumentDefinition $argument): string
6688 array: $ argument ->type ::cases ()
6789 );
6890
69- $ partsAsString = ' {<em > ' . implode ('| ' , $ parts ) . '</em >} ' ;
70- $ line = "<em > {$ argument ->name }</em > " ;
91+ $ partsAsString = ' {<style="fg-cyan" > ' . implode ('| ' , $ parts ) . '</style >} ' ;
92+ $ line = "<style= \" fg-cyan \" > {$ argument ->name }</style > " ;
7193
7294 if ($ argument ->hasDefault ) {
7395 return "[ {$ line }= {$ argument ->default ->value }{$ partsAsString }] " ;
0 commit comments