Skip to content

Commit bcaef59

Browse files
committed
Support full --no-ansi options
1 parent b0f630f commit bcaef59

File tree

4 files changed

+30
-12
lines changed

4 files changed

+30
-12
lines changed

src/StaticPHP/Command/BaseCommand.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,8 @@ public function initialize(InputInterface $input, OutputInterface $output): void
7070
});
7171
$version = $this->getVersionWithCommit();
7272
if (!$this->no_motd) {
73-
echo str_replace('{version}', '' . ConsoleColor::none("v{$version}"), '' . ConsoleColor::magenta(self::$motd));
73+
$str = str_replace('{version}', '' . ConsoleColor::none("v{$version}"), '' . ConsoleColor::magenta(self::$motd));
74+
echo $this->input->getOption('no-ansi') ? strip_ansi_colors($str) : $str;
7475
}
7576
}
7677

src/StaticPHP/Exception/ExceptionHandler.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
use SPC\builder\macos\MacOSBuilder;
1111
use SPC\builder\windows\WindowsBuilder;
1212
use StaticPHP\DI\ApplicationContext;
13+
use StaticPHP\Util\InteractiveTerm;
1314
use ZM\Logger\ConsoleColor;
1415

1516
class ExceptionHandler
@@ -189,7 +190,7 @@ private static function logError($message, int $indent_space = 0, bool $output_l
189190
$line = str_pad($v, strlen($v) + $indent_space, ' ', STR_PAD_LEFT);
190191
fwrite($spc_log, strip_ansi_colors($line) . PHP_EOL);
191192
if ($output_log) {
192-
echo ConsoleColor::red($line) . PHP_EOL;
193+
InteractiveTerm::plain(ConsoleColor::red($line) . '');
193194
}
194195
}
195196
}

src/StaticPHP/Util/InteractiveTerm.php

Lines changed: 24 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
use StaticPHP\DI\ApplicationContext;
88
use Symfony\Component\Console\Helper\ProgressIndicator;
9+
use Symfony\Component\Console\Input\InputInterface;
910
use Symfony\Component\Console\Output\OutputInterface;
1011
use 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
}

src/globals/functions.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -271,11 +271,11 @@ function keyboard_interrupt_unregister(): void
271271
/**
272272
* Strip ANSI color codes from a string.
273273
*/
274-
function strip_ansi_colors(string $text): string
274+
function strip_ansi_colors(string|Stringable $text): string
275275
{
276276
// Regular expression to match ANSI escape sequences
277277
// Including color codes, cursor control, clear screen and other control sequences
278-
return preg_replace('/\e\[[0-9;]*[a-zA-Z]/', '', $text);
278+
return preg_replace('/\e\[[0-9;]*[a-zA-Z]/', '', strval($text));
279279
}
280280

281281
/**

0 commit comments

Comments
 (0)