-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.go
51 lines (39 loc) · 837 Bytes
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
package main
import (
"fmt"
"os"
"os/exec"
"strings"
)
func main() {
if len(os.Args) < 2 {
return
}
//Only take the 3rd argument and ignore the rest
query := os.Args[1]
elements := strings.SplitN(query, ">", 2)
count := len(elements)
commandArgs := []string{
"--column",
"--line-number",
"--no-heading",
"--color=always",
"--smart-case",
}
if count == 1 {
commandArgs = append(commandArgs, "--", elements[0])
} else {
globs := strings.Fields(elements[0])
for _, glob := range globs {
commandArgs = append(commandArgs, "--iglob", glob)
}
commandArgs = append(commandArgs, "--", strings.Trim(elements[1], " "))
}
output, err := exec.Command("rg", commandArgs...).Output()
if err != nil {
fmt.Printf("No result. Error: %s\n", err)
return
} else {
fmt.Printf(string(output))
}
}