Support full --no-ansi options

This commit is contained in:
crazywhalecc
2025-12-09 16:54:29 +08:00
parent b0f630f95f
commit bcaef59a15
4 changed files with 30 additions and 12 deletions

View File

@@ -70,7 +70,8 @@ abstract class BaseCommand extends Command
}); });
$version = $this->getVersionWithCommit(); $version = $this->getVersionWithCommit();
if (!$this->no_motd) { if (!$this->no_motd) {
echo str_replace('{version}', '' . ConsoleColor::none("v{$version}"), '' . ConsoleColor::magenta(self::$motd)); $str = str_replace('{version}', '' . ConsoleColor::none("v{$version}"), '' . ConsoleColor::magenta(self::$motd));
echo $this->input->getOption('no-ansi') ? strip_ansi_colors($str) : $str;
} }
} }

View File

@@ -10,6 +10,7 @@ use SPC\builder\linux\LinuxBuilder;
use SPC\builder\macos\MacOSBuilder; use SPC\builder\macos\MacOSBuilder;
use SPC\builder\windows\WindowsBuilder; use SPC\builder\windows\WindowsBuilder;
use StaticPHP\DI\ApplicationContext; use StaticPHP\DI\ApplicationContext;
use StaticPHP\Util\InteractiveTerm;
use ZM\Logger\ConsoleColor; use ZM\Logger\ConsoleColor;
class ExceptionHandler class ExceptionHandler
@@ -189,7 +190,7 @@ class ExceptionHandler
$line = str_pad($v, strlen($v) + $indent_space, ' ', STR_PAD_LEFT); $line = str_pad($v, strlen($v) + $indent_space, ' ', STR_PAD_LEFT);
fwrite($spc_log, strip_ansi_colors($line) . PHP_EOL); fwrite($spc_log, strip_ansi_colors($line) . PHP_EOL);
if ($output_log) { if ($output_log) {
echo ConsoleColor::red($line) . PHP_EOL; InteractiveTerm::plain(ConsoleColor::red($line) . '');
} }
} }
} }

View File

@@ -6,6 +6,7 @@ namespace StaticPHP\Util;
use StaticPHP\DI\ApplicationContext; use StaticPHP\DI\ApplicationContext;
use Symfony\Component\Console\Helper\ProgressIndicator; use Symfony\Component\Console\Helper\ProgressIndicator;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface; use Symfony\Component\Console\Output\OutputInterface;
use ZM\Logger\ConsoleColor; use ZM\Logger\ConsoleColor;
@@ -15,50 +16,55 @@ class InteractiveTerm
public static function notice(string $message, bool $indent = false): void public static function notice(string $message, bool $indent = false): void
{ {
$no_ansi = ApplicationContext::get(InputInterface::class)->getOption('no-ansi') ?? false;
$output = ApplicationContext::get(OutputInterface::class); $output = ApplicationContext::get(OutputInterface::class);
if ($output->isVerbose()) { if ($output->isVerbose()) {
logger()->notice(strip_ansi_colors($message)); logger()->notice(strip_ansi_colors($message));
} else { } else {
$output->writeln(ConsoleColor::cyan(($indent ? ' ' : '') . '▶ ') . $message); $output->writeln(($no_ansi ? 'strip_ansi_colors' : 'strval')(ConsoleColor::cyan(($indent ? ' ' : '') . '▶ ') . $message));
} }
} }
public static function success(string $message, bool $indent = false): void public static function success(string $message, bool $indent = false): void
{ {
$no_ansi = ApplicationContext::get(InputInterface::class)->getOption('no-ansi') ?? false;
$output = ApplicationContext::get(OutputInterface::class); $output = ApplicationContext::get(OutputInterface::class);
if ($output->isVerbose()) { if ($output->isVerbose()) {
logger()->info(strip_ansi_colors($message)); logger()->info(strip_ansi_colors($message));
} else { } else {
$output->writeln(ConsoleColor::green(($indent ? ' ' : '') . '✔ ') . $message); $output->writeln(($no_ansi ? 'strip_ansi_colors' : 'strval')(ConsoleColor::green(($indent ? ' ' : '') . '✔ ') . $message));
} }
} }
public static function plain(string $message): void public static function plain(string $message): void
{ {
$no_ansi = ApplicationContext::get(InputInterface::class)->getOption('no-ansi') ?? false;
$output = ApplicationContext::get(OutputInterface::class); $output = ApplicationContext::get(OutputInterface::class);
if ($output->isVerbose()) { if ($output->isVerbose()) {
logger()->info(strip_ansi_colors($message)); logger()->info(strip_ansi_colors($message));
} else { } else {
$output->writeln($message); $output->writeln(($no_ansi ? 'strip_ansi_colors' : 'strval')($message));
} }
} }
public static function info(string $message): void public static function info(string $message): void
{ {
$no_ansi = ApplicationContext::get(InputInterface::class)->getOption('no-ansi') ?? false;
$output = ApplicationContext::get(OutputInterface::class); $output = ApplicationContext::get(OutputInterface::class);
if (!$output->isVerbose()) { if (!$output->isVerbose()) {
$output->writeln(ConsoleColor::green('▶ ') . $message); $output->writeln(($no_ansi ? 'strip_ansi_colors' : 'strval')(ConsoleColor::green('▶ ') . $message));
} }
logger()->info(strip_ansi_colors($message)); logger()->info(strip_ansi_colors($message));
} }
public static function error(string $message, bool $indent = true): void public static function error(string $message, bool $indent = true): void
{ {
$no_ansi = ApplicationContext::get(InputInterface::class)->getOption('no-ansi') ?? false;
$output = ApplicationContext::get(OutputInterface::class); $output = ApplicationContext::get(OutputInterface::class);
if ($output->isVerbose()) { if ($output->isVerbose()) {
logger()->error(strip_ansi_colors($message)); logger()->error(strip_ansi_colors($message));
} else { } else {
$output->writeln('' . ConsoleColor::red(($indent ? ' ' : '') . '✘ ' . $message)); $output->writeln(($no_ansi ? 'strip_ansi_colors' : 'strval')(ConsoleColor::red(($indent ? ' ' : '') . '✘ ' . $message)));
} }
} }
@@ -69,11 +75,14 @@ class InteractiveTerm
public static function setMessage(string $message): void public static function setMessage(string $message): void
{ {
self::$indicator?->setMessage($message); $no_ansi = ApplicationContext::get(InputInterface::class)->getOption('no-ansi') ?? false;
self::$indicator?->setMessage(($no_ansi ? 'strip_ansi_colors' : 'strval')($message));
} }
public static function finish(string $message, bool $status = true): void public static function finish(string $message, bool $status = true): void
{ {
$no_ansi = ApplicationContext::get(InputInterface::class)->getOption('no-ansi') ?? false;
$message = $no_ansi ? strip_ansi_colors($message) : $message;
$output = ApplicationContext::get(OutputInterface::class); $output = ApplicationContext::get(OutputInterface::class);
if ($output->isVerbose()) { if ($output->isVerbose()) {
if ($status) { if ($status) {
@@ -85,9 +94,9 @@ class InteractiveTerm
} }
if (self::$indicator !== null) { if (self::$indicator !== null) {
if (!$status) { if (!$status) {
self::$indicator->finish($message, '' . ConsoleColor::red(' ✘')); self::$indicator->finish($message, ($no_ansi ? 'strip_ansi_colors' : 'strval')(ConsoleColor::red(' ✘')));
} else { } else {
self::$indicator->finish($message, '' . ConsoleColor::green(' ✔')); self::$indicator->finish($message, ($no_ansi ? 'strip_ansi_colors' : 'strval')(ConsoleColor::green(' ✔')));
} }
self::$indicator = null; self::$indicator = null;
} }
@@ -95,6 +104,7 @@ class InteractiveTerm
public static function indicateProgress(string $message): void public static function indicateProgress(string $message): void
{ {
$no_ansi = ApplicationContext::get(InputInterface::class)->getOption('no-ansi') ?? false;
$output = ApplicationContext::get(OutputInterface::class); $output = ApplicationContext::get(OutputInterface::class);
if ($output->isVerbose()) { if ($output->isVerbose()) {
logger()->info(strip_ansi_colors($message)); logger()->info(strip_ansi_colors($message));
@@ -106,6 +116,12 @@ class InteractiveTerm
self::$indicator->advance(); self::$indicator->advance();
return; return;
} }
// if no ansi, use a dot instead of spinner
if ($no_ansi) {
self::$indicator = new ProgressIndicator(ApplicationContext::get(OutputInterface::class), 'verbose', 100, [' •', ' •']);
self::$indicator->start(strip_ansi_colors($message));
return;
}
self::$indicator = new ProgressIndicator(ApplicationContext::get(OutputInterface::class), 'verbose', 100, [' ⠏', ' ⠛', ' ⠹', ' ⢸', ' ⣰', ' ⣤', ' ⣆', ' ⡇']); self::$indicator = new ProgressIndicator(ApplicationContext::get(OutputInterface::class), 'verbose', 100, [' ⠏', ' ⠛', ' ⠹', ' ⢸', ' ⣰', ' ⣤', ' ⣆', ' ⡇']);
self::$indicator->start($message); self::$indicator->start($message);
} }

View File

@@ -271,11 +271,11 @@ function keyboard_interrupt_unregister(): void
/** /**
* Strip ANSI color codes from a string. * Strip ANSI color codes from a string.
*/ */
function strip_ansi_colors(string $text): string function strip_ansi_colors(string|Stringable $text): string
{ {
// Regular expression to match ANSI escape sequences // Regular expression to match ANSI escape sequences
// Including color codes, cursor control, clear screen and other control sequences // Including color codes, cursor control, clear screen and other control sequences
return preg_replace('/\e\[[0-9;]*[a-zA-Z]/', '', $text); return preg_replace('/\e\[[0-9;]*[a-zA-Z]/', '', strval($text));
} }
/** /**