forked from monochromegane/the_platinum_searcher
-
Notifications
You must be signed in to change notification settings - Fork 0
/
formatter.go
165 lines (148 loc) · 3.68 KB
/
formatter.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
package the_platinum_searcher
import (
"fmt"
"io"
"github.com/shiena/ansicolor"
"golang.org/x/text/encoding/japanese"
"golang.org/x/text/transform"
)
type formatPrinter interface {
print(match match)
}
func newFormatPrinter(pattern pattern, w io.Writer, opts Option) formatPrinter {
writer := newWriter(w, opts)
decorator := newDecorator(pattern, opts)
switch {
case opts.SearchOption.SearchStream:
return matchLine{decorator: decorator, w: writer}
case opts.OutputOption.FilesWithMatches:
return fileWithMatch{decorator: decorator, w: writer, useNull: opts.OutputOption.Null}
case opts.OutputOption.Count:
return count{decorator: decorator, w: writer}
case opts.OutputOption.EnableGroup:
return group{decorator: decorator, w: writer, useNull: opts.OutputOption.Null, enableLineNumber: opts.OutputOption.EnableLineNumber}
default:
return noGroup{decorator: decorator, w: writer, enableLineNumber: opts.OutputOption.EnableLineNumber}
}
}
type matchLine struct {
w io.Writer
decorator decorator
}
func (f matchLine) print(match match) {
for _, line := range match.lines {
column := ""
if line.matched && line.column > 0 {
column = f.decorator.columnNumber(line.column) + SeparatorColon
}
fmt.Fprintln(f.w,
column+
f.decorator.match(line.text, line.matched),
)
}
}
type fileWithMatch struct {
w io.Writer
decorator decorator
useNull bool
}
func (f fileWithMatch) print(match match) {
if f.useNull {
fmt.Fprint(f.w, f.decorator.path(match.path))
fmt.Fprint(f.w, "\x00")
} else {
fmt.Fprintln(f.w, f.decorator.path(match.path))
}
}
type count struct {
w io.Writer
decorator decorator
}
func (f count) print(match match) {
count := len(match.lines)
fmt.Fprintln(f.w,
f.decorator.path(match.path)+
SeparatorColon+
f.decorator.lineNumber(count),
)
}
type group struct {
w io.Writer
decorator decorator
useNull bool
enableLineNumber bool
}
func (f group) print(match match) {
if f.useNull {
fmt.Fprint(f.w, f.decorator.path(match.path))
fmt.Fprint(f.w, "\x00")
} else {
fmt.Fprintln(f.w, f.decorator.path(match.path))
}
for _, line := range match.lines {
sep := SeparatorColon
if !line.matched {
sep = SeparatorHyphen
}
column := ""
if line.matched && line.column > 0 {
column = f.decorator.columnNumber(line.column) + SeparatorColon
}
lineNum := ""
if f.enableLineNumber {
lineNum = f.decorator.lineNumber(line.num) + sep
}
fmt.Fprintln(f.w,
lineNum+
column+
f.decorator.match(line.text, line.matched),
)
}
fmt.Fprintln(f.w)
}
type noGroup struct {
w io.Writer
decorator decorator
enableLineNumber bool
}
func (f noGroup) print(match match) {
path := f.decorator.path(match.path) + SeparatorColon
for _, line := range match.lines {
sep := SeparatorColon
if !line.matched {
sep = SeparatorHyphen
}
column := ""
if line.matched && line.column > 0 {
column = f.decorator.columnNumber(line.column) + SeparatorColon
}
lineNum := ""
if f.enableLineNumber {
lineNum = f.decorator.lineNumber(line.num) + sep
}
fmt.Fprintln(f.w,
path+
lineNum+
column+
f.decorator.match(line.text, line.matched),
)
}
}
func newWriter(out io.Writer, opts Option) io.Writer {
encoder := func() io.Writer {
switch opts.OutputOption.OutputEncode {
case "sjis":
return transform.NewWriter(out, japanese.ShiftJIS.NewEncoder())
case "euc":
return transform.NewWriter(out, japanese.EUCJP.NewEncoder())
case "jis":
return transform.NewWriter(out, japanese.ISO2022JP.NewEncoder())
default:
return out
}
}()
if opts.OutputOption.EnableColor {
return ansicolor.NewAnsiColorWriter(encoder)
}
return encoder
}