mirror of
https://codeberg.org/scip/tablizer.git
synced 2026-02-04 10:20:59 +01:00
refactored line colorizer
This commit is contained in:
@@ -111,10 +111,6 @@ type Config struct {
|
|||||||
Colorizers []string // []string{"/ /-/", "/foo/fg[:bg]/"}
|
Colorizers []string // []string{"/ /-/", "/foo/fg[:bg]/"}
|
||||||
UseColorizers []Transposer // {Search: re, Replace: color}
|
UseColorizers []Transposer // {Search: re, Replace: color}
|
||||||
|
|
||||||
/*
|
|
||||||
FIXME: make configurable somehow, config file or ENV
|
|
||||||
see https://github.com/gookit/color.
|
|
||||||
*/
|
|
||||||
ColorStyle color.Style
|
ColorStyle color.Style
|
||||||
HighlightStyle color.Style
|
HighlightStyle color.Style
|
||||||
NoHighlightStyle color.Style
|
NoHighlightStyle color.Style
|
||||||
|
|||||||
@@ -39,7 +39,6 @@ func TestPrepareModeFlags(t *testing.T) {
|
|||||||
{Modeflag{}, ASCII},
|
{Modeflag{}, ASCII},
|
||||||
}
|
}
|
||||||
|
|
||||||
// FIXME: use a map for easier printing
|
|
||||||
for _, testdata := range tests {
|
for _, testdata := range tests {
|
||||||
testname := fmt.Sprintf("PrepareModeFlags-expect-%d", testdata.expect)
|
testname := fmt.Sprintf("PrepareModeFlags-expect-%d", testdata.expect)
|
||||||
t.Run(testname, func(t *testing.T) {
|
t.Run(testname, func(t *testing.T) {
|
||||||
|
|||||||
@@ -245,35 +245,34 @@ func reduceColumns(conf cfg.Config, data *Tabdata) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// FIXME: refactor this beast!
|
|
||||||
func colorizeData(conf cfg.Config, output string) string {
|
func colorizeData(conf cfg.Config, output string) string {
|
||||||
|
if !conf.NoColor && !color.IsConsole(os.Stdout) {
|
||||||
|
panic(1)
|
||||||
|
return output
|
||||||
|
}
|
||||||
|
|
||||||
switch {
|
switch {
|
||||||
case conf.UseHighlight && color.IsConsole(os.Stdout):
|
case conf.UseHighlight:
|
||||||
highlight := true
|
highlight := true
|
||||||
colorized := ""
|
colorized := ""
|
||||||
first := true
|
style := color.Style{}
|
||||||
|
|
||||||
for _, line := range strings.Split(output, "\n") {
|
for idx, line := range strings.Split(output, "\n") {
|
||||||
if highlight {
|
if idx == 0 {
|
||||||
if first {
|
style = conf.HighlightHdrStyle
|
||||||
// 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)
|
switch highlight {
|
||||||
first = false
|
case true:
|
||||||
} else {
|
if idx > 0 {
|
||||||
line = conf.HighlightStyle.Sprint(line)
|
style = conf.HighlightStyle
|
||||||
}
|
}
|
||||||
} else {
|
case false:
|
||||||
line = conf.NoHighlightStyle.Sprint(line)
|
style = conf.NoHighlightStyle
|
||||||
}
|
}
|
||||||
|
|
||||||
|
line = style.Sprint(line)
|
||||||
|
|
||||||
highlight = !highlight
|
highlight = !highlight
|
||||||
|
|
||||||
colorized += line + "\n"
|
colorized += line + "\n"
|
||||||
@@ -281,7 +280,7 @@ func colorizeData(conf cfg.Config, output string) string {
|
|||||||
|
|
||||||
return colorized
|
return colorized
|
||||||
|
|
||||||
case len(conf.Patterns) > 0 && !conf.NoColor && color.IsConsole(os.Stdout):
|
case len(conf.Patterns) > 0:
|
||||||
out := output
|
out := output
|
||||||
|
|
||||||
for _, re := range conf.Patterns {
|
for _, re := range conf.Patterns {
|
||||||
|
|||||||
Reference in New Issue
Block a user