Skip to content

Commit

Permalink
add 'full-match-highlight' option
Browse files Browse the repository at this point in the history
  • Loading branch information
EugenDueck committed Apr 15, 2024
1 parent e96eca5 commit acbf88e
Showing 1 changed file with 22 additions and 7 deletions.
29 changes: 22 additions & 7 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.7"
const version = "2"

var foregroundColors = []string{
//"\033[30m", // Black
Expand Down Expand Up @@ -91,17 +91,22 @@ func addRange(ranges []rangeWithID, newRange rangeWithID) []rangeWithID {
return result
}

func match(line string, regexps []*regexp.Regexp, varyGroupColors bool) []rangeWithID {
func match(line string, regexps []*regexp.Regexp, varyGroupColors, fullMatchHighlight bool) []rangeWithID {
var ranges []rangeWithID
colorIdx := 0
for _, re := range regexps {
numGroups := re.NumSubexp()
matchRanges := re.FindAllStringSubmatchIndex(line, -1)
firstGroupToColorize := min(1, numGroups)
groupsToColorize := numGroups + 1 - firstGroupToColorize
if fullMatchHighlight {
firstGroupToColorize = 0
groupsToColorize = 1
}
for _, matchRange := range matchRanges {
// if there is no capturing group, the full match will be colorized (group 0)
// if there are capturing groups, all groups but group 0 (the full match) will be colorized
// if there are capturing groups, all groups but group 0 (the full match) will be colorized, unless
// fullMatchHighlight == true
for i := 0; i < groupsToColorize; i++ {
curColorIdx := colorIdx
if varyGroupColors {
Expand Down Expand Up @@ -154,6 +159,7 @@ func printUsage() {
func main() {
var (
fixedStrings bool
fullMatchHighlight bool
showHelp bool
noHighlight bool
onlyHighlight bool
Expand All @@ -165,6 +171,7 @@ func main() {
)

pflag.BoolVarP(&fixedStrings, "fixed-strings", "F", false, "Do not interpret regular expression metacharacters.")
pflag.BoolVarP(&fullMatchHighlight, "full-match-highlight", "f", false, "Highlight the entire match, even if pattern contains capturing groups.")
pflag.BoolVarP(&showHelp, "help", "", false, "Display this help and exit.")
pflag.BoolVarP(&noHighlight, "no-highlight", "h", false, "Do not color by changing the background color.")
pflag.BoolVarP(&onlyHighlight, "only-highlight", "H", false, "Only color by changing the background color.")
Expand Down Expand Up @@ -194,19 +201,27 @@ func main() {
os.Exit(1)
}

if pflag.Lookup("vary-group-colors-on").Changed {
if pflag.Lookup("vary-group-colors-off").Changed {
varyGroupColorsOnChanged := pflag.Lookup("vary-group-colors-on").Changed
varyGroupColorsOffChanged := pflag.Lookup("vary-group-colors-off").Changed
if varyGroupColorsOnChanged {
if varyGroupColorsOffChanged {
_, _ = fmt.Printf("Error: -g/-G arguments cannot both be used at the same time.\n\n")
printUsage()
os.Exit(1)
}
varyGroupColors = true
} else if pflag.Lookup("vary-group-colors-off").Changed {
} else if varyGroupColorsOffChanged {
varyGroupColors = false
} else {
varyGroupColors = len(regexStrings) == 1
}

if fullMatchHighlight && (varyGroupColorsOnChanged || varyGroupColorsOffChanged) {
_, _ = fmt.Printf("Error: -f and -g/-G arguments cannot both be used at the same time.\n\n")
printUsage()
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 {
Expand Down Expand Up @@ -253,7 +268,7 @@ func main() {
scanner := bufio.NewScanner(os.Stdin)
for scanner.Scan() {
line := scanner.Text()
ranges := match(line, regexps, varyGroupColors)
ranges := match(line, regexps, varyGroupColors, fullMatchHighlight)
colorizedLine := colorize(line, colors, ranges, patternColorCount)
fmt.Println(colorizedLine)
}
Expand Down

0 comments on commit acbf88e

Please sign in to comment.