From a8c9ede77e7509ecde5522768f585b647ae6ca9f Mon Sep 17 00:00:00 2001 From: Thomas von Dein Date: Tue, 21 Nov 2023 11:40:55 +0100 Subject: [PATCH] added -L flag to highligh lines in alternating bg color --- cfg/config.go | 9 +++++++-- cmd/root.go | 1 + cmd/tablizer.go | 12 ++++++------ lib/helpers.go | 27 ++++++++++++++++++++++----- tablizer.1 | 7 ++++--- tablizer.pod | 5 +++-- 6 files changed, 43 insertions(+), 18 deletions(-) diff --git a/cfg/config.go b/cfg/config.go index beb8fad..94e6e20 100644 --- a/cfg/config.go +++ b/cfg/config.go @@ -22,7 +22,6 @@ import ( "os" "regexp" - "github.com/glycerine/zygomys/zygo" "github.com/gookit/color" ) @@ -46,6 +45,7 @@ type Config struct { Pattern string PatternR *regexp.Regexp UseFuzzySearch bool + UseHighlight bool SortMode string SortDescending bool @@ -55,7 +55,8 @@ type Config struct { FIXME: make configurable somehow, config file or ENV see https://github.com/gookit/color. */ - ColorStyle color.Style + ColorStyle color.Style + HighlightStyle color.Style NoColor bool @@ -104,13 +105,16 @@ func Colors() map[color.Level]map[string]color.Color { return map[color.Level]map[string]color.Color{ color.Level16: { "bg": color.BgGreen, "fg": color.FgBlack, + "hlbg": color.BgGray, "hlfg": color.FgWhite, }, color.Level256: { "bg": color.BgLightGreen, "fg": color.FgBlack, + "hlbg": color.BgGray, "hlfg": color.FgWhite, }, color.LevelRgb: { // FIXME: maybe use something nicer "bg": color.BgLightGreen, "fg": color.FgBlack, + "hlbg": color.BgGray, "hlfg": color.FgWhite, }, } } @@ -123,6 +127,7 @@ func (c *Config) DetermineColormode() { level := color.TermColorLevel() colors := Colors() c.ColorStyle = color.New(colors[level]["bg"], colors[level]["fg"]) + c.HighlightStyle = color.New(colors[level]["hlbg"], colors[level]["hlfg"]) } } diff --git a/cmd/root.go b/cmd/root.go index d3bb98b..6557ef3 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -118,6 +118,7 @@ func Execute() { rootCmd.PersistentFlags().BoolVarP(&conf.InvertMatch, "invert-match", "v", false, "select non-matching rows") rootCmd.PersistentFlags().BoolVarP(&ShowManual, "man", "m", false, "Display manual page") rootCmd.PersistentFlags().BoolVarP(&conf.UseFuzzySearch, "fuzzy", "z", false, "Use fuzzy searching") + rootCmd.PersistentFlags().BoolVarP(&conf.UseHighlight, "highlight-lines", "L", false, "Use alternating background colors") rootCmd.PersistentFlags().StringVarP(&ShowCompletion, "completion", "", "", "Display completion code") rootCmd.PersistentFlags().StringVarP(&conf.Separator, "separator", "s", cfg.DefaultSeparator, "Custom field separator") rootCmd.PersistentFlags().StringVarP(&conf.Columns, "columns", "c", "", "Only show the speficied columns (separated by ,)") diff --git a/cmd/tablizer.go b/cmd/tablizer.go index 300e56c..8811831 100644 --- a/cmd/tablizer.go +++ b/cmd/tablizer.go @@ -22,10 +22,11 @@ SYNOPSIS -X, --extended Enable extended output -M, --markdown Enable markdown table output -O, --orgtbl Enable org-mode table output - -S, --shell Enable shell evaluable ouput + -S, --shell Enable shell evaluable output -Y, --yaml Enable yaml output -C, --csv Enable CSV output -A, --ascii Default output mode, ascii tabular + -L, --hightlight-lines Use alternating background colors for tables Sort Mode Flags (mutually exclusive): -a, --sort-age sort according to age (duration) string @@ -38,7 +39,7 @@ SYNOPSIS -d, --debug Enable debugging -h, --help help for tablizer -m, --man Display manual page - -v, --version Print program version + -V, --version Print program version DESCRIPTION Many programs generate tabular output. But sometimes you need to @@ -313,10 +314,11 @@ Output Flags (mutually exclusive): -X, --extended Enable extended output -M, --markdown Enable markdown table output -O, --orgtbl Enable org-mode table output - -S, --shell Enable shell evaluable ouput + -S, --shell Enable shell evaluable output -Y, --yaml Enable yaml output -C, --csv Enable CSV output -A, --ascii Default output mode, ascii tabular + -L, --hightlight-lines Use alternating background colors for tables Sort Mode Flags (mutually exclusive): -a, --sort-age sort according to age (duration) string @@ -326,12 +328,10 @@ Sort Mode Flags (mutually exclusive): Other Flags: --completion Generate the autocompletion script for - -l --load-path Where to search for lisp plugins. Maybe a file or - a directory containing files with *.zy extension -d, --debug Enable debugging -h, --help help for tablizer -m, --man Display manual page - -v, --version Print program version + -V, --version Print program version ` diff --git a/lib/helpers.go b/lib/helpers.go index dff6fbc..90707a0 100644 --- a/lib/helpers.go +++ b/lib/helpers.go @@ -20,13 +20,14 @@ package lib import ( "errors" "fmt" - "github.com/gookit/color" - "github.com/tlinden/tablizer/cfg" "os" "regexp" "sort" "strconv" "strings" + + "github.com/gookit/color" + "github.com/tlinden/tablizer/cfg" ) func contains(s []int, e int) bool { @@ -155,9 +156,25 @@ func trimRow(row []string) []string { 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.ReplaceAllStringFunc(output, func(in string) string { - return c.ColorStyle.Sprint(in) - }) + highlight := true + colorized := "" + + for _, line := range strings.Split(output, "\n") { + if c.UseHighlight { + if highlight { + line = c.HighlightStyle.Sprint(line) + } + highlight = !highlight + } else { + line = r.ReplaceAllStringFunc(line, func(in string) string { + return c.ColorStyle.Sprint(in) + }) + } + + colorized += line + "\n" + } + + return colorized } else { return output } diff --git a/tablizer.1 b/tablizer.1 index c651e8d..d60e3f0 100644 --- a/tablizer.1 +++ b/tablizer.1 @@ -133,7 +133,7 @@ .\" ======================================================================== .\" .IX Title "TABLIZER 1" -.TH TABLIZER 1 "2023-05-06" "1" "User Commands" +.TH TABLIZER 1 "2023-11-21" "1" "User Commands" .\" For nroff, turn off justification. Always turn off hyphenation; it makes .\" way too many mistakes in technical documents. .if n .ad l @@ -160,10 +160,11 @@ tablizer \- Manipulate tabular output of other programs \& \-X, \-\-extended Enable extended output \& \-M, \-\-markdown Enable markdown table output \& \-O, \-\-orgtbl Enable org\-mode table output -\& \-S, \-\-shell Enable shell evaluable ouput +\& \-S, \-\-shell Enable shell evaluable output \& \-Y, \-\-yaml Enable yaml output \& \-C, \-\-csv Enable CSV output \& \-A, \-\-ascii Default output mode, ascii tabular +\& \-L, \-\-hightlight\-lines Use alternating background colors for tables \& \& Sort Mode Flags (mutually exclusive): \& \-a, \-\-sort\-age sort according to age (duration) string @@ -176,7 +177,7 @@ tablizer \- Manipulate tabular output of other programs \& \-d, \-\-debug Enable debugging \& \-h, \-\-help help for tablizer \& \-m, \-\-man Display manual page -\& \-v, \-\-version Print program version +\& \-V, \-\-version Print program version .Ve .SH "DESCRIPTION" .IX Header "DESCRIPTION" diff --git a/tablizer.pod b/tablizer.pod index b75c080..7c72985 100644 --- a/tablizer.pod +++ b/tablizer.pod @@ -21,10 +21,11 @@ tablizer - Manipulate tabular output of other programs -X, --extended Enable extended output -M, --markdown Enable markdown table output -O, --orgtbl Enable org-mode table output - -S, --shell Enable shell evaluable ouput + -S, --shell Enable shell evaluable output -Y, --yaml Enable yaml output -C, --csv Enable CSV output -A, --ascii Default output mode, ascii tabular + -L, --hightlight-lines Use alternating background colors for tables Sort Mode Flags (mutually exclusive): -a, --sort-age sort according to age (duration) string @@ -37,7 +38,7 @@ tablizer - Manipulate tabular output of other programs -d, --debug Enable debugging -h, --help help for tablizer -m, --man Display manual page - -v, --version Print program version + -V, --version Print program version =head1 DESCRIPTION