Skip to content

Commit

Permalink
Feature additions (#11)
Browse files Browse the repository at this point in the history
* add color table support (using alternating colorization of rows) using new flag `-L`
* add config file support (HCL format) using `~/.config/tablizer/config` or `-f <file>` so the user can customize colors
* removed golang 1.17 support
  • Loading branch information
TLINDEN authored Nov 22, 2023
1 parent 9eadb94 commit 6fccd12
Show file tree
Hide file tree
Showing 11 changed files with 388 additions and 89 deletions.
28 changes: 4 additions & 24 deletions .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ jobs:
build:
strategy:
matrix:
version: [1.17, 1.18, 1.19]
version: [1.18, 1.19]
os: [ubuntu-latest, windows-latest, macos-latest]
name: Build
runs-on: ${{ matrix.os }}
Expand All @@ -30,29 +30,9 @@ jobs:
steps:
- uses: actions/setup-go@v3
with:
go-version: 1.17
go-version: 1.18
- uses: actions/checkout@v3
- name: golangci-lint
uses: golangci/golangci-lint-action@v3
#with:
# Optional: version of golangci-lint to use in form of v1.2 or v1.2.3 or `latest` to use the latest version
# version: v1.29

# Optional: working directory, useful for monorepos
# working-directory: somedir

# Optional: golangci-lint command line arguments.
# args: --issues-exit-code=0

# Optional: show only new issues if it's a pull request. The default value is `false`.
# only-new-issues: true

# Optional: if set to true then the all caching functionality will be complete disabled,
# takes precedence over all other caching options.
# skip-cache: true

# Optional: if set to true then the action don't cache or restore ~/go/pkg.
# skip-pkg-cache: true

# Optional: if set to true then the action don't cache or restore ~/.cache/go-build.
# skip-build-cache: true
with:
skip-cache: true
157 changes: 147 additions & 10 deletions cfg/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,21 +19,36 @@ package cfg
import (
"errors"
"fmt"
"log"
"os"
"regexp"


"github.com/glycerine/zygomys/zygo"
"github.com/gookit/color"
"github.com/hashicorp/hcl/v2/hclsimple"
)

const DefaultSeparator string = `(\s\s+|\t)`
const Version string = "v1.0.17"
const Version string = "v1.1.0"

var DefaultLoadPath string = os.Getenv("HOME") + "/.config/tablizer/lisp"
var DefaultConfigfile string = os.Getenv("HOME") + "/.config/tablizer/config"

var VERSION string // maintained by -x

// public config, set via config file or using defaults
type Configuration struct {
FG string `hcl:"FG"`
BG string `hcl:"BG"`
HighlightFG string `hcl:"HighlightFG"`
HighlightBG string `hcl:"HighlightBG"`
NoHighlightFG string `hcl:"NoHighlightFG"`
NoHighlightBG string `hcl:"NoHighlightBG"`
HighlightHdrFG string `hcl:"HighlightHdrFG"`
HighlightHdrBG string `hcl:"HighlightHdrBG"`
}

// internal config
type Config struct {
Debug bool
NoNumbering bool
Expand All @@ -46,6 +61,7 @@ type Config struct {
Pattern string
PatternR *regexp.Regexp
UseFuzzySearch bool
UseHighlight bool

SortMode string
SortDescending bool
Expand All @@ -55,7 +71,10 @@ 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
NoHighlightStyle color.Style
HighlightHdrStyle color.Style

NoColor bool

Expand All @@ -65,6 +84,11 @@ type Config struct {

// a path containing lisp scripts to be loaded on startup
LispLoadPath string

// config file, optional
Configfile string

Configuration Configuration
}

// maps outputmode short flags to output mode, ie. -O => -o orgtbl
Expand Down Expand Up @@ -100,19 +124,73 @@ type Sortmode struct {
var ValidHooks []string

// default color schemes
func Colors() map[color.Level]map[string]color.Color {
return map[color.Level]map[string]color.Color{
func (c *Config) Colors() map[color.Level]map[string]color.Color {
colors := map[color.Level]map[string]color.Color{
color.Level16: {
"bg": color.BgGreen, "fg": color.FgBlack,
"bg": color.BgGreen, "fg": color.FgWhite,
"hlbg": color.BgGray, "hlfg": color.FgWhite,
},
color.Level256: {
"bg": color.BgLightGreen, "fg": color.FgBlack,
"bg": color.BgLightGreen, "fg": color.FgWhite,
"hlbg": color.BgLightBlue, "hlfg": color.FgWhite,
},
color.LevelRgb: {
// FIXME: maybe use something nicer
"bg": color.BgLightGreen, "fg": color.FgBlack,
"bg": color.BgLightGreen, "fg": color.FgWhite,
"hlbg": color.BgHiGreen, "hlfg": color.FgWhite,
"nohlbg": color.BgWhite, "nohlfg": color.FgLightGreen,
"hdrbg": color.BgBlue, "hdrfg": color.FgWhite,
},
}

if len(c.Configuration.BG) > 0 {
colors[color.Level16]["bg"] = ColorStringToBGColor(c.Configuration.BG)
colors[color.Level256]["bg"] = ColorStringToBGColor(c.Configuration.BG)
colors[color.LevelRgb]["bg"] = ColorStringToBGColor(c.Configuration.BG)
}

if len(c.Configuration.FG) > 0 {
colors[color.Level16]["fg"] = ColorStringToColor(c.Configuration.FG)
colors[color.Level256]["fg"] = ColorStringToColor(c.Configuration.FG)
colors[color.LevelRgb]["fg"] = ColorStringToColor(c.Configuration.FG)
}

if len(c.Configuration.HighlightBG) > 0 {
colors[color.Level16]["hlbg"] = ColorStringToBGColor(c.Configuration.HighlightBG)
colors[color.Level256]["hlbg"] = ColorStringToBGColor(c.Configuration.HighlightBG)
colors[color.LevelRgb]["hlbg"] = ColorStringToBGColor(c.Configuration.HighlightBG)
}

if len(c.Configuration.HighlightFG) > 0 {
colors[color.Level16]["hlfg"] = ColorStringToColor(c.Configuration.HighlightFG)
colors[color.Level256]["hlfg"] = ColorStringToColor(c.Configuration.HighlightFG)
colors[color.LevelRgb]["hlfg"] = ColorStringToColor(c.Configuration.HighlightFG)
}

if len(c.Configuration.NoHighlightBG) > 0 {
colors[color.Level16]["nohlbg"] = ColorStringToBGColor(c.Configuration.NoHighlightBG)
colors[color.Level256]["nohlbg"] = ColorStringToBGColor(c.Configuration.NoHighlightBG)
colors[color.LevelRgb]["nohlbg"] = ColorStringToBGColor(c.Configuration.NoHighlightBG)
}

if len(c.Configuration.NoHighlightFG) > 0 {
colors[color.Level16]["nohlfg"] = ColorStringToColor(c.Configuration.NoHighlightFG)
colors[color.Level256]["nohlfg"] = ColorStringToColor(c.Configuration.NoHighlightFG)
colors[color.LevelRgb]["nohlfg"] = ColorStringToColor(c.Configuration.NoHighlightFG)
}

if len(c.Configuration.HighlightHdrBG) > 0 {
colors[color.Level16]["hdrbg"] = ColorStringToBGColor(c.Configuration.HighlightHdrBG)
colors[color.Level256]["hdrbg"] = ColorStringToBGColor(c.Configuration.HighlightHdrBG)
colors[color.LevelRgb]["hdrbg"] = ColorStringToBGColor(c.Configuration.HighlightHdrBG)
}

if len(c.Configuration.HighlightHdrFG) > 0 {
colors[color.Level16]["hdrfg"] = ColorStringToColor(c.Configuration.HighlightHdrFG)
colors[color.Level256]["hdrfg"] = ColorStringToColor(c.Configuration.HighlightHdrFG)
colors[color.LevelRgb]["hdrfg"] = ColorStringToColor(c.Configuration.HighlightHdrFG)
}

return colors
}

// find supported color mode, modifies config based on constants
Expand All @@ -121,8 +199,12 @@ func (c *Config) DetermineColormode() {
color.Disable()
} else {
level := color.TermColorLevel()
colors := Colors()
colors := c.Colors()

c.ColorStyle = color.New(colors[level]["bg"], colors[level]["fg"])
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"])
}
}

Expand Down Expand Up @@ -217,3 +299,58 @@ func (c *Config) PreparePattern(pattern string) error {

return nil
}

func (c *Config) ParseConfigfile() error {
if path, err := os.Stat(c.Configfile); !os.IsNotExist(err) {
if !path.IsDir() {
configstring, err := os.ReadFile(path.Name())
if err != nil {
return err
}

err = hclsimple.Decode(
path.Name(), []byte(configstring),
nil, &c.Configuration,
)
if err != nil {
log.Fatalf("Failed to load configuration: %s", err)
}
}
}

return nil
}

// translate color string to internal color value
func ColorStringToColor(colorname string) color.Color {
for name, color := range color.FgColors {
if name == colorname {
return color
}
}

for name, color := range color.ExFgColors {
if name == colorname {
return color
}
}

return color.Normal
}

// same, for background colors
func ColorStringToBGColor(colorname string) color.Color {
for name, color := range color.BgColors {
if name == colorname {
return color
}
}

for name, color := range color.ExBgColors {
if name == colorname {
return color
}
}

return color.Normal
}
11 changes: 10 additions & 1 deletion cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,14 +92,19 @@ func Execute() {
}

// Setup
err := conf.ParseConfigfile()
if err != nil {
return err
}

conf.CheckEnv()
conf.PrepareModeFlags(modeflag)
conf.PrepareSortFlags(sortmode)
conf.DetermineColormode()
conf.ApplyDefaults()

// setup lisp env, load plugins etc
err := lib.SetupLisp(&conf)
err = lib.SetupLisp(&conf)
if err != nil {
return err
}
Expand All @@ -118,6 +123,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 ,)")
Expand Down Expand Up @@ -145,6 +151,9 @@ func Execute() {
// lisp options
rootCmd.PersistentFlags().StringVarP(&conf.LispLoadPath, "load-path", "l", cfg.DefaultLoadPath, "Load path for lisp plugins (expects *.zy files)")

// config file
rootCmd.PersistentFlags().StringVarP(&conf.Configfile, "config", "f", cfg.DefaultConfigfile, "config file (default: ~/.config/tablizer/config)")

rootCmd.SetUsageTemplate(strings.TrimSpace(usage) + "\n")

err := rootCmd.Execute()
Expand Down
44 changes: 38 additions & 6 deletions cmd/tablizer.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -35,10 +36,11 @@ SYNOPSIS
Other Flags:
--completion <shell> Generate the autocompletion script for <shell>
-f, --config <file> Configuration file (default: ~/.config/tablizer/config)
-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
Expand Down Expand Up @@ -257,6 +259,36 @@ DESCRIPTION
and source this file from your PowerShell profile.
CONFIGURATION AND COLORS
YOu can put certain configuration values into a configuration file in
HCL format. By default tablizer looks for
"$HOME/.config/tablizer/config", but you can provide one using the
parameter "-f".
In the configuration the following variables can be defined:
BG = "lightGreen"
FG = "white"
HighlightBG = "lightGreen"
HighlightFG = "white"
NoHighlightBG = "white"
NoHighlightFG = "lightGreen"
HighlightHdrBG = "red"
HighlightHdrFG = "white"
The following color definitions are available:
black, blue, cyan, darkGray, default, green, lightBlue, lightCyan,
lightGreen, lightMagenta, lightRed, lightWhite, lightYellow, magenta,
red, white, yellow
The Variables FG and BG are being used to highlight matches. The other
*FG and *BG variables are for colored table output (enabled with the
"-L" parameter).
Colorization can be turned off completely either by setting the
parameter "-N" or the environment variable NO_COLOR to a true value.
BUGS
In order to report a bug, unexpected behavior, feature requests or to
submit a patch, please open an issue on github:
Expand Down Expand Up @@ -313,10 +345,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
Expand All @@ -326,12 +359,11 @@ Sort Mode Flags (mutually exclusive):
Other Flags:
--completion <shell> Generate the autocompletion script for <shell>
-l --load-path <path> Where to search for lisp plugins. Maybe a file or
a directory containing files with *.zy extension
-f, --config <file> Configuration file (default: ~/.config/tablizer/config)
-d, --debug Enable debugging
-h, --help help for tablizer
-m, --man Display manual page
-v, --version Print program version
-V, --version Print program version
`
Loading

0 comments on commit 6fccd12

Please sign in to comment.