From ebdf7c51e873d53bd16002d788ba8a8b81da60d4 Mon Sep 17 00:00:00 2001 From: Ioannis Igoumenos Date: Fri, 17 Oct 2025 20:00:48 +0300 Subject: [PATCH] Fix verbosity print --- .../src/Command/TransmogrifyCommand.php | 22 ++-- .../src/Lib/Util/CommandLinePrinter.php | 104 ++++++++++++------ 2 files changed, 81 insertions(+), 45 deletions(-) diff --git a/app/plugins/Transmogrify/src/Command/TransmogrifyCommand.php b/app/plugins/Transmogrify/src/Command/TransmogrifyCommand.php index 24a5ecaae..9bd4daaa8 100644 --- a/app/plugins/Transmogrify/src/Command/TransmogrifyCommand.php +++ b/app/plugins/Transmogrify/src/Command/TransmogrifyCommand.php @@ -280,7 +280,7 @@ public function execute(Arguments $args, ConsoleIo $io): int $outboundQualifiedTableName = $this->outconn->qualifyTableName($t); $Model = TableRegistry::getTableLocator()->get($t); - $this->cmdPrinter->info(message: sprintf("Transmogrifying table: %s(%s)", Inflector::classify($t), $t)); + $this->cmdPrinter->out(message: sprintf("Transmogrifying table: %s(%s)", Inflector::classify($t), $t)); /* @@ -372,8 +372,6 @@ public function execute(Arguments $args, ConsoleIo $io): int $origRow = $row; // Execute any pre-processing hooks to transform or validate the row data - // TODO: if i need to skip the insert i want something shared. I can add this in the cache - // and then skip insert from everywhere. $this->runPreRowHook($t, $origRow, $row); // Set changelog defaults (created/modified timestamps, user IDs) @@ -412,7 +410,7 @@ public function execute(Arguments $args, ConsoleIo $io): int $warns++; $this->cache['rejected'][$outboundQualifiedTableName][$row['id']] = $row; $this->cmdPrinter->warn("Skipping $t record " . $row['id'] . " due to invalid foreign key: " . $e->getMessage()); -// $this->cmdPrinter->pause(); + $this->cmdPrinter->pause(); } catch(\InvalidArgumentException $e) { // If we can't find a value for mapping we skip the record @@ -420,19 +418,17 @@ public function execute(Arguments $args, ConsoleIo $io): int $warns++; $this->cache['rejected'][$outboundQualifiedTableName][$row['id']] = $row; $this->cmdPrinter->warn("Skipping $t record " . $row['id'] . ": " . $e->getMessage()); -// $this->cmdPrinter->pause(); + $this->cmdPrinter->pause(); } catch(\Exception $e) { $err++; $this->cmdPrinter->error("$t record " . $row['id'] . ": " . $e->getMessage()); -// $this->cmdPrinter->pause(); + $this->cmdPrinter->pause(); } $tally++; - - if (!$this->args->getOption('quiet') && !$this->args->getOption('verbose')) { - $this->cmdPrinter->update($tally); - } + // Always delegate progress updates to the printer; it will decide what to draw + $this->cmdPrinter->update($tally); } $this->cmdPrinter->finish(); @@ -446,7 +442,7 @@ public function execute(Arguments $args, ConsoleIo $io): int // Execute any post-processing hooks for the table if($this->cache['skipInsert'][$outboundQualifiedTableName] === false) { - $this->cmdPrinter->info('Running post-table hook for ' . $t); + $this->cmdPrinter->out('Running post-table hook for ' . $t); $this->runPostTableHook($t); } @@ -454,7 +450,7 @@ public function execute(Arguments $args, ConsoleIo $io): int if (!empty($pendingSelected) && isset($pendingSelected[$t])) { unset($pendingSelected[$t]); if (empty($pendingSelected)) { - $this->cmdPrinter->info('All selected tables have been processed. Exiting.'); + $this->cmdPrinter->out('All selected tables have been processed. Exiting.'); return BaseCommand::CODE_SUCCESS; } } @@ -465,7 +461,7 @@ public function execute(Arguments $args, ConsoleIo $io): int if (isset($tables[$currentIndex + 1])) { $this->cmdPrinter->info("Next table to process: " . $tables[$currentIndex + 1]); } else { - $this->cmdPrinter->info("This is the last table to process."); + $this->cmdPrinter->out(PHP_EOL. "Table import complete. Exiting."); } $this->cmdPrinter->pause(); diff --git a/app/plugins/Transmogrify/src/Lib/Util/CommandLinePrinter.php b/app/plugins/Transmogrify/src/Lib/Util/CommandLinePrinter.php index 06ae4ff25..052d5646f 100644 --- a/app/plugins/Transmogrify/src/Lib/Util/CommandLinePrinter.php +++ b/app/plugins/Transmogrify/src/Lib/Util/CommandLinePrinter.php @@ -68,14 +68,6 @@ class CommandLinePrinter */ private bool $useColors; - /** - * Whether verbose output is enabled - * - * @var bool - * @since COmanage Registry v5.2.0 - */ - private bool $verbose; - /** * Whether the progress bar is currently active * @@ -99,9 +91,6 @@ public function __construct(?ConsoleIo $io = null, string $barColor = 'blue', in $this->barColor = in_array($barColor, ['blue', 'green'], true) ? $barColor : 'blue'; $this->barWidth = max(10, $barWidth); $this->useColors = $useColors; - // Try to detect verbose mode from ConsoleIo if available - $this->verbose = $this->detectVerboseFromIo(); - } /** @@ -114,7 +103,7 @@ public function __construct(?ConsoleIo $io = null, string $barColor = 'blue', in public function start(int $total): void { // When verbose is enabled, do not draw the progress bar at all - if ($this->verbose) { + if ($this->getVerboseLevel() > 1) { return; } @@ -149,7 +138,7 @@ public function start(int $total): void public function update(int $current): void { // When verbose is enabled, do not draw the progress bar at all - if ($this->verbose || !$this->barActive) { + if ($this->getVerboseLevel() > 1 || !$this->barActive) { return; } @@ -172,7 +161,7 @@ public function update(int $current): void public function finish(): void { // When verbose is enabled, do not draw the progress bar at all - if ($this->verbose || !$this->barActive) { + if ($this->getVerboseLevel() > 1 || !$this->barActive) { return; } @@ -195,6 +184,18 @@ public function finish(): void $this->current = 0; } + /** + * Display an out level message + * + * @param string $message Message to display + * @return void + * @since COmanage Registry v5.2.0 + */ + public function out(string $message): void + { + $this->message($message, 'out'); + } + /** * Display an info level message * @@ -262,7 +263,10 @@ public function debug(string $message): void * @return void * @since COmanage Registry v5.2.0 */ - public function verbose(string $message): void { $this->message($message, 'verbose'); } + public function verbose(string $message): void + { + $this->message($message, 'verbose'); + } /** * Display a message with the specified level @@ -275,7 +279,7 @@ public function verbose(string $message): void { $this->message($message, 'verb public function message(string $message, string $level = 'info'): void { // Suppress verbose messages unless verbose mode is enabled - if (strtolower($level) === 'verbose' && $this->verbose !== true) { + if (!$this->shouldPrintLevel($level)) { return; } @@ -373,7 +377,6 @@ public function pause(string $prompt = 'Press to continue...'): void private function colorizeLevel(string $level, string $message): string { $level = strtolower($level); - $prefix = ''; switch ($level) { case 'warn': case 'warning': @@ -388,14 +391,16 @@ private function colorizeLevel(string $level, string $message): string case 'verbose': $prefix = '[VERBOSE] '; break; - - default: + case 'info': $prefix = '[INFO] '; + break; + default: + $prefix = ''; } // For INFO, render the label (up to the first colon) in white, value unchanged (or green if white info default) - if ($level === 'info') { - $formatted = $this->useColors ? $this->formatInfoLabelWhite($message) : $message; + if ($level === 'info' || $level === 'out') { + $formatted = $this->useColors ? $this->formatLabelWhite($message) : $message; return $prefix . $formatted; } @@ -491,7 +496,7 @@ private function rawWrite(string $str): void { if ($this->io) { // ConsoleIo::out() defaults to a trailing newline; we want raw text - $this->io->out($str, 0); + $this->io->out($str, 0, 0); } else { // Fallback to STDOUT echo $str; @@ -505,14 +510,18 @@ private function rawWrite(string $str): void * @return string Formatted message * @since COmanage Registry v5.2.0 */ - private function formatInfoLabelWhite(string $message): string + private function formatLabelWhite(string $message): string { $lines = explode("\n", $message); foreach ($lines as $i => $line) { if ($line === '') { continue; } if (preg_match('/^([^:\r\n]+:)(.*)$/', $line, $m)) { $second = $m[2]; - if ($this->useColors && $this->defaultColorForLevel('info') === 'white' && $second !== '') { + if ( + $this->useColors + && ($this->defaultColorForLevel('info') === 'white' || $this->defaultColorForLevel('out') === 'white') + && $second !== '' + ) { $second = $this->wrapColor($second, 'green'); } $lines[$i] = $this->wrapColor($m[1], 'white') . $second; @@ -522,24 +531,55 @@ private function formatInfoLabelWhite(string $message): string } /** - * Detect verbose mode from ConsoleIO instance + * Detect verbose level from ConsoleIO instance * - * @return bool Whether verbose mode is enabled + * @return int Verbose level * @since COmanage Registry v5.2.0 */ - private function detectVerboseFromIo(): bool + private function detectVerboseFromIo(): int { + $default = 0; if ($this->io === null) { - return false; + return $default; } if (method_exists($this->io, 'level')) { try { - $level = (int)$this->io->level(); - return $level >= 2; // VERBOSE + return $this->io->level(); } catch (\Throwable $e) { - return false; + return $default; } } - return false; + return $default; + } + + /** + * Determine if a message of the given level should be printed based on verbosity setting + * + * @param string $level Message level to check + * @return bool Whether the message should be printed + * @since COmanage Registry v5.2.0 + */ + private function shouldPrintLevel(string $level): bool + { + $level = strtolower($level); + $verboseLevel = $this->detectVerboseFromIo(); + + return match ($verboseLevel) { + 0 => in_array($level, ['out', 'error'], true), // only errors and out + 1 => in_array($level, ['out', 'info', 'warn', 'warning', 'error'], true), // no verbose/debug + 2 => in_array($level, ['out', 'info', 'warn', 'warning', 'error', 'debug', 'verbose'], true), // all messages + default => true, + }; + } + + /** + * Get the current verbosity level + * + * @return int Verbosity level (0=quiet, 1=normal, 2=verbose) + * @since COmanage Registry v5.2.0 + */ + private function getVerboseLevel(): int + { + return $this->detectVerboseFromIo(); } }