Skip to content

Commit

Permalink
change from "first pattern wins" to "last pattern wins", because it f…
Browse files Browse the repository at this point in the history
…eels more intuitive
  • Loading branch information
EugenDueck committed Mar 15, 2024
1 parent 2311735 commit c689210
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 3 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
```
Expand Down
6 changes: 4 additions & 2 deletions colorexp.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (
"regexp"
)

const version = "1.0.1"
const version = "1.0.2"

var foregroundColors = []string{
//"\033[30m", // Black
Expand Down Expand Up @@ -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 {
Expand All @@ -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
Expand Down

0 comments on commit c689210

Please sign in to comment.