From 3bcd0d05b4be9f276c005aec2bc57349d5e949a2 Mon Sep 17 00:00:00 2001 From: JonGardiner Date: Tue, 6 Feb 2024 16:23:07 -0700 Subject: [PATCH] Ansi color when outputting to TTY. --- src/Output/ConsoleOutput.php | 5 ++++- src/Output/OutputInterface.php | 2 ++ src/Output/XUnitOutput.php | 5 +++++ src/Phases/AnalyzingPhase.php | 27 ++++++++++++++++++++------- src/Phases/IndexingPhase.php | 7 ++++++- 5 files changed, 37 insertions(+), 9 deletions(-) diff --git a/src/Output/ConsoleOutput.php b/src/Output/ConsoleOutput.php index f3a0bf1..9f30d68 100644 --- a/src/Output/ConsoleOutput.php +++ b/src/Output/ConsoleOutput.php @@ -34,8 +34,10 @@ public function emitError($className, $fileName, $lineNumber, $name, $message = */ public function renderResults() { echo "\n"; + $white=$this->ttyContent("\33[97m"); + $reset=$this->ttyContent("\33[0m"); foreach ($this->errors as $fileName => $errors) { - echo " Line | $fileName\n"; + echo " ${white}Line${reset} | ${white}$fileName${reset}\n"; echo "-------+----------------------------------------------------------------\n"; usort($errors, function ($cmpa, $cmpb) { return $cmpa['line'] > $cmpb['line'] ? 1 : ($cmpa['line'] == $cmpb['line'] ? 0 : -1); @@ -44,6 +46,7 @@ public function renderResults() { if (!is_int($error['line'])) { var_dump($error); } + printf("%6d | %s\n", $error['line'], $error['message']); } echo "\n"; diff --git a/src/Output/OutputInterface.php b/src/Output/OutputInterface.php index fdfc22e..4253ec9 100644 --- a/src/Output/OutputInterface.php +++ b/src/Output/OutputInterface.php @@ -34,6 +34,8 @@ function emitError($className, $file, $line, $type, $message = ""); */ function output($verbose, $extraVerbose); + function ttyContent(string $content):string; + /** * outputVerbose * diff --git a/src/Output/XUnitOutput.php b/src/Output/XUnitOutput.php index c699baa..d023fc7 100644 --- a/src/Output/XUnitOutput.php +++ b/src/Output/XUnitOutput.php @@ -256,6 +256,11 @@ public function emitError($className, $fileName, $lineNumber, $name, $message="" $this->outputExtraVerbose("ERROR: $fileName $lineNumber: $name: $message\n"); } + public function ttyContent($content):string { + return $this->isTTY ? $content : ""; + } + + /** * output * diff --git a/src/Phases/AnalyzingPhase.php b/src/Phases/AnalyzingPhase.php index 86576c8..0afe446 100644 --- a/src/Phases/AnalyzingPhase.php +++ b/src/Phases/AnalyzingPhase.php @@ -278,12 +278,15 @@ protected function processChildMessage($socket, $msg, &$processingCount, &$fileN $kbs=intdiv( intdiv($bytes, 1024), (time()-$start) ?: 1); ["total"=>$errors, "displayed"=>$displayCount] = $output->getErrorCounts(); if ($output->isTTY()) { - printf("%d/%d, %d/%d MB (%d%%), %d KB/s %d errors, %d suppressed \r", + $white=$output->ttyContent("\33[97m"); + $red=$output->ttyContent("\33[31m"); + $reset=$output->ttyContent("\33[0m"); + printf("$white%d$reset/$white%d$reset, $white%d$reset/$white%d$reset MB ($white%d$reset%%), $white%d$reset KB/s $red%d$reset errors \r", $fileNumber, count($toProcess), intdiv($bytes, 1024 * 1024), intdiv($totalBytes, 1024 * 1024), intval(round(100 * $bytes / $totalBytes)), $kbs, - $displayCount, $errors - $displayCount + $displayCount ); } else { $output->output(".", sprintf("%d - %s", $fileNumber-1, $analyzedFileName)); @@ -320,7 +323,7 @@ protected function runChildAnalyzer($socket, Config $config) { $receive = trim($receive); if ($receive == "TIMINGS") { $this->socket_write_all($socket, "TIMINGS " . base64_encode(json_encode($this->analyzer->getTimingsAndCounts()) ). "\n"); - return 0; + return; } else { list(, $file) = explode(' ', $receive, 2); $size = $this->analyzeFile($file, $config); @@ -378,6 +381,9 @@ public function run(Config $config, OutputInterface $output) { $output->output("Invalid or missing paths in your test config section.\n", "Invalid or missing paths in your test config section.\n"); exit; } + + $white=$output->ttyContent("\33[97m"); + $reset=$output->ttyContent("\33[0m"); $output->outputVerbose("Test directories are valid: Starting Analysis\n"); $toProcess = []; if ($config->hasFileList()) { @@ -387,13 +393,20 @@ public function run(Config $config, OutputInterface $output) { } else { foreach ($indexPaths as $path) { $tmpDirectory = Util::fullDirectoryPath($baseDirectory, $path); - $output->outputVerbose("Directory: $path\n"); + $output->outputVerbose( + "Directory: " . + $white . + $path . + $reset . + $output->ttyContent("\33[0m") . + "\n" + ); $it2 = DirectoryLister::getGenerator($tmpDirectory); $this->getPhase2Files($config, $it2, $toProcess); } } - $output->outputVerbose("Allotting work for " . $config->getPartitions() . " partitions\n"); + $output->outputVerbose("Allotting work for " . $white . $config->getPartitions() . $reset . " partitions\n"); // Sort all the files first by size and second by name. // Once we have a list that is roughly even, then we can split @@ -430,9 +443,9 @@ public function run(Config $config, OutputInterface $output) { } } - $output->outputVerbose("Sizes: " . implode(", ", $sizes)."\n"); + $output->outputVerbose("Partition sizes: " . $white . implode("$reset,$white ", $sizes)."$reset\n"); - $output->outputVerbose("Partition " . ($partitionNumber + 1) . " analyzing " . number_format(count($partialList) ). " files (" . number_format($sizes[$partitionNumber] ). " bytes)\n"); + $output->outputVerbose("Partition " . $white.($partitionNumber + 1).$reset . " analyzing " . $white.number_format(count($partialList) ). $reset." files (" . $white.number_format($sizes[$partitionNumber] ).$reset. " bytes)\n"); return $this->phase2($config, $output, $partialList, $sizes[$partitionNumber]); } diff --git a/src/Phases/IndexingPhase.php b/src/Phases/IndexingPhase.php index 878009b..bf5a0d1 100644 --- a/src/Phases/IndexingPhase.php +++ b/src/Phases/IndexingPhase.php @@ -217,7 +217,12 @@ function ($socket, $msg) use (&$itr, &$fileNumber, &$bytes, $output, $start, $co return ProcessManager::CLOSE_CONNECTION; } if ($fileNumber % 50 == 0) { - $process= sprintf("Processing %.1f KB/second", $bytes / 1024 / (microtime(true) - $start)); + $process= sprintf( + "Processing %s%.1f%s KB/second", + $output->ttyContent("\33[97m"), + $bytes / 1024 / (microtime(true) - $start), + $output->ttyContent("\33[0m") + ); if ($config->getOutputLevel()==1) { if (!$output->isTTY()) { $output->outputVerbose(".");