From 9cedac654377e3e9c46752737c394d1dd4dc763c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Thomas=20Str=C3=B6mberg?= Date: Sun, 3 Nov 2024 08:51:10 -0500 Subject: [PATCH] brief: highlight evidence (#566) * brief: highlight evidence * remove whitespace --- pkg/render/terminal_brief.go | 21 ++++++++++++++++++++- 1 file changed, 20 insertions(+), 1 deletion(-) diff --git a/pkg/render/terminal_brief.go b/pkg/render/terminal_brief.go index 93bc43908..f8d8f9b12 100644 --- a/pkg/render/terminal_brief.go +++ b/pkg/render/terminal_brief.go @@ -17,6 +17,7 @@ import ( "io" "github.com/chainguard-dev/malcontent/pkg/malcontent" + "github.com/fatih/color" ) type TerminalBrief struct { @@ -36,11 +37,29 @@ func (r TerminalBrief) File(_ context.Context, fr *malcontent.FileReport) error return nil } + width := suggestedWidth() fmt.Fprintf(r.w, "├─ %s %s\n", riskEmoji(fr.RiskScore), fr.Path) for _, b := range fr.Behaviors { + content := fmt.Sprintf("│ %s %s — %s", riskColor(fr.RiskLevel, "•"), riskColor(fr.RiskLevel, b.ID), b.Description) e := evidenceString(b.MatchStrings, b.Description) - fmt.Fprintf(r.w, "│ %s %s — %s%s\n", riskColor(fr.RiskLevel, "•"), riskColor(fr.RiskLevel, b.ID), b.Description, e) + + // no evidence to give + if e == "" { + continue + } + + fmt.Fprint(r.w, content) + color.New(color.FgHiBlack).Fprint(r.w, ":") + e = color.RGB(255, 255, 255).Sprint(e) + + // Two-line output for long evidence strings + if ansiLineLength(content+e)+1 > width && len(e) > 4 { + fmt.Fprintln(r.w, "\n"+truncate(fmt.Sprintf("│ %s", e), width)) + continue + } + // Single-line output for short evidence + fmt.Fprintln(r.w, " "+e) } return nil