-
Notifications
You must be signed in to change notification settings - Fork 2
/
work.go
134 lines (120 loc) · 2.52 KB
/
work.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
package main
import (
"bufio"
"fmt"
"os"
"runtime"
"strings"
"time"
)
type Search struct {
execText string //排除文本
hasExec bool
parseFileCount uint
totalFileCount uint
startTime time.Time
findCount uint
searchFile string
searchPath string
searchText string
regFile bool
suffix string
}
func New(file, path, text, execText string) *Search {
sea := Search{
searchPath: path,
searchFile: file,
searchText: text,
}
if strings.Contains(file, "*") {
sea.suffix = strings.ReplaceAll(file, "*", "")
sea.regFile = true
}
sea.execText = execText
sea.hasExec = execText != ""
return &sea
}
func (s *Search) check(fileName string) bool {
if s.regFile {
return strings.HasSuffix(fileName, s.suffix)
}
return fileName == s.searchFile
}
func (s *Search) echoInfo() {
//查找到以下结果
fmt.Printf("总共搜索:%v个文件,搜索到:%v处结果,总计耗时:%v\n", s.totalFileCount, s.findCount, time.Now().Sub(s.startTime))
}
//去除前缀空格
func trimPrefixText(s *string) *string {
f := false
for {
if strings.HasPrefix(*s, "\\O") {
*s = strings.TrimPrefix(*s, "\\O")
f = true
} else {
f = false
}
if strings.HasPrefix(*s, "\t") {
*s = strings.TrimPrefix(*s, "\t")
f = true
}
if f == false {
goto Exit
}
}
Exit:
return s
}
func (s *Search) find(path string) {
if strings.HasSuffix(path, "\\") {
path = path[0 : len(path)-1]
}
dir, err := os.ReadDir(path)
if err != nil {
return
}
for _, file := range dir {
abs := ""
if runtime.GOOS == "darwin" {
abs = path + "/" + file.Name()
} else {
abs = path + "\\" + file.Name()
}
if file.IsDir() {
s.find(abs)
} else {
if s.check(file.Name()) {
s.totalFileCount++
file, err := os.Open(abs)
if err != nil {
return
}
scanner := bufio.NewScanner(file)
var line uint = 1
for scanner.Scan() {
if s.hasExec {
if strings.Contains(scanner.Text(), s.searchText) && !strings.Contains(scanner.Text(), s.execText) {
text := scanner.Text()
fmt.Printf("%v:%v %v\n ", abs, line, "\t"+*trimPrefixText(&text))
s.findCount++
}
} else {
if strings.Contains(scanner.Text(), s.searchText) {
text := scanner.Text()
fmt.Printf("%v:%v %v\n ", abs, line, "\t"+*trimPrefixText(&text))
s.findCount++
}
}
line++
}
file.Close()
s.parseFileCount++
}
}
}
}
func (s *Search) Run() {
s.startTime = time.Now()
s.find(s.searchPath)
s.echoInfo()
}