forked from monochromegane/the_platinum_searcher
/
print.go
174 lines (154 loc) · 3.57 KB
/
print.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
173
174
package the_platinum_searcher
import (
"fmt"
"io"
"os"
"regexp"
"github.com/shiena/ansicolor"
"golang.org/x/text/encoding/japanese"
"golang.org/x/text/transform"
)
var FileMatchCount, MatchCount uint
type PrintParams struct {
Pattern *Pattern
Path string
Matches []*Match
}
type print struct {
In chan *PrintParams
Done chan struct{}
Option *Option
writer io.Writer
decorator Decorator
}
func Print(in chan *PrintParams, done chan struct{}, option *Option) {
print := &print{
In: in,
Done: done,
Option: option,
writer: newWriter(option),
decorator: newDecorator(option),
}
print.Start()
}
func (p *print) Start() {
FileMatchCount = 0
MatchCount = 0
for arg := range p.In {
if p.Option.FilesWithRegexp != "" {
p.printPath(arg.Path)
fmt.Println()
FileMatchCount++
continue
}
if len(arg.Matches) == 0 {
continue
}
if p.Option.Count {
p.printPath(arg.Path)
var re *regexp.Regexp
pattern := arg.Pattern
if pattern.UseRegexp || pattern.IgnoreCase {
re = pattern.Regexp
} else {
re = regexp.MustCompile(regexp.QuoteMeta(pattern.Pattern))
}
count := 0
for _, m := range arg.Matches {
count += len(re.FindAllString(m.Line.Str, -1))
MatchCount++
}
p.printCount(count)
fmt.Println()
FileMatchCount++
continue
}
if p.Option.FilesWithMatches {
p.printPath(arg.Path)
fmt.Println()
FileMatchCount++
continue
}
if !p.Option.NoGroup {
p.printPath(arg.Path)
fmt.Println()
FileMatchCount++
}
lastLineNum := 0
enableContext := p.Option.Before > 0 || p.Option.After > 0
for _, v := range arg.Matches {
if v == nil {
continue
}
if enableContext {
if lastLineNum > 0 && lastLineNum+1 != v.FirstLineNum() {
fmt.Println("--")
}
lastLineNum = v.LastLineNum()
}
if p.Option.NoGroup {
p.printPath(arg.Path)
}
p.printContext(v.Befores)
p.printMatch(arg.Pattern, v)
MatchCount++
fmt.Println()
p.printContext(v.Afters)
}
if !p.Option.NoGroup {
fmt.Println()
}
}
if p.Option.Stats {
fmt.Printf("%d Files Matched\n", FileMatchCount)
fmt.Printf("%d Total Text Matches\n", MatchCount)
}
p.Done <- struct{}{}
}
func (p *print) printPath(path string) {
fmt.Fprint(p.writer, p.decorator.path(path))
if !p.Option.FilesWithMatches && p.Option.FilesWithRegexp == "" {
fmt.Fprintf(p.writer, ":")
}
}
func (p *print) printLineNumber(lineNum int, sep string) {
fmt.Fprint(p.writer, p.decorator.lineNumber(lineNum, sep))
}
func (p *print) printColumn(column int, sep string) {
fmt.Fprint(p.writer, p.decorator.column(column, sep))
}
func (p *print) printMatch(pattern *Pattern, match *Match) {
p.printLineNumber(match.Num, ":")
if p.Option.Column {
p.printColumn(match.Col, ":")
}
fmt.Fprint(p.writer, p.decorator.match(pattern, match.Line))
}
func (p *print) printCount(cnt int) {
fmt.Fprint(p.writer, p.decorator.count(cnt))
}
func (p *print) printContext(lines []*Line) {
for _, line := range lines {
p.printLineNumber(line.Num, "-")
fmt.Fprint(p.writer, line.Str)
fmt.Fprintln(p.writer)
}
}
func newWriter(option *Option) io.Writer {
encoder := func() io.Writer {
switch option.OutputEncode {
case "sjis":
return transform.NewWriter(os.Stdout, japanese.ShiftJIS.NewEncoder())
case "euc":
return transform.NewWriter(os.Stdout, japanese.EUCJP.NewEncoder())
case "jis":
return transform.NewWriter(os.Stdout, japanese.ISO2022JP.NewEncoder())
default:
return os.Stdout
}
}()
if option.EnableColor {
return ansicolor.NewAnsiColorWriter(encoder)
}
return encoder
}