From 8a0586d107f24deaf487fca7706c98412db06115 Mon Sep 17 00:00:00 2001 From: Rockingcool Date: Fri, 3 Jan 2025 15:49:14 -0600 Subject: [PATCH] Added support for printing specific match indices ('-m' and '-p' flags combined) --- main.go | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/main.go b/main.go index 53ff644..7742649 100644 --- a/main.go +++ b/main.go @@ -705,8 +705,14 @@ func main() { if !(*multiLineFlag) { fmt.Fprintf(out, "Line %d:\n", lineNum) } - for _, idx := range matchIndices { - fmt.Fprintf(out, "%s\n", idx.toString()) + for i, m := range matchIndices { + // Only print a match if: + // a. We are _not_ printing just one match + // OR + // b. We _are_ printing just one match, and this is that match + if !matchNumFlagEnabled || (i+1) == *matchNum { // Match indexes start from 1; loop counter starts from 0 + fmt.Fprintf(out, "%s\n", m.toString()) + } } err := out.Flush() if err != nil {