From 60700ac2bcadfccbaae8a41604b9c3b72c23bb26 Mon Sep 17 00:00:00 2001 From: ehpersonal38 <103586016+ehpersonal38@users.noreply.github.com> Date: Mon, 6 Mar 2023 16:51:02 -0500 Subject: [PATCH] Trying to colorize diff --stat in Update output --- homepage/views.php | 38 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) diff --git a/homepage/views.php b/homepage/views.php index 0544c88..16e02b5 100644 --- a/homepage/views.php +++ b/homepage/views.php @@ -307,6 +307,44 @@ if(isset($_GET['view'])){ $results = str_replace("failed", "failed",$results); $results = str_replace("active (running)", "active (running)",$results); $results = str_replace("Your branch is up to date", "Your branch is up to date",$results); + + // split the input string into lines + $lines = explode("\n", $results); + + // iterate over each line + foreach ($lines as &$line) { + // check if the line matches the pattern + if (preg_match('/^(.+?)\s*\|\s*(\d+)\s*([\+\- ]+)(\d+)?$/', $line, $matches)) { + // extract the filename, count, and indicator letters + $filename = $matches[1]; + $count = $matches[2]; + $diff = $matches[3]; + $delta = $matches[4] ?? ''; + // determine the indicator letters + $diff_array = str_split($diff); + $indicators = array_map(function ($d) use ($delta) { + if ($d === '+') { + return 'A'; + } elseif ($d === '-') { + return 'B'; + } elseif ($d === ' ') { + if ($delta !== '') { + return 'A'; + } else { + return ' '; + } + } + }, $diff_array); + // modify the line with the new indicator letters + $line = sprintf('%-35s|%3d %s%s', $filename, $count, implode('', $indicators), $delta); + } + } + + // rejoin the modified lines into a string + $output = implode("\n", $lines); + $results = $lines; + + if(strlen($results) == 0) { $results = "This command has no output."; }