diff --git a/cmd/main.go b/cmd/main.go index 3edb5b8..9e1c9c0 100644 --- a/cmd/main.go +++ b/cmd/main.go @@ -61,6 +61,11 @@ func main() { panic("Invalid match number to print.") } + // Enable lineFlag if lineNumFlag is enabled + if *lineNumFlag { + *lineFlag = true + } + // Process: // 1. Convert regex into postfix notation (Shunting-Yard algorithm) // a. Add explicit concatenation operators to facilitate this @@ -87,7 +92,7 @@ func main() { for _, inputFilename := range inputFilenames { inputFile, err := os.Open(inputFilename) if err != nil { - fmt.Printf("%s: No such file or directory\n", flag.Args()[1]) + fmt.Printf("%s: No such file or directory\n", inputFilename) os.Exit(2) } inputFiles = append(inputFiles, inputFile) @@ -108,6 +113,7 @@ func main() { } for _, inputFile := range inputFiles { + lineNum = 0 reader := bufio.NewReader(inputFile) linesRead = false for true { @@ -202,7 +208,12 @@ func main() { if !(*invertFlag) && len(matchIndices) == 0 || *invertFlag && len(matchIndices) > 0 { continue } else { - color.New(color.FgMagenta).Fprintf(out, "%s: ", inputFile.Name()) // Print filename + if len(inputFiles) > 1 { // Don't need to print file name if there's only one file + color.New(color.FgMagenta).Fprintf(out, "%s:", inputFile.Name()) // Print filename + } + if *lineNumFlag { + color.New(color.FgGreen).Fprintf(out, "%d:", lineNum) // Print filename + } } }