From 399620de983162c8c30806dd13a29230cc124a34 Mon Sep 17 00:00:00 2001 From: Thomas von Dein Date: Wed, 19 Oct 2022 12:43:54 +0200 Subject: [PATCH] Added so we can use struct holding all configuration --- cfg/config.go | 147 +++++++++++++++++++++++++++++++++++++++++++++ cfg/config_test.go | 20 ++++++ 2 files changed, 167 insertions(+) create mode 100644 cfg/config.go create mode 100644 cfg/config_test.go diff --git a/cfg/config.go b/cfg/config.go new file mode 100644 index 0000000..7556b78 --- /dev/null +++ b/cfg/config.go @@ -0,0 +1,147 @@ +/* +Copyright © 2022 Thomas von Dein + +This program is free software: you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation, either version 3 of the License, or +(at your option) any later version. + +This program is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +GNU General Public License for more details. + +You should have received a copy of the GNU General Public License +along with this program. If not, see . +*/ +package cfg + +import ( + "errors" + "fmt" + "github.com/gookit/color" + "regexp" +) + +const DefaultSeparator string = `(\s\s+|\t)` +const ValidOutputModes string = "(orgtbl|markdown|extended|ascii|yaml)" +const Version string = "v1.0.11" + +var VERSION string // maintained by -x + +type Config struct { + Debug bool + NoNumbering bool + Columns string + UseColumns []int + Separator string + OutputMode string + InvertMatch bool + Pattern string + + SortMode string + SortDescending bool + SortByColumn int + + /* + 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. + */ + MatchFG string + MatchBG string + NoColor bool +} + +// maps outputmode short flags to output mode, ie. -O => -o orgtbl +type Modeflag struct { + X bool + O bool + M bool + S bool + Y bool + A bool +} + +// various sort types +type Sortmode struct { + Numeric bool + Time bool + Age bool +} + +func Colors() map[color.Level]map[string]string { + // default color schemes + return map[color.Level]map[string]string{ + color.Level16: { + "bg": "green", "fg": "black", + }, + color.Level256: { + "bg": "lightGreen", "fg": "black", + }, + color.LevelRgb: { + // FIXME: maybe use something nicer + "bg": "lightGreen", "fg": "black", + }, + } +} + +func Getversion() string { + // main program version + + // generated version string, used by -v contains lib.Version on + // main branch, and lib.Version-$branch-$lastcommit-$date on + // development branch + + return fmt.Sprintf("This is tablizer version %s", VERSION) +} + +func (conf *Config) PrepareModeFlags(flag Modeflag, mode string) error { + if len(mode) == 0 { + // associate short flags like -X with mode selector + switch { + case flag.X: + conf.OutputMode = "extended" + case flag.M: + conf.OutputMode = "markdown" + case flag.O: + conf.OutputMode = "orgtbl" + case flag.S: + conf.OutputMode = "shell" + conf.NoNumbering = true + case flag.Y: + conf.OutputMode = "yaml" + conf.NoNumbering = true + default: + conf.OutputMode = "ascii" + } + } else { + r, err := regexp.Compile(ValidOutputModes) + + if err != nil { + return errors.New("Failed to validate output mode spec!") + } + + match := r.MatchString(mode) + + if !match { + return errors.New("Invalid output mode!") + } + } + + return nil +} + +func (conf *Config) PrepareSortFlags(flag Sortmode) { + switch { + case flag.Numeric: + conf.SortMode = "numeric" + case flag.Age: + conf.SortMode = "duration" + case flag.Time: + conf.SortMode = "time" + default: + conf.SortMode = "string" + } +} diff --git a/cfg/config_test.go b/cfg/config_test.go new file mode 100644 index 0000000..b2f1df8 --- /dev/null +++ b/cfg/config_test.go @@ -0,0 +1,20 @@ +/* +Copyright © 2022 Thomas von Dein + +This program is free software: you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation, either version 3 of the License, or +(at your option) any later version. + +This program is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +GNU General Public License for more details. + +You should have received a copy of the GNU General Public License +along with this program. If not, see . +*/ + +package cfg + +// FIXME: add tests