Added support for line num flag
This commit is contained in:
13
cmd/main.go
13
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,8 +208,13 @@ func main() {
|
||||
if !(*invertFlag) && len(matchIndices) == 0 || *invertFlag && len(matchIndices) > 0 {
|
||||
continue
|
||||
} else {
|
||||
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
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// If we are substituting, we need a different behavior, as follows:
|
||||
|
Reference in New Issue
Block a user