66
77use StaticPHP \DI \ApplicationContext ;
88use Symfony \Component \Console \Helper \ProgressIndicator ;
9+ use Symfony \Component \Console \Input \InputInterface ;
910use Symfony \Component \Console \Output \OutputInterface ;
1011use ZM \Logger \ConsoleColor ;
1112
@@ -15,50 +16,55 @@ class InteractiveTerm
1516
1617 public static function notice (string $ message , bool $ indent = false ): void
1718 {
19+ $ no_ansi = ApplicationContext::get (InputInterface::class)->getOption ('no-ansi ' ) ?? false ;
1820 $ output = ApplicationContext::get (OutputInterface::class);
1921 if ($ output ->isVerbose ()) {
2022 logger ()->notice (strip_ansi_colors ($ message ));
2123 } else {
22- $ output ->writeln (ConsoleColor::cyan (($ indent ? ' ' : '' ) . '▶ ' ) . $ message );
24+ $ output ->writeln (( $ no_ansi ? ' strip_ansi_colors ' : ' strval ' )( ConsoleColor::cyan (($ indent ? ' ' : '' ) . '▶ ' ) . $ message) );
2325 }
2426 }
2527
2628 public static function success (string $ message , bool $ indent = false ): void
2729 {
30+ $ no_ansi = ApplicationContext::get (InputInterface::class)->getOption ('no-ansi ' ) ?? false ;
2831 $ output = ApplicationContext::get (OutputInterface::class);
2932 if ($ output ->isVerbose ()) {
3033 logger ()->info (strip_ansi_colors ($ message ));
3134 } else {
32- $ output ->writeln (ConsoleColor::green (($ indent ? ' ' : '' ) . '✔ ' ) . $ message );
35+ $ output ->writeln (( $ no_ansi ? ' strip_ansi_colors ' : ' strval ' )( ConsoleColor::green (($ indent ? ' ' : '' ) . '✔ ' ) . $ message) );
3336 }
3437 }
3538
3639 public static function plain (string $ message ): void
3740 {
41+ $ no_ansi = ApplicationContext::get (InputInterface::class)->getOption ('no-ansi ' ) ?? false ;
3842 $ output = ApplicationContext::get (OutputInterface::class);
3943 if ($ output ->isVerbose ()) {
4044 logger ()->info (strip_ansi_colors ($ message ));
4145 } else {
42- $ output ->writeln ($ message );
46+ $ output ->writeln (( $ no_ansi ? ' strip_ansi_colors ' : ' strval ' )( $ message) );
4347 }
4448 }
4549
4650 public static function info (string $ message ): void
4751 {
52+ $ no_ansi = ApplicationContext::get (InputInterface::class)->getOption ('no-ansi ' ) ?? false ;
4853 $ output = ApplicationContext::get (OutputInterface::class);
4954 if (!$ output ->isVerbose ()) {
50- $ output ->writeln (ConsoleColor::green ('▶ ' ) . $ message );
55+ $ output ->writeln (( $ no_ansi ? ' strip_ansi_colors ' : ' strval ' )( ConsoleColor::green ('▶ ' ) . $ message) );
5156 }
5257 logger ()->info (strip_ansi_colors ($ message ));
5358 }
5459
5560 public static function error (string $ message , bool $ indent = true ): void
5661 {
62+ $ no_ansi = ApplicationContext::get (InputInterface::class)->getOption ('no-ansi ' ) ?? false ;
5763 $ output = ApplicationContext::get (OutputInterface::class);
5864 if ($ output ->isVerbose ()) {
5965 logger ()->error (strip_ansi_colors ($ message ));
6066 } else {
61- $ output ->writeln ('' . ConsoleColor::red (($ indent ? ' ' : '' ) . '✘ ' . $ message ));
67+ $ output ->writeln (( $ no_ansi ? ' strip_ansi_colors ' : ' strval ' )( ConsoleColor::red (($ indent ? ' ' : '' ) . '✘ ' . $ message) ));
6268 }
6369 }
6470
@@ -69,11 +75,14 @@ public static function advance(): void
6975
7076 public static function setMessage (string $ message ): void
7177 {
72- self ::$ indicator ?->setMessage($ message );
78+ $ no_ansi = ApplicationContext::get (InputInterface::class)->getOption ('no-ansi ' ) ?? false ;
79+ self ::$ indicator ?->setMessage(($ no_ansi ? 'strip_ansi_colors ' : 'strval ' )($ message ));
7380 }
7481
7582 public static function finish (string $ message , bool $ status = true ): void
7683 {
84+ $ no_ansi = ApplicationContext::get (InputInterface::class)->getOption ('no-ansi ' ) ?? false ;
85+ $ message = $ no_ansi ? strip_ansi_colors ($ message ) : $ message ;
7786 $ output = ApplicationContext::get (OutputInterface::class);
7887 if ($ output ->isVerbose ()) {
7988 if ($ status ) {
@@ -85,16 +94,17 @@ public static function finish(string $message, bool $status = true): void
8594 }
8695 if (self ::$ indicator !== null ) {
8796 if (!$ status ) {
88- self ::$ indicator ->finish ($ message , '' . ConsoleColor::red (' ✘ ' ));
97+ self ::$ indicator ->finish ($ message , ( $ no_ansi ? ' strip_ansi_colors ' : ' strval ' )( ConsoleColor::red (' ✘ ' ) ));
8998 } else {
90- self ::$ indicator ->finish ($ message , '' . ConsoleColor::green (' ✔ ' ));
99+ self ::$ indicator ->finish ($ message , ( $ no_ansi ? ' strip_ansi_colors ' : ' strval ' )( ConsoleColor::green (' ✔ ' ) ));
91100 }
92101 self ::$ indicator = null ;
93102 }
94103 }
95104
96105 public static function indicateProgress (string $ message ): void
97106 {
107+ $ no_ansi = ApplicationContext::get (InputInterface::class)->getOption ('no-ansi ' ) ?? false ;
98108 $ output = ApplicationContext::get (OutputInterface::class);
99109 if ($ output ->isVerbose ()) {
100110 logger ()->info (strip_ansi_colors ($ message ));
@@ -106,6 +116,12 @@ public static function indicateProgress(string $message): void
106116 self ::$ indicator ->advance ();
107117 return ;
108118 }
119+ // if no ansi, use a dot instead of spinner
120+ if ($ no_ansi ) {
121+ self ::$ indicator = new ProgressIndicator (ApplicationContext::get (OutputInterface::class), 'verbose ' , 100 , [' • ' , ' • ' ]);
122+ self ::$ indicator ->start (strip_ansi_colors ($ message ));
123+ return ;
124+ }
109125 self ::$ indicator = new ProgressIndicator (ApplicationContext::get (OutputInterface::class), 'verbose ' , 100 , [' ⠏ ' , ' ⠛ ' , ' ⠹ ' , ' ⢸ ' , ' ⣰ ' , ' ⣤ ' , ' ⣆ ' , ' ⡇ ' ]);
110126 self ::$ indicator ->start ($ message );
111127 }
0 commit comments