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:
@@ -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 {
|
||||
|
||||
Reference in New Issue
Block a user