Added support for -o flag: only print matching content
This commit is contained in:
18
main.go
18
main.go
@@ -372,7 +372,10 @@ func thompson(re []postfixNode) *State {
|
||||
|
||||
func main() {
|
||||
invertFlag := flag.Bool("v", false, "Invert match.")
|
||||
//onlyFlag := flag.Bool("o", false, "Print only colored content.")
|
||||
// This flag has two 'modes':
|
||||
// 1. Without '-v': Prints only matches. Prints a newline after every match.
|
||||
// 2. With '-v': Substitutes all matches with empty string.
|
||||
onlyFlag := flag.Bool("o", false, "Print only colored content.")
|
||||
flag.Parse()
|
||||
|
||||
// Process:
|
||||
@@ -419,8 +422,19 @@ func main() {
|
||||
for i, c := range test_str {
|
||||
if indicesToPrint.contains(i) {
|
||||
color.New(color.FgRed).Fprintf(out, "%c", c)
|
||||
// Newline after every match - only if -v is disabled.
|
||||
if !(*invertFlag) {
|
||||
for _, idx := range matchIndices {
|
||||
if i+1 == idx.endIdx { // End index is one more than last index of match
|
||||
fmt.Fprintf(out, "\n")
|
||||
break
|
||||
}
|
||||
}
|
||||
}
|
||||
} else {
|
||||
fmt.Fprintf(out, "%c", c)
|
||||
if !(*onlyFlag) {
|
||||
fmt.Fprintf(out, "%c", c)
|
||||
}
|
||||
}
|
||||
}
|
||||
err = out.Flush()
|
||||
|
Reference in New Issue
Block a user