From 473a009a86ac4c58a3977b203e47a9795a896989 Mon Sep 17 00:00:00 2001 From: Thomas von Dein Date: Mon, 19 Jan 2026 13:54:05 +0100 Subject: [PATCH] refactored line colorizer --- cfg/config.go | 4 ---- cfg/config_test.go | 1 - lib/helpers.go | 45 ++++++++++++++++++++++----------------------- 3 files changed, 22 insertions(+), 28 deletions(-) diff --git a/cfg/config.go b/cfg/config.go index e0f0208..c7c5c91 100644 --- a/cfg/config.go +++ b/cfg/config.go @@ -111,10 +111,6 @@ type Config struct { Colorizers []string // []string{"/ /-/", "/foo/fg[:bg]/"} UseColorizers []Transposer // {Search: re, Replace: color} - /* - FIXME: make configurable somehow, config file or ENV - see https://github.com/gookit/color. - */ ColorStyle color.Style HighlightStyle color.Style NoHighlightStyle color.Style diff --git a/cfg/config_test.go b/cfg/config_test.go index b4b1058..6b2bbb4 100644 --- a/cfg/config_test.go +++ b/cfg/config_test.go @@ -39,7 +39,6 @@ func TestPrepareModeFlags(t *testing.T) { {Modeflag{}, ASCII}, } - // FIXME: use a map for easier printing for _, testdata := range tests { testname := fmt.Sprintf("PrepareModeFlags-expect-%d", testdata.expect) t.Run(testname, func(t *testing.T) { diff --git a/lib/helpers.go b/lib/helpers.go index 6027b08..ab291b7 100644 --- a/lib/helpers.go +++ b/lib/helpers.go @@ -245,35 +245,34 @@ func reduceColumns(conf cfg.Config, data *Tabdata) { } } -// FIXME: refactor this beast! func colorizeData(conf cfg.Config, output string) string { + if !conf.NoColor && !color.IsConsole(os.Stdout) { + panic(1) + return output + } + switch { - case conf.UseHighlight && color.IsConsole(os.Stdout): + case conf.UseHighlight: highlight := true colorized := "" - first := true + style := color.Style{} - for _, line := range strings.Split(output, "\n") { - if highlight { - if first { - // we need to add two spaces to the header line - // because tablewriter omits them for some reason - // in pprint mode. This doesn't matter as long as - // we don't use colorization. But with colors the - // missing spaces can be seen. - if conf.OutputMode == cfg.ASCII { - line += " " - } - - line = conf.HighlightHdrStyle.Sprint(line) - first = false - } else { - line = conf.HighlightStyle.Sprint(line) - } - } else { - line = conf.NoHighlightStyle.Sprint(line) + for idx, line := range strings.Split(output, "\n") { + if idx == 0 { + style = conf.HighlightHdrStyle } + switch highlight { + case true: + if idx > 0 { + style = conf.HighlightStyle + } + case false: + style = conf.NoHighlightStyle + } + + line = style.Sprint(line) + highlight = !highlight colorized += line + "\n" @@ -281,7 +280,7 @@ func colorizeData(conf cfg.Config, output string) string { return colorized - case len(conf.Patterns) > 0 && !conf.NoColor && color.IsConsole(os.Stdout): + case len(conf.Patterns) > 0: out := output for _, re := range conf.Patterns {