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-2025 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
|
||||
@@ -22,8 +22,8 @@ import (
|
||||
"io"
|
||||
"strings"
|
||||
|
||||
"github.com/lithammer/fuzzysearch/fuzzy"
|
||||
"codeberg.org/scip/tablizer/cfg"
|
||||
"github.com/lithammer/fuzzysearch/fuzzy"
|
||||
)
|
||||
|
||||
/*
|
||||
|
||||
@@ -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 {
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
Copyright © 2022-2025 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
|
||||
@@ -23,6 +23,7 @@ import (
|
||||
"fmt"
|
||||
"io"
|
||||
"log"
|
||||
"os"
|
||||
"strconv"
|
||||
"strings"
|
||||
"text/template"
|
||||
@@ -75,8 +76,8 @@ func printData(writer io.Writer, conf cfg.Config, data *Tabdata) {
|
||||
}
|
||||
}
|
||||
|
||||
func output(writer io.Writer, str string) {
|
||||
_, err := fmt.Fprint(writer, str)
|
||||
func output(writer io.Writer, conf cfg.Config, str string) {
|
||||
_, err := fmt.Fprint(writer, colorizeOutput(conf, str))
|
||||
if err != nil {
|
||||
log.Fatalf("failed to print output: %s", err)
|
||||
}
|
||||
@@ -138,7 +139,7 @@ func printOrgmodeData(writer io.Writer, conf cfg.Config, data *Tabdata) {
|
||||
log.Fatalf("Failed to render table: %s", err)
|
||||
}
|
||||
|
||||
output(writer, color.Sprint(colorizeData(conf, tableString.String())))
|
||||
output(writer, conf, color.Sprint(colorizeData(conf, tableString.String())))
|
||||
}
|
||||
|
||||
/*
|
||||
@@ -197,7 +198,7 @@ func printMarkdownData(writer io.Writer, conf cfg.Config, data *Tabdata) {
|
||||
log.Fatalf("Failed to render table: %s", err)
|
||||
}
|
||||
|
||||
output(writer, color.Sprint(colorizeData(conf, tableString.String())))
|
||||
output(writer, conf, color.Sprint(colorizeData(conf, tableString.String())))
|
||||
}
|
||||
|
||||
/*
|
||||
@@ -254,7 +255,7 @@ func printASCIIData(writer io.Writer, conf cfg.Config, data *Tabdata) {
|
||||
log.Fatalf("Failed to render table: %s", err)
|
||||
}
|
||||
|
||||
output(writer, color.Sprint(colorizeData(conf, tableString.String())))
|
||||
output(writer, conf, color.Sprint(colorizeData(conf, tableString.String())))
|
||||
}
|
||||
|
||||
/*
|
||||
@@ -275,7 +276,7 @@ func printExtendedData(writer io.Writer, conf cfg.Config, data *Tabdata) {
|
||||
}
|
||||
}
|
||||
|
||||
output(writer, colorizeData(conf, out))
|
||||
output(writer, conf, colorizeData(conf, out))
|
||||
}
|
||||
|
||||
/*
|
||||
@@ -298,7 +299,7 @@ func printShellData(writer io.Writer, data *Tabdata) {
|
||||
}
|
||||
|
||||
// no colorization here
|
||||
output(writer, out)
|
||||
output(writer, cfg.Config{}, out)
|
||||
}
|
||||
|
||||
func printJsonData(writer io.Writer, data *Tabdata) {
|
||||
@@ -327,7 +328,7 @@ func printJsonData(writer io.Writer, data *Tabdata) {
|
||||
log.Fatal(err)
|
||||
}
|
||||
|
||||
output(writer, string(jsonstr))
|
||||
output(writer, cfg.Config{}, string(jsonstr))
|
||||
}
|
||||
|
||||
func printYamlData(writer io.Writer, data *Tabdata) {
|
||||
@@ -364,7 +365,7 @@ func printYamlData(writer io.Writer, data *Tabdata) {
|
||||
log.Fatal(err)
|
||||
}
|
||||
|
||||
output(writer, string(yamlstr))
|
||||
output(writer, cfg.Config{}, string(yamlstr))
|
||||
}
|
||||
|
||||
func printCSVData(writer io.Writer, conf cfg.Config, data *Tabdata) {
|
||||
@@ -414,3 +415,23 @@ func printTemplateData(writer io.Writer, conf cfg.Config, data *Tabdata) {
|
||||
log.Fatalf("failed to print output: %s", err)
|
||||
}
|
||||
}
|
||||
|
||||
func colorizeOutput(conf cfg.Config, input string) string {
|
||||
if len(conf.UseColorizers) > 0 && !conf.NoColor && color.IsConsole(os.Stdout) {
|
||||
for _, colorizer := range conf.UseColorizers {
|
||||
// colorize matching parts of the whole output with given color, if the terminal supports it
|
||||
// color may contain fg:bg or just fg. Color definitions see https://github.com/gookit/color
|
||||
input = colorizer.Search.ReplaceAllStringFunc(input, func(in string) string {
|
||||
col := colorizer.Replace
|
||||
if strings.Contains(col, ":") {
|
||||
parts := strings.Split(col, ":")
|
||||
return color.Sprintf("<fg=%s;bg=%s>%s</>", parts[0], parts[1], in)
|
||||
}
|
||||
|
||||
return color.Sprintf("<fg=%s>%s</>", col, in)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
return input
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user