fixed #5, colorization now always works as expected

This commit is contained in:
2022-10-25 14:24:05 +02:00
parent 001021dac8
commit 417faf3ff2
6 changed files with 36 additions and 60 deletions

View File

@@ -1,17 +1,11 @@
## Fixes to be implemented ## Fixes to be implemented
- highlighting does not repeat, only 1 will be highlighted, see #3
- rm printYamlData() log.Fatal(), maybe return error on all printers?
- refactor parser, there's some duplicate code, remove pattern from parser args - refactor parser, there's some duplicate code, remove pattern from parser args
## Features to be implemented ## Features to be implemented
- add comment support (csf.NewReader().Comment = '#') - add comment support (csf.NewReader().Comment = '#')
- add output mode csv
- add --no-headers option - add --no-headers option

View File

@@ -46,12 +46,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 will be set by see https://github.com/gookit/color.
io.ProcessFiles() according to currently supported
color mode.
*/ */
MatchFG string ColorStyle color.Style
MatchBG string
NoColor bool NoColor bool
} }
@@ -84,22 +82,43 @@ type Sortmode struct {
Age bool Age bool
} }
func Colors() map[color.Level]map[string]string { // default color schemes
// default color schemes func Colors() map[color.Level]map[string]color.Color {
return map[color.Level]map[string]string{ return map[color.Level]map[string]color.Color{
color.Level16: { color.Level16: {
"bg": "green", "fg": "black", "bg": color.BgGreen, "fg": color.FgBlack,
}, },
color.Level256: { color.Level256: {
"bg": "lightGreen", "fg": "black", "bg": color.BgLightGreen, "fg": color.FgBlack,
}, },
color.LevelRgb: { color.LevelRgb: {
// FIXME: maybe use something nicer // FIXME: maybe use something nicer
"bg": "lightGreen", "fg": "black", "bg": color.BgLightGreen, "fg": color.FgBlack,
}, },
} }
} }
// find supported color mode, modifies config based on constants
func (c *Config) DetermineColormode() {
if !isTerminal(os.Stdout) {
color.Disable()
} else {
level := color.TermColorLevel()
colors := Colors()
c.ColorStyle = color.New(colors[level]["bg"], colors[level]["fg"])
}
}
// Return true if current terminal is interactive
func isTerminal(f *os.File) bool {
o, _ := f.Stat()
if (o.Mode() & os.ModeCharDevice) == os.ModeCharDevice {
return true
} else {
return false
}
}
func Getversion() string { func Getversion() string {
// main program version // main program version

View File

@@ -69,10 +69,11 @@ func Execute() {
return nil return nil
} }
// prepare flags // Setup
conf.CheckEnv() conf.CheckEnv()
conf.PrepareModeFlags(modeflag) conf.PrepareModeFlags(modeflag)
conf.PrepareSortFlags(sortmode) conf.PrepareSortFlags(sortmode)
conf.DetermineColormode()
if err := conf.PreparePattern(); err != nil { if err := conf.PreparePattern(); err != nil {
return err return err

View File

@@ -152,36 +152,13 @@ func trimRow(row []string) []string {
return fixedrow return fixedrow
} }
func maskParens(in string) string {
/*
we need to escape brackets, because the color module treats
text enclosed within < and > as a color tag and therefore the
color tags don't work anymore.
See https://github.com/gookit/color/issues/52 for details.
*/
return strings.ReplaceAll(strings.ReplaceAll(in, ">", "⦘"), "<", "⦗")
}
func unmaskParens(in string) string {
// does the reverse from above during actual output
return strings.ReplaceAll(strings.ReplaceAll(in, "⦘", ">"), "⦗", "<")
}
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 len(c.Pattern) > 0 && !c.NoColor && color.IsConsole(os.Stdout) {
r := regexp.MustCompile("(" + c.Pattern + ")") r := regexp.MustCompile("(" + c.Pattern + ")")
return r.ReplaceAllString(maskParens(output), "<bg="+c.MatchBG+";fg="+c.MatchFG+">$1</>") return r.ReplaceAllStringFunc(output, func(in string) string {
return c.ColorStyle.Sprint(in)
})
} else { } else {
return output return output
} }
} }
func isTerminal(f *os.File) bool {
o, _ := f.Stat()
if (o.Mode() & os.ModeCharDevice) == os.ModeCharDevice {
return true
} else {
return false
}
}

View File

@@ -19,7 +19,6 @@ package lib
import ( import (
"errors" "errors"
"github.com/gookit/color"
"github.com/tlinden/tablizer/cfg" "github.com/tlinden/tablizer/cfg"
"io" "io"
"os" "os"
@@ -32,8 +31,6 @@ func ProcessFiles(c cfg.Config, args []string) error {
return err return err
} }
determineColormode(&c)
for _, fd := range fds { for _, fd := range fds {
data, err := parseFile(c, fd, pattern) data, err := parseFile(c, fd, pattern)
if err != nil { if err != nil {
@@ -51,18 +48,6 @@ func ProcessFiles(c cfg.Config, args []string) error {
return nil return nil
} }
// find supported color mode, modifies config based on constants
func determineColormode(c *cfg.Config) {
if !isTerminal(os.Stdout) {
color.Disable()
} else {
level := color.TermColorLevel()
colors := cfg.Colors()
c.MatchFG = colors[level]["fg"]
c.MatchBG = colors[level]["bg"]
}
}
func determineIO(c *cfg.Config, args []string) ([]io.Reader, string, error) { func determineIO(c *cfg.Config, args []string) ([]io.Reader, string, error) {
var pattern string var pattern string
var fds []io.Reader var fds []io.Reader

View File

@@ -65,7 +65,7 @@ func printData(w io.Writer, c cfg.Config, data *Tabdata) {
} }
func output(w io.Writer, str string) { func output(w io.Writer, str string) {
fmt.Fprint(w, unmaskParens(str)) fmt.Fprint(w, str)
} }
/* /*