Change when a newline is printed; so that we don't print extraneous newlinesraneous newlines

master
Aadhavan Srinivasan 3 weeks ago
parent 38c842cb07
commit 7b31031553

@ -204,13 +204,15 @@ func main() {
color.New(color.FgRed).Fprintf(out, "%c", c) color.New(color.FgRed).Fprintf(out, "%c", c)
// Newline after every match - only if -o is enabled and -v is disabled. // Newline after every match - only if -o is enabled and -v is disabled.
if *onlyFlag && !(*invertFlag) { if *onlyFlag && !(*invertFlag) {
for _, idx := range matchIndices { for matchIdxNum, idx := range matchIndices {
if matchIdxNum < len(matchIndices)-1 { // Only print a newline afte printing a match, if there are multiple matches on the line, and we aren't on the last one. This is because the newline that gets added at the end will take care of that.
if i+1 == idx[0].EndIdx { // End index is one more than last index of match if i+1 == idx[0].EndIdx { // End index is one more than last index of match
fmt.Fprintf(out, "\n") fmt.Fprintf(out, "\n")
break break
} }
} }
} }
}
} else { } else {
if !(*onlyFlag) { if !(*onlyFlag) {
fmt.Fprintf(out, "%c", c) fmt.Fprintf(out, "%c", c)
@ -222,6 +224,10 @@ func main() {
if err != nil { if err != nil {
panic(err) panic(err)
} }
// If the last character in the string wasn't a newline, AND we either have don't -o set or we do (and we've matched something), then print a newline
if (len(test_str_runes) > 0 && test_str_runes[len(test_str_runes)-1] != '\n') &&
(!*onlyFlag || indicesToPrint.len() > 0) {
fmt.Println() fmt.Println()
} }
} }
}

Loading…
Cancel
Save