colorize-output using regexes, refactor line colorization (#49)

This commit is contained in:
T. von Dein
2026-01-19 14:05:28 +01:00
parent 46fde289f5
commit 834892e302
11 changed files with 143 additions and 60 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
@@ -28,7 +28,7 @@ import (
)
const (
Version = "v1.5.12"
Version = "v1.6.0"
MAXPARTS = 2
)
@@ -102,15 +102,15 @@ 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
see https://github.com/gookit/color.
*/
ColorStyle color.Style
HighlightStyle color.Style
NoHighlightStyle color.Style
@@ -357,6 +357,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.