-
Notifications
You must be signed in to change notification settings - Fork 0
/
cmd.go
172 lines (157 loc) · 3.72 KB
/
cmd.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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
package main
import (
"flag"
"fmt"
"github.com/gookit/color"
"os"
"os/exec"
"strings"
)
const v = "1.2.0"
var (
commit = flag.String("commit", "", "the commit on which you want to findKeyword")
ignoreCase = flag.Bool("ignore-case", false, "ignore case sensitivity")
removed = flag.Bool("removed", false, "include removed lines")
indexed = flag.Bool("indexed", false, "indexed output")
version = flag.Bool("version", false, "prints current version")
)
func main() {
flag.Parse()
if *version {
fmt.Printf("Version: %s\n", v)
os.Exit(0)
}
args := notBlankStrings(flag.Args())
searchTerms := deduplicate(args, *ignoreCase)
if len(searchTerms) == 0 {
fmt.Fprintln(os.Stderr, "missing search terms")
os.Exit(1)
}
diffOutput, err := gitDiff(*commit)
if err != nil {
panic(err)
}
diff, err := parseDiff(diffOutput)
if err != nil {
panic(err)
}
fo := findOptions{
ignoreCase: *ignoreCase,
removed: *removed,
}
c := find(diff, searchTerms, fo)
done := false
for !done {
r, ok := <-c
done = !ok
if ok {
printFindResult(r)
}
}
}
func gitDiff(commit string) (string, error) {
c := exec.Command("git", "--no-pager", "diff")
if commit != "" {
c.Args = append(c.Args, commit+"~1", commit)
}
dir, err := os.Getwd()
if err != nil {
return "", err
}
c.Dir = dir
output, err := c.Output()
if err != nil {
return "", err
}
return string(output), nil
}
func printFindResult(r findResult) {
if len(r.occurrences) == 0 {
return
}
if r.file.newFilename == r.file.oldFilename {
color.Bluef("%s\n", r.file.newFilename)
} else {
if r.file.newFilename == "" {
color.Bluef("[old] %s\n", r.file.oldFilename)
} else if r.file.oldFilename == "" {
color.Bluef("[new] %s\n", r.file.newFilename)
} else {
color.Bluef("%s -> %s\n", r.file.oldFilename, r.file.newFilename)
}
}
for _, lo := range r.occurrences {
if lo.line.added {
if *indexed {
color.Greenf("+ %s:%d\t|", r.file.newFilename, lo.line.lineNumber)
} else {
color.Greenf("+ %d\t|", lo.line.lineNumber)
}
printLineByHighlightingOccurrences(lo.line.content, lo.occurrences, color.Green, color.Yellow)
} else if *removed {
if *indexed {
color.Redf("- %s:%d\t|", r.file.oldFilename, lo.line.lineNumber)
} else {
color.Redf("- %d\t|", lo.line.lineNumber)
}
printLineByHighlightingOccurrences(lo.line.content, lo.occurrences, color.Red, color.Yellow)
}
}
}
func printLineByHighlightingOccurrences(s string, lo map[string][]uint, pc, sc color.Color) {
occurrenceIndexPairs := make([][2]uint, 0, 10)
i := 0
for st, indexes := range lo {
stLen := uint(len(st))
for _, index := range indexes {
occurrenceIndexPairs = append(occurrenceIndexPairs, [2]uint{index, index + stLen})
i++
}
}
for i := 0; i < len(occurrenceIndexPairs); i++ {
for j := 0; j < len(occurrenceIndexPairs); j++ {
if occurrenceIndexPairs[i][0] < occurrenceIndexPairs[j][0] {
pair := occurrenceIndexPairs[j]
occurrenceIndexPairs[j] = occurrenceIndexPairs[i]
occurrenceIndexPairs[i] = pair
}
}
}
index := uint(0)
for _, p := range occurrenceIndexPairs {
if index >= p[1] {
continue
}
if index >= p[0] {
sc.Print(s[index:p[1]])
} else {
pc.Print(s[index:p[0]])
sc.Print(s[p[0]:p[1]])
}
index = p[1]
}
pc.Println(s[index:])
}
func deduplicate(ss []string, ignoreCase bool) []string {
m := make(map[string]any)
for _, s := range ss {
if ignoreCase {
s = strings.ToLower(s)
}
m[s] = nil
}
uss := make([]string, 0, len(m))
for s := range m {
uss = append(uss, s)
}
return uss
}
func notBlankStrings(ss []string) []string {
ness := make([]string, 0, len(ss))
for _, s := range ss {
if len(strings.TrimSpace(s)) != 0 {
ness = append(ness, s)
}
}
return ness
}