From bcaef59a1551c40363317957af866b55c2f4ce91 Mon Sep 17 00:00:00 2001 From: crazywhalecc Date: Tue, 9 Dec 2025 16:54:29 +0800 Subject: [PATCH] Support full --no-ansi options --- src/StaticPHP/Command/BaseCommand.php | 3 +- src/StaticPHP/Exception/ExceptionHandler.php | 3 +- src/StaticPHP/Util/InteractiveTerm.php | 32 +++++++++++++++----- src/globals/functions.php | 4 +-- 4 files changed, 30 insertions(+), 12 deletions(-) diff --git a/src/StaticPHP/Command/BaseCommand.php b/src/StaticPHP/Command/BaseCommand.php index b6c09f3f..da01723a 100644 --- a/src/StaticPHP/Command/BaseCommand.php +++ b/src/StaticPHP/Command/BaseCommand.php @@ -70,7 +70,8 @@ abstract class BaseCommand extends Command }); $version = $this->getVersionWithCommit(); 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; } } diff --git a/src/StaticPHP/Exception/ExceptionHandler.php b/src/StaticPHP/Exception/ExceptionHandler.php index db11b8fd..a7732763 100644 --- a/src/StaticPHP/Exception/ExceptionHandler.php +++ b/src/StaticPHP/Exception/ExceptionHandler.php @@ -10,6 +10,7 @@ use SPC\builder\linux\LinuxBuilder; use SPC\builder\macos\MacOSBuilder; use SPC\builder\windows\WindowsBuilder; use StaticPHP\DI\ApplicationContext; +use StaticPHP\Util\InteractiveTerm; use ZM\Logger\ConsoleColor; class ExceptionHandler @@ -189,7 +190,7 @@ class ExceptionHandler $line = str_pad($v, strlen($v) + $indent_space, ' ', STR_PAD_LEFT); fwrite($spc_log, strip_ansi_colors($line) . PHP_EOL); if ($output_log) { - echo ConsoleColor::red($line) . PHP_EOL; + InteractiveTerm::plain(ConsoleColor::red($line) . ''); } } } diff --git a/src/StaticPHP/Util/InteractiveTerm.php b/src/StaticPHP/Util/InteractiveTerm.php index 01e4bdc9..47932763 100644 --- a/src/StaticPHP/Util/InteractiveTerm.php +++ b/src/StaticPHP/Util/InteractiveTerm.php @@ -6,6 +6,7 @@ namespace StaticPHP\Util; use StaticPHP\DI\ApplicationContext; use Symfony\Component\Console\Helper\ProgressIndicator; +use Symfony\Component\Console\Input\InputInterface; use Symfony\Component\Console\Output\OutputInterface; use ZM\Logger\ConsoleColor; @@ -15,50 +16,55 @@ class InteractiveTerm 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); if ($output->isVerbose()) { logger()->notice(strip_ansi_colors($message)); } 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 { + $no_ansi = ApplicationContext::get(InputInterface::class)->getOption('no-ansi') ?? false; $output = ApplicationContext::get(OutputInterface::class); if ($output->isVerbose()) { logger()->info(strip_ansi_colors($message)); } 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 { + $no_ansi = ApplicationContext::get(InputInterface::class)->getOption('no-ansi') ?? false; $output = ApplicationContext::get(OutputInterface::class); if ($output->isVerbose()) { logger()->info(strip_ansi_colors($message)); } else { - $output->writeln($message); + $output->writeln(($no_ansi ? 'strip_ansi_colors' : 'strval')($message)); } } public static function info(string $message): void { + $no_ansi = ApplicationContext::get(InputInterface::class)->getOption('no-ansi') ?? false; $output = ApplicationContext::get(OutputInterface::class); 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)); } 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); if ($output->isVerbose()) { logger()->error(strip_ansi_colors($message)); } 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 { - 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 { + $no_ansi = ApplicationContext::get(InputInterface::class)->getOption('no-ansi') ?? false; + $message = $no_ansi ? strip_ansi_colors($message) : $message; $output = ApplicationContext::get(OutputInterface::class); if ($output->isVerbose()) { if ($status) { @@ -85,9 +94,9 @@ class InteractiveTerm } if (self::$indicator !== null) { if (!$status) { - self::$indicator->finish($message, '' . ConsoleColor::red(' ✘')); + self::$indicator->finish($message, ($no_ansi ? 'strip_ansi_colors' : 'strval')(ConsoleColor::red(' ✘'))); } else { - self::$indicator->finish($message, '' . ConsoleColor::green(' ✔')); + self::$indicator->finish($message, ($no_ansi ? 'strip_ansi_colors' : 'strval')(ConsoleColor::green(' ✔'))); } self::$indicator = null; } @@ -95,6 +104,7 @@ class InteractiveTerm public static function indicateProgress(string $message): void { + $no_ansi = ApplicationContext::get(InputInterface::class)->getOption('no-ansi') ?? false; $output = ApplicationContext::get(OutputInterface::class); if ($output->isVerbose()) { logger()->info(strip_ansi_colors($message)); @@ -106,6 +116,12 @@ class InteractiveTerm self::$indicator->advance(); 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->start($message); } diff --git a/src/globals/functions.php b/src/globals/functions.php index 8621e7ad..93cd1ae0 100644 --- a/src/globals/functions.php +++ b/src/globals/functions.php @@ -271,11 +271,11 @@ function keyboard_interrupt_unregister(): void /** * 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 // 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)); } /**