mirror of
https://codeberg.org/scip/tablizer.git
synced 2025-12-17 04:30:56 +01:00
fixed alternating highlighting, now looks reasonable
This commit is contained in:
@@ -55,8 +55,10 @@ type Config struct {
|
|||||||
FIXME: make configurable somehow, config file or ENV
|
FIXME: make configurable somehow, config file or ENV
|
||||||
see https://github.com/gookit/color.
|
see https://github.com/gookit/color.
|
||||||
*/
|
*/
|
||||||
ColorStyle color.Style
|
ColorStyle color.Style
|
||||||
HighlightStyle color.Style
|
HighlightStyle color.Style
|
||||||
|
NoHighlightStyle color.Style
|
||||||
|
HighlightHdrStyle color.Style
|
||||||
|
|
||||||
NoColor bool
|
NoColor bool
|
||||||
|
|
||||||
@@ -114,7 +116,9 @@ func Colors() map[color.Level]map[string]color.Color {
|
|||||||
color.LevelRgb: {
|
color.LevelRgb: {
|
||||||
// FIXME: maybe use something nicer
|
// FIXME: maybe use something nicer
|
||||||
"bg": color.BgLightGreen, "fg": color.FgWhite,
|
"bg": color.BgLightGreen, "fg": color.FgWhite,
|
||||||
"hlbg": color.BgBlue, "hlfg": color.FgWhite,
|
"hlbg": color.BgHiGreen, "hlfg": color.FgWhite,
|
||||||
|
"nohlbg": color.BgWhite, "nohlfg": color.FgLightGreen,
|
||||||
|
"hdrbg": color.BgBlue, "hdrfg": color.FgWhite,
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -128,6 +132,8 @@ func (c *Config) DetermineColormode() {
|
|||||||
colors := Colors()
|
colors := Colors()
|
||||||
c.ColorStyle = color.New(colors[level]["bg"], colors[level]["fg"])
|
c.ColorStyle = color.New(colors[level]["bg"], colors[level]["fg"])
|
||||||
c.HighlightStyle = color.New(colors[level]["hlbg"], colors[level]["hlfg"])
|
c.HighlightStyle = color.New(colors[level]["hlbg"], colors[level]["hlfg"])
|
||||||
|
c.NoHighlightStyle = color.New(colors[level]["nohlbg"], colors[level]["nohlfg"])
|
||||||
|
c.HighlightHdrStyle = color.New(colors[level]["hdrbg"], colors[level]["hdrfg"])
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -154,24 +154,42 @@ func trimRow(row []string) []string {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func colorizeData(c cfg.Config, output string) string {
|
func colorizeData(c cfg.Config, output string) string {
|
||||||
if len(c.Pattern) > 0 && !c.NoColor && color.IsConsole(os.Stdout) {
|
if c.UseHighlight && color.IsConsole(os.Stdout) {
|
||||||
r := regexp.MustCompile("(" + c.Pattern + ")")
|
|
||||||
return r.ReplaceAllStringFunc(output, func(in string) string {
|
|
||||||
return c.ColorStyle.Sprint(in)
|
|
||||||
})
|
|
||||||
} else if c.UseHighlight && color.IsConsole(os.Stdout) {
|
|
||||||
highlight := true
|
highlight := true
|
||||||
colorized := ""
|
colorized := ""
|
||||||
|
first := true
|
||||||
|
|
||||||
for _, line := range strings.Split(output, "\n") {
|
for _, line := range strings.Split(output, "\n") {
|
||||||
if highlight {
|
if highlight {
|
||||||
line = c.HighlightStyle.Sprint(line)
|
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 c.OutputMode == cfg.Ascii {
|
||||||
|
line = line + " "
|
||||||
|
}
|
||||||
|
|
||||||
|
line = c.HighlightHdrStyle.Sprint(line)
|
||||||
|
first = false
|
||||||
|
} else {
|
||||||
|
line = c.HighlightStyle.Sprint(line)
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
line = c.NoHighlightStyle.Sprint(line)
|
||||||
}
|
}
|
||||||
highlight = !highlight
|
highlight = !highlight
|
||||||
|
|
||||||
colorized += line + "\n"
|
colorized += line + "\n"
|
||||||
}
|
}
|
||||||
|
|
||||||
return colorized
|
return colorized
|
||||||
|
} else if len(c.Pattern) > 0 && !c.NoColor && color.IsConsole(os.Stdout) {
|
||||||
|
r := regexp.MustCompile("(" + c.Pattern + ")")
|
||||||
|
return r.ReplaceAllStringFunc(output, func(in string) string {
|
||||||
|
return c.ColorStyle.Sprint(in)
|
||||||
|
})
|
||||||
} else {
|
} else {
|
||||||
return output
|
return output
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -20,15 +20,16 @@ package lib
|
|||||||
import (
|
import (
|
||||||
"encoding/csv"
|
"encoding/csv"
|
||||||
"fmt"
|
"fmt"
|
||||||
"github.com/gookit/color"
|
|
||||||
"github.com/olekukonko/tablewriter"
|
|
||||||
"github.com/tlinden/tablizer/cfg"
|
|
||||||
"gopkg.in/yaml.v3"
|
|
||||||
"io"
|
"io"
|
||||||
"log"
|
"log"
|
||||||
"regexp"
|
"regexp"
|
||||||
"strconv"
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
|
"github.com/gookit/color"
|
||||||
|
"github.com/olekukonko/tablewriter"
|
||||||
|
"github.com/tlinden/tablizer/cfg"
|
||||||
|
"gopkg.in/yaml.v3"
|
||||||
)
|
)
|
||||||
|
|
||||||
func printData(w io.Writer, c cfg.Config, data *Tabdata) {
|
func printData(w io.Writer, c cfg.Config, data *Tabdata) {
|
||||||
@@ -148,9 +149,15 @@ func printAsciiData(w io.Writer, c cfg.Config, data *Tabdata) {
|
|||||||
table.SetRowSeparator("")
|
table.SetRowSeparator("")
|
||||||
table.SetHeaderLine(false)
|
table.SetHeaderLine(false)
|
||||||
table.SetBorder(false)
|
table.SetBorder(false)
|
||||||
table.SetTablePadding("\t") // pad with tabs
|
|
||||||
table.SetNoWhiteSpace(true)
|
table.SetNoWhiteSpace(true)
|
||||||
|
|
||||||
|
if !c.UseHighlight {
|
||||||
|
// the tabs destroy the highlighting
|
||||||
|
table.SetTablePadding("\t") // pad with tabs
|
||||||
|
} else {
|
||||||
|
table.SetTablePadding(" ")
|
||||||
|
}
|
||||||
|
|
||||||
table.Render()
|
table.Render()
|
||||||
output(w, color.Sprint(colorizeData(c, tableString.String())))
|
output(w, color.Sprint(colorizeData(c, tableString.String())))
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user