From c689210b3c993aa29affb2503cbc11795c1dd349 Mon Sep 17 00:00:00 2001 From: Eugen Dueck Date: Fri, 15 Mar 2024 22:09:58 +0900 Subject: [PATCH] change from "first pattern wins" to "last pattern wins", because it feels more intuitive --- README.md | 2 +- colorexp.go | 6 ++++-- 2 files changed, 5 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index 4596b8f..e6cc3d3 100644 --- a/README.md +++ b/README.md @@ -4,7 +4,7 @@ something that is not easy to achieve with tools like `grep` and `less`. **Colorexp** - uses the Go regexp format, as documented [here](https://pkg.go.dev/regexp/syntax). -- supports overlapping matches (the color of the first matching pattern will be used) +- supports overlapping matches (the color for the last pattern that matches will be used) # Usage ``` diff --git a/colorexp.go b/colorexp.go index 977c14b..618feb9 100644 --- a/colorexp.go +++ b/colorexp.go @@ -8,7 +8,7 @@ import ( "regexp" ) -const version = "1.0.1" +const version = "1.0.2" var foregroundColors = []string{ //"\033[30m", // Black @@ -160,6 +160,7 @@ func main() { os.Exit(1) } + // Note that the order in regexps is the reverse of the original order, to implement the "last regexp wins" logic var regexps []*regexp.Regexp for _, regexString := range regexStrings { if fixedStrings { @@ -173,7 +174,8 @@ func main() { _, _ = fmt.Printf("Invalid regular expression: %v\n", err) os.Exit(1) } - regexps = append(regexps, re) + // insert at the beginning of the slice, to revert the order + regexps = append([]*regexp.Regexp{re}, regexps...) } var colors []string