Skip to content

Commit

Permalink
Ansi color when outputting to TTY.
Browse files Browse the repository at this point in the history
  • Loading branch information
jongardiner committed Feb 6, 2024
1 parent 7f27040 commit 3bcd0d0
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 9 deletions.
5 changes: 4 additions & 1 deletion src/Output/ConsoleOutput.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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";
Expand Down
2 changes: 2 additions & 0 deletions src/Output/OutputInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ function emitError($className, $file, $line, $type, $message = "");
*/
function output($verbose, $extraVerbose);

function ttyContent(string $content):string;

/**
* outputVerbose
*
Expand Down
5 changes: 5 additions & 0 deletions src/Output/XUnitOutput.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
*
Expand Down
27 changes: 20 additions & 7 deletions src/Phases/AnalyzingPhase.php
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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()) {
Expand All @@ -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
Expand Down Expand Up @@ -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]);
}

Expand Down
7 changes: 6 additions & 1 deletion src/Phases/IndexingPhase.php
Original file line number Diff line number Diff line change
Expand Up @@ -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(".");
Expand Down

0 comments on commit 3bcd0d0

Please sign in to comment.