diff --git a/TODO.md b/TODO.md index 5327317..4977f90 100644 --- a/TODO.md +++ b/TODO.md @@ -1,17 +1,11 @@ ## 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 ## Features to be implemented - add comment support (csf.NewReader().Comment = '#') -- add output mode csv - - add --no-headers option diff --git a/cfg/config.go b/cfg/config.go index 25ea36d..b6a8bb3 100644 --- a/cfg/config.go +++ b/cfg/config.go @@ -46,12 +46,10 @@ type Config struct { /* FIXME: make configurable somehow, config file or ENV - see https://github.com/gookit/color will be set by - io.ProcessFiles() according to currently supported - color mode. + see https://github.com/gookit/color. */ - MatchFG string - MatchBG string + ColorStyle color.Style + NoColor bool } @@ -84,22 +82,43 @@ type Sortmode struct { Age bool } -func Colors() map[color.Level]map[string]string { - // default color schemes - return map[color.Level]map[string]string{ +// default color schemes +func Colors() map[color.Level]map[string]color.Color { + return map[color.Level]map[string]color.Color{ color.Level16: { - "bg": "green", "fg": "black", + "bg": color.BgGreen, "fg": color.FgBlack, }, color.Level256: { - "bg": "lightGreen", "fg": "black", + "bg": color.BgLightGreen, "fg": color.FgBlack, }, color.LevelRgb: { // 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 { // main program version diff --git a/cmd/root.go b/cmd/root.go index 9600cf2..828e129 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -69,10 +69,11 @@ func Execute() { return nil } - // prepare flags + // Setup conf.CheckEnv() conf.PrepareModeFlags(modeflag) conf.PrepareSortFlags(sortmode) + conf.DetermineColormode() if err := conf.PreparePattern(); err != nil { return err diff --git a/lib/helpers.go b/lib/helpers.go index d13a188..dff6fbc 100644 --- a/lib/helpers.go +++ b/lib/helpers.go @@ -152,36 +152,13 @@ func trimRow(row []string) []string { 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 { if len(c.Pattern) > 0 && !c.NoColor && color.IsConsole(os.Stdout) { r := regexp.MustCompile("(" + c.Pattern + ")") - return r.ReplaceAllString(maskParens(output), "$1") + return r.ReplaceAllStringFunc(output, func(in string) string { + return c.ColorStyle.Sprint(in) + }) } else { return output } } - -func isTerminal(f *os.File) bool { - o, _ := f.Stat() - if (o.Mode() & os.ModeCharDevice) == os.ModeCharDevice { - return true - } else { - return false - } -} diff --git a/lib/io.go b/lib/io.go index 6e1645c..6a2cd1c 100644 --- a/lib/io.go +++ b/lib/io.go @@ -19,7 +19,6 @@ package lib import ( "errors" - "github.com/gookit/color" "github.com/tlinden/tablizer/cfg" "io" "os" @@ -32,8 +31,6 @@ func ProcessFiles(c cfg.Config, args []string) error { return err } - determineColormode(&c) - for _, fd := range fds { data, err := parseFile(c, fd, pattern) if err != nil { @@ -51,18 +48,6 @@ func ProcessFiles(c cfg.Config, args []string) error { 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) { var pattern string var fds []io.Reader diff --git a/lib/printer.go b/lib/printer.go index f59bde6..06a619a 100644 --- a/lib/printer.go +++ b/lib/printer.go @@ -65,7 +65,7 @@ func printData(w io.Writer, c cfg.Config, data *Tabdata) { } func output(w io.Writer, str string) { - fmt.Fprint(w, unmaskParens(str)) + fmt.Fprint(w, str) } /*