forked from monochromegane/the_platinum_searcher
-
Notifications
You must be signed in to change notification settings - Fork 0
/
formatter.go
198 lines (180 loc) · 4.58 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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
package mack
import (
"fmt"
"io"
"log"
"github.com/antchfx/jsonquery"
"github.com/shiena/ansicolor"
"golang.org/x/net/html"
"golang.org/x/text/encoding/japanese"
"golang.org/x/text/transform"
)
type formatPrinter interface {
print(match match)
}
func newFormatPrinter(pattern regex_pattern, w io.Writer, opts Option) formatPrinter {
writer := newWriter(w, opts)
decorator := newDecorator(pattern, opts) // decorator is for rich terminal output only. the "printer" controls output format.
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.OutputJson:
return NewJsonPrinter(w, opts.OutputOption)
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.(string), 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(f.render_line(line.text), line.matched),
)
}
fmt.Fprintln(f.w)
}
func (f group) render_line(line any) string {
switch v := line.(type) {
case nil:
return ""
case string:
return v
case *jsonquery.Node:
str, err := RenderJson(v.Value(), "") // fixme: drop options into group struct
if err != nil {
log.Printf("error rendering %v: %s", v, err)
str = fmt.Sprintf("%v", v)
}
return str
case *html.Node:
return RenderHtml(v)
case map[string]interface{}:
str, err := RenderJson(v, " ")
if err != nil {
log.Printf("error rendering %v: %s", v, err)
str = fmt.Sprintf("%v", v)
}
return str
default:
fmt.Printf("type unknown %T, blindly string-ifying: %v", v, v)
return fmt.Sprintf("%v", v)
}
}
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.(string), 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
}