refactored line colorizer

This commit is contained in:
2026-01-19 13:54:05 +01:00
parent bba477f56d
commit 473a009a86
3 changed files with 22 additions and 28 deletions

View File

@@ -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

View File

@@ -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) {

View File

@@ -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 {