-
Notifications
You must be signed in to change notification settings - Fork 139
/
print.go
153 lines (141 loc) · 3.8 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
package print
import (
"code.google.com/p/go.text/encoding/japanese"
"code.google.com/p/go.text/transform"
"fmt"
"github.com/monochromegane/the_platinum_searcher/search/match"
"github.com/monochromegane/the_platinum_searcher/search/option"
"github.com/monochromegane/the_platinum_searcher/search/pattern"
"github.com/shiena/ansicolor"
"io"
"os"
"strings"
)
const (
ColorReset = "\x1b[0m\x1b[K"
ColorLineNumber = "\x1b[1;33m" /* yellow with black background */
ColorPath = "\x1b[1;32m" /* bold green */
ColorMatch = "\x1b[30;43m" /* black with yellow background */
)
type Params struct {
Pattern *pattern.Pattern
Path string
Matches []*match.Match
}
type Printer struct {
In chan *Params
Done chan bool
Option *option.Option
writer io.Writer
}
func NewPrinter(in chan *Params, done chan bool, option *option.Option) *Printer {
return &Printer{in, done, option, createWriter(option)}
}
func (self *Printer) Print() {
var fileMatchCount, matchCount uint
fileMatchCount = 0
matchCount = 0
for arg := range self.In {
if self.Option.FilesWithRegexp != "" {
self.printPath(arg.Path)
fmt.Println()
fileMatchCount++
continue
}
if len(arg.Matches) == 0 {
continue
}
if self.Option.FilesWithMatches {
self.printPath(arg.Path)
fmt.Println()
fileMatchCount++
continue
}
if !self.Option.NoGroup {
self.printPath(arg.Path)
fmt.Println()
fileMatchCount++
}
lastLineNum := 0
enableContext := self.Option.Before > 0 || self.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 self.Option.NoGroup {
self.printPath(arg.Path)
}
self.printContext(v.Befores)
self.printMatch(arg.Pattern, v.Line)
matchCount++
fmt.Println()
self.printContext(v.Afters)
}
if !self.Option.NoGroup {
fmt.Println()
}
}
if self.Option.Stats {
fmt.Printf("%d Files Matched\n", fileMatchCount)
fmt.Printf("%d Total Text Matches\n", matchCount)
}
self.Done <- true
}
func (self *Printer) printPath(path string) {
if self.Option.EnableColor {
fmt.Fprintf(self.writer, "%s%s%s", ColorPath, path, ColorReset)
} else {
fmt.Fprintf(self.writer, "%s", path)
}
if !self.Option.FilesWithMatches && self.Option.FilesWithRegexp == "" {
fmt.Fprintf(self.writer, ":")
}
}
func (self *Printer) printLineNumber(lineNum int, sep string) {
if self.Option.EnableColor {
fmt.Fprintf(self.writer, "%s%d%s%s", ColorLineNumber, lineNum, ColorReset, sep)
} else {
fmt.Fprintf(self.writer, "%d%s", lineNum, sep)
}
}
func (self *Printer) printMatch(pattern *pattern.Pattern, line *match.Line) {
self.printLineNumber(line.Num, ":")
if !self.Option.EnableColor {
fmt.Fprintf(self.writer, "%s", line.Str)
} else if pattern.IgnoreCase {
fmt.Fprintf(self.writer, "%s", pattern.Regexp.ReplaceAllString(line.Str, ColorMatch+"${1}"+ColorReset))
} else {
fmt.Fprintf(self.writer, "%s", strings.Replace(line.Str, pattern.Pattern, ColorMatch+pattern.Pattern+ColorReset, -1))
}
}
func (self *Printer) printContext(lines []*match.Line) {
for _, line := range lines {
self.printLineNumber(line.Num, "-")
fmt.Fprintf(self.writer, "%s", line.Str)
fmt.Fprintln(self.writer)
}
}
func createWriter(option *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
}