Skip to content

Commit

Permalink
Fix verbosity print
Browse files Browse the repository at this point in the history
  • Loading branch information
Ioannis committed Oct 29, 2025
1 parent 341c83a commit 36a0361
Show file tree
Hide file tree
Showing 2 changed files with 81 additions and 45 deletions.
22 changes: 9 additions & 13 deletions app/plugins/Transmogrify/src/Command/TransmogrifyCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -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));


/*
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -412,27 +410,25 @@ 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
// (ie: mapLegacyFieldNames basically requires a successful mapping)
$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();
Expand All @@ -446,15 +442,15 @@ 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);
}

// If user selected a subset, exit as soon as all selected tables are processed
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;
}
}
Expand All @@ -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();
Expand Down
104 changes: 72 additions & 32 deletions app/plugins/Transmogrify/src/Lib/Util/CommandLinePrinter.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
*
Expand All @@ -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();

}

/**
Expand All @@ -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;
}

Expand Down Expand Up @@ -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;
}

Expand All @@ -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;
}

Expand All @@ -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
*
Expand Down Expand Up @@ -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
Expand All @@ -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;
}

Expand Down Expand Up @@ -373,7 +377,6 @@ public function pause(string $prompt = 'Press <enter> to continue...'): void
private function colorizeLevel(string $level, string $message): string
{
$level = strtolower($level);
$prefix = '';
switch ($level) {
case 'warn':
case 'warning':
Expand All @@ -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;
}

Expand Down Expand Up @@ -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;
Expand All @@ -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;
Expand All @@ -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();
}
}

0 comments on commit 36a0361

Please sign in to comment.