mirror of
https://codeberg.org/scip/tablizer.git
synced 2026-02-04 02:20:56 +01:00
colorize-output using regexes, refactor line colorization (#49)
This commit is contained in:
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
Copyright © 2022 Thomas von Dein
|
||||
Copyright © 2022-2026 Thomas von Dein
|
||||
|
||||
This program is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU General Public License as published by
|
||||
@@ -26,8 +26,8 @@ import (
|
||||
"strconv"
|
||||
"strings"
|
||||
|
||||
"github.com/gookit/color"
|
||||
"codeberg.org/scip/tablizer/cfg"
|
||||
"github.com/gookit/color"
|
||||
)
|
||||
|
||||
func findindex(s []int, e int) (int, bool) {
|
||||
@@ -245,35 +245,33 @@ 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) {
|
||||
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 +279,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 {
|
||||
|
||||
Reference in New Issue
Block a user