Skip to content

Commit

Permalink
added config file support to set custom colors
Browse files Browse the repository at this point in the history
  • Loading branch information
Thomas von Dein committed Nov 22, 2023
1 parent 811173d commit f045adf
Show file tree
Hide file tree
Showing 8 changed files with 253 additions and 20 deletions.
136 changes: 131 additions & 5 deletions cfg/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,20 +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 Down Expand Up @@ -68,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 @@ -103,8 +124,8 @@ 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.FgWhite,
"hlbg": color.BgGray, "hlfg": color.FgWhite,
Expand All @@ -114,13 +135,62 @@ func Colors() map[color.Level]map[string]color.Color {
"hlbg": color.BgLightBlue, "hlfg": color.FgWhite,
},
color.LevelRgb: {
// FIXME: maybe use something nicer
"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 @@ -129,7 +199,8 @@ 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"])
Expand Down Expand Up @@ -228,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
}
10 changes: 9 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 Down Expand Up @@ -146,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
2 changes: 2 additions & 0 deletions cmd/tablizer.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ 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
Expand Down Expand Up @@ -328,6 +329,7 @@ Sort Mode Flags (mutually exclusive):
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
Expand Down
12 changes: 12 additions & 0 deletions config.hcl
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# supported colors:
# black, blue, cyan, darkGray, default, green, lightBlue, lightCyan,
# lightGreen, lightMagenta, lightRed, lightWhite, lightYellow,
# magenta, red, white, yellow
BG = "lightGreen"
FG = "white"
HighlightBG = "lightGreen"
HighlightFG = "white"
NoHighlightBG = "white"
NoHighlightFG = "lightGreen"
HighlightHdrBG = "red"
HighlightHdrFG = "white"
24 changes: 20 additions & 4 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,24 +5,40 @@ go 1.18
require (
github.com/alecthomas/repr v0.1.1
github.com/araddon/dateparse v0.0.0-20210429162001-6b43995a97de
github.com/glycerine/zygomys v5.1.2+incompatible
github.com/gookit/color v1.5.2
github.com/hashicorp/hcl/v2 v2.19.1
github.com/lithammer/fuzzysearch v1.1.7
github.com/olekukonko/tablewriter v0.0.5
github.com/spf13/cobra v1.6.1
gopkg.in/yaml.v3 v3.0.1
)

require (
github.com/agext/levenshtein v1.2.1 // indirect
github.com/apparentlymart/go-textseg/v13 v13.0.0 // indirect
github.com/apparentlymart/go-textseg/v15 v15.0.0 // indirect
github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc // indirect
github.com/glycerine/blake2b v0.0.0-20151022103502-3c8c640cd7be // indirect
github.com/glycerine/goconvey v0.0.0-20190410193231-58a59202ab31 // indirect
github.com/glycerine/greenpack v5.1.1+incompatible // indirect
github.com/glycerine/liner v0.0.0-20160121172638-72909af234e0 // indirect
github.com/glycerine/zygomys v5.1.2+incompatible // indirect
github.com/lithammer/fuzzysearch v1.1.7 // indirect
github.com/google/go-cmp v0.5.9 // indirect
github.com/gopherjs/gopherjs v1.17.2 // indirect
github.com/jtolds/gls v4.20.0+incompatible // indirect
github.com/kr/pretty v0.3.1 // indirect
github.com/mitchellh/go-wordwrap v0.0.0-20150314170334-ad45545899c7 // indirect
github.com/philhofer/fwd v1.1.2 // indirect
github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2 // indirect
github.com/shurcooL/go v0.0.0-20200502201357-93f07166e636 // indirect
github.com/shurcooL/go-goon v1.0.0 // indirect
github.com/stretchr/testify v1.8.4 // indirect
github.com/tinylib/msgp v1.1.8 // indirect
github.com/ugorji/go/codec v1.2.11 // indirect
golang.org/x/text v0.8.0 // indirect
github.com/zclconf/go-cty v1.13.0 // indirect
golang.org/x/exp v0.0.0-20230905200255-921286631fa9 // indirect
golang.org/x/text v0.13.0 // indirect
gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15 // indirect
)

require (
Expand All @@ -34,5 +50,5 @@ require (
github.com/rivo/uniseg v0.2.0 // indirect
github.com/spf13/pflag v1.0.5 // indirect
github.com/xo/terminfo v0.0.0-20220910002029-abceb7e1c41e // indirect
golang.org/x/sys v0.5.0 // indirect
golang.org/x/sys v0.12.0 // indirect
)
Loading

0 comments on commit f045adf

Please sign in to comment.