add output colorization support using regexes

This commit is contained in:
2026-01-19 13:38:37 +01:00
parent 46fde289f5
commit cb3921458a
8 changed files with 86 additions and 23 deletions

View File

@@ -1,5 +1,5 @@
/*
Copyright © 2022-2025 Thomas von Dein
Copyright © 2022-2026 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
@@ -102,10 +102,14 @@ type Config struct {
SortByColumn string // 1,2
UseSortByColumn []int // []int{1,2}
TransposeColumns string // 1,2
UseTransposeColumns []int // []int{1,2}
Transposers []string // []string{"/ /-/", "/foo/bar/"}
UseTransposers []Transposer // {Search: re, Replace: string}
TransposeColumns string // 1,2
UseTransposeColumns []int // []int{1,2}
Transposers []string // []string{"/ /-/", "/foo/bar/"}
UseTransposers []Transposer // {Search: re, Replace: string}
Colorizers []string // []string{"/ /-/", "/foo/fg[:bg]/"}
UseColorizers []Transposer // {Search: re, Replace: color}
/*
FIXME: make configurable somehow, config file or ENV
@@ -357,6 +361,23 @@ func (conf *Config) PrepareTransposers() error {
return nil
}
func (conf *Config) PrepareColorizers() error {
for _, colorizer := range conf.Colorizers {
parts := strings.Split(colorizer, string(colorizer[0]))
if len(parts) != 4 {
return fmt.Errorf("colorizer function must have the format /regexp/foreground-color[:background-color]/")
}
conf.UseColorizers = append(conf.UseColorizers,
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.