From a4498b3e3399fc5f7dbf045b9d2318f7c4331f91 Mon Sep 17 00:00:00 2001 From: Fernandez Ludovic Date: Sat, 25 Mar 2023 12:11:21 +0100 Subject: [PATCH] output: add colored-tab --- .golangci.reference.yml | 2 +- pkg/commands/run.go | 6 ++++-- pkg/config/output.go | 1 + pkg/printers/tab.go | 14 +++++++++++--- pkg/printers/tab_test.go | 2 +- pkg/printers/text.go | 11 ++++++----- 6 files changed, 24 insertions(+), 12 deletions(-) diff --git a/.golangci.reference.yml b/.golangci.reference.yml index dacc57a1b44e..be3280abb020 100644 --- a/.golangci.reference.yml +++ b/.golangci.reference.yml @@ -76,7 +76,7 @@ run: # output configuration options output: - # Format: colored-line-number|line-number|json|tab|checkstyle|code-climate|junit-xml|github-actions|teamcity + # Format: colored-line-number|line-number|json|colored-tab|tab|checkstyle|code-climate|junit-xml|github-actions|teamcity # # Multiple can be specified by separating them by comma, output can be provided # for each of them by separating format name and path by colon symbol. diff --git a/pkg/commands/run.go b/pkg/commands/run.go index 20106b44f624..453b3a666d61 100644 --- a/pkg/commands/run.go +++ b/pkg/commands/run.go @@ -475,8 +475,10 @@ func (e *Executor) createPrinter(format string, w io.Writer) (printers.Printer, p = printers.NewText(e.cfg.Output.PrintIssuedLine, format == config.OutFormatColoredLineNumber, e.cfg.Output.PrintLinterName, e.log.Child(logutils.DebugKeyTextPrinter), w) - case config.OutFormatTab: - p = printers.NewTab(e.cfg.Output.PrintLinterName, e.log.Child(logutils.DebugKeyTabPrinter), w) + case config.OutFormatTab, config.OutFormatColoredTab: + p = printers.NewTab(e.cfg.Output.PrintLinterName, + format == config.OutFormatColoredTab, + e.log.Child(logutils.DebugKeyTabPrinter), w) case config.OutFormatCheckstyle: p = printers.NewCheckstyle(w) case config.OutFormatCodeClimate: diff --git a/pkg/config/output.go b/pkg/config/output.go index 2c49ea7f4e66..e946539fc1b2 100644 --- a/pkg/config/output.go +++ b/pkg/config/output.go @@ -5,6 +5,7 @@ const ( OutFormatLineNumber = "line-number" OutFormatColoredLineNumber = "colored-line-number" OutFormatTab = "tab" + OutFormatColoredTab = "colored-tab" OutFormatCheckstyle = "checkstyle" OutFormatCodeClimate = "code-climate" OutFormatHTML = "html" diff --git a/pkg/printers/tab.go b/pkg/printers/tab.go index ffef49108587..dd296c65f97c 100644 --- a/pkg/printers/tab.go +++ b/pkg/printers/tab.go @@ -14,13 +14,16 @@ import ( type Tab struct { printLinterName bool - log logutils.Log - w io.Writer + useColors bool + + log logutils.Log + w io.Writer } -func NewTab(printLinterName bool, log logutils.Log, w io.Writer) *Tab { +func NewTab(printLinterName, useColors bool, log logutils.Log, w io.Writer) *Tab { return &Tab{ printLinterName: printLinterName, + useColors: useColors, log: log, w: w, } @@ -28,6 +31,11 @@ func NewTab(printLinterName bool, log logutils.Log, w io.Writer) *Tab { func (p *Tab) SprintfColored(ca color.Attribute, format string, args ...interface{}) string { c := color.New(ca) + + if !p.useColors { + c.DisableColor() + } + return c.Sprintf(format, args...) } diff --git a/pkg/printers/tab_test.go b/pkg/printers/tab_test.go index 882699ef853c..6e9cbcd35e3a 100644 --- a/pkg/printers/tab_test.go +++ b/pkg/printers/tab_test.go @@ -46,7 +46,7 @@ func TestTab_Print(t *testing.T) { buf := new(bytes.Buffer) - printer := NewTab(true, logutils.NewStderrLog(logutils.DebugKeyEmpty), buf) + printer := NewTab(true, false, logutils.NewStderrLog(logutils.DebugKeyEmpty), buf) err := printer.Print(context.Background(), issues) require.NoError(t, err) diff --git a/pkg/printers/text.go b/pkg/printers/text.go index d59391b29d28..7c0e8f928dc0 100644 --- a/pkg/printers/text.go +++ b/pkg/printers/text.go @@ -14,8 +14,8 @@ import ( type Text struct { printIssuedLine bool - useColors bool printLinterName bool + useColors bool log logutils.Log w io.Writer @@ -24,19 +24,20 @@ type Text struct { func NewText(printIssuedLine, useColors, printLinterName bool, log logutils.Log, w io.Writer) *Text { return &Text{ printIssuedLine: printIssuedLine, - useColors: useColors, printLinterName: printLinterName, + useColors: useColors, log: log, w: w, } } func (p *Text) SprintfColored(ca color.Attribute, format string, args ...interface{}) string { + c := color.New(ca) + if !p.useColors { - return fmt.Sprintf(format, args...) + c.DisableColor() } - c := color.New(ca) return c.Sprintf(format, args...) } @@ -73,7 +74,7 @@ func (p *Text) printSourceCode(i *result.Issue) { } } -func (p Text) printUnderLinePointer(i *result.Issue) { +func (p *Text) printUnderLinePointer(i *result.Issue) { // if column == 0 it means column is unknown (e.g. for gosec) if len(i.SourceLines) != 1 || i.Pos.Column == 0 { return