fix: display multi-line diffs with correct colors

This commit is contained in:
technofab 2025-05-31 18:09:33 +02:00
parent b50a1c61a6
commit 2b7c215ffa

View file

@ -3,6 +3,7 @@ package main
import ( import (
"fmt" "fmt"
"os" "os"
"regexp"
"strings" "strings"
"github.com/jedib0t/go-pretty/v6/table" "github.com/jedib0t/go-pretty/v6/table"
@ -26,8 +27,25 @@ func printErrors(results Results) {
} else { } else {
message = result.ErrorMessage message = result.ErrorMessage
} }
// handle multi-line colored changes
colorState := ""
colorRegex := regexp.MustCompile(`\x1b\[[0-9;]*m`) // Match any escape sequence
for line := range strings.Lines(message) { for line := range strings.Lines(message) {
fmt.Printf("%s %s", text.FgRed.Sprint("|"), line) coloredLine := colorState + line
fmt.Printf("%s %s", text.FgRed.Sprint("|"), coloredLine)
matches := colorRegex.FindAllString(line, -1)
// determine last color code, to copy to next line
if len(matches) > 0 {
lastMatch := matches[len(matches)-1]
if lastMatch == "\x1b[0m" {
colorState = "" // reset color state
} else {
colorState = lastMatch // save color state for next line
}
}
} }
if message == "" { if message == "" {
fmt.Printf("- no output -") fmt.Printf("- no output -")