interactive mode
This commit is contained in:
98
main.go
98
main.go
@@ -6,6 +6,7 @@ import (
|
||||
"fmt"
|
||||
"os"
|
||||
"sort"
|
||||
"strings"
|
||||
"sync"
|
||||
)
|
||||
|
||||
@@ -334,28 +335,91 @@ func main() {
|
||||
regexModePtr := flag.Bool("ex", false, "set if using g2eb expressions, leave for full string matching")
|
||||
noHighlightPtr := flag.Bool("noh", false, "no hightlght")
|
||||
filePtr := flag.String("f", "", "specify a file path to read file instead of stdin")
|
||||
interactivePtr := flag.Bool("it", false, "Interactive mode - ignores all other flags")
|
||||
|
||||
flag.Parse()
|
||||
|
||||
pattern := flag.Args()[0]
|
||||
var in *os.File
|
||||
if *filePtr != "" {
|
||||
f, err := os.Open(*filePtr)
|
||||
if err != nil {
|
||||
_ = fmt.Errorf("Could not open file %s", *filePtr)
|
||||
os.Exit(-1)
|
||||
}
|
||||
in = f
|
||||
} else {
|
||||
in = os.Stdin
|
||||
}
|
||||
m := processReader(in, pattern, *regexModePtr)
|
||||
if *interactivePtr {
|
||||
currentMode := false // false - full match, true - exp
|
||||
m := "full match"
|
||||
var currentPattern string
|
||||
var in *os.File
|
||||
var filename string
|
||||
running := true
|
||||
reader := bufio.NewReader(os.Stdin)
|
||||
for running {
|
||||
fmt.Print(">>> ")
|
||||
line, _ := reader.ReadString('\n') // read until newline (or EOF)
|
||||
args := strings.Fields(line)
|
||||
fmt.Println(args)
|
||||
if len(args) == 0 {
|
||||
continue
|
||||
}
|
||||
switch args[0] {
|
||||
case "exit":
|
||||
running = false
|
||||
case "mode":
|
||||
currentMode = !currentMode
|
||||
if currentMode {
|
||||
m = "expression match"
|
||||
} else {
|
||||
m = "full match"
|
||||
}
|
||||
fmt.Printf("Current mode: %s\n", m)
|
||||
case "pattern":
|
||||
currentPattern = args[1]
|
||||
fmt.Printf("Current pattern: %s\n", currentPattern)
|
||||
case "file":
|
||||
filename = args[1]
|
||||
a, err := os.Open(args[1])
|
||||
if err != nil {
|
||||
fmt.Printf("Could not open file %s\n", args[1])
|
||||
}
|
||||
defer a.Close()
|
||||
in = a
|
||||
fmt.Println("Successfully opened file")
|
||||
case "run":
|
||||
m := processReader(in, currentPattern, currentMode)
|
||||
|
||||
for _, lm := range m {
|
||||
if *noHighlightPtr {
|
||||
fmt.Println(lm.Line)
|
||||
for _, lm := range m {
|
||||
if *noHighlightPtr {
|
||||
fmt.Println(lm.Line)
|
||||
} else {
|
||||
fmt.Println(highlight(lm.Line, lm.Start, lm.End, green))
|
||||
}
|
||||
}
|
||||
case "help":
|
||||
fmt.Printf("Mode: %s, Pattern: %s, File: %s\n\n", m, currentPattern, filename)
|
||||
fmt.Println("mode - toggles expression mode")
|
||||
fmt.Println("pattern <pattern> - sets pattern")
|
||||
fmt.Println("file <filename> - sets file")
|
||||
fmt.Println("run - runs the search")
|
||||
fmt.Println("exit")
|
||||
default:
|
||||
}
|
||||
}
|
||||
} else {
|
||||
pattern := flag.Args()[0]
|
||||
var in *os.File
|
||||
if *filePtr != "" {
|
||||
f, err := os.Open(*filePtr)
|
||||
if err != nil {
|
||||
_ = fmt.Errorf("Could not open file %s", *filePtr)
|
||||
os.Exit(-1)
|
||||
}
|
||||
defer f.Close()
|
||||
in = f
|
||||
} else {
|
||||
fmt.Println(highlight(lm.Line, lm.Start, lm.End, green))
|
||||
in = os.Stdin
|
||||
}
|
||||
m := processReader(in, pattern, *regexModePtr)
|
||||
|
||||
for _, lm := range m {
|
||||
if *noHighlightPtr {
|
||||
fmt.Println(lm.Line)
|
||||
} else {
|
||||
fmt.Println(highlight(lm.Line, lm.Start, lm.End, green))
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user