added transpose function (-T + -R)

This commit is contained in:
2025-01-12 19:28:52 +01:00
committed by T.v.Dein
parent 8792c5a40f
commit 4d894a728b
9 changed files with 271 additions and 134 deletions

View File

@@ -49,6 +49,11 @@ type Settings struct {
HighlightHdrBG string `hcl:"HighlightHdrBG"`
}
type Transposer struct {
Search regexp.Regexp
Replace string
}
// internal config
type Config struct {
Debug bool
@@ -68,6 +73,11 @@ type Config struct {
SortDescending bool
SortByColumn int
TransposeColumns string // 1,2
UseTransposeColumns []int // []int{1,2}
Transposers []string // []string{"/ /-/", "/foo/bar/"}
UseTransposers []Transposer // {Search: re, Replace: string}
/*
FIXME: make configurable somehow, config file or ENV
see https://github.com/gookit/color.
@@ -283,6 +293,29 @@ func (conf *Config) PrepareFilters() error {
return nil
}
// check if transposers match transposer columns and prepare transposer structs
func (conf *Config) PrepareTransposers() error {
if len(conf.Transposers) != len(conf.UseTransposeColumns) {
return fmt.Errorf("the number of transposers needs to correspond to the number of transpose columns: %d != %d",
len(conf.Transposers), len(strings.Split(conf.TransposeColumns, ",")))
}
for _, transposer := range conf.Transposers {
parts := strings.Split(transposer, "/")
if len(parts) != 4 {
return fmt.Errorf("transposer function must have the format /regexp/replace-string/")
}
conf.UseTransposers = append(conf.UseTransposers,
Transposer{
Search: *regexp.MustCompile(parts[1]),
Replace: parts[2]},
)
}
return nil
}
func (conf *Config) CheckEnv() {
// check for environment vars, command line flags have precedence,
// NO_COLOR is being checked by the color module itself.