-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprinter.go
112 lines (97 loc) · 2.34 KB
/
printer.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
package main
import (
"fmt"
"github.com/fatih/color"
"strconv"
"strings"
)
var bgYellow = color.New(color.BgYellow).SprintFunc()
var fgYellow = color.New(color.FgYellow).SprintFunc()
var cyan = color.New(color.FgCyan, color.Bold).SprintFunc()
func colorize(str string, bounds []int) string {
fst := str[0:bounds[0]]
match := str[bounds[0]:bounds[1]]
rest := str[bounds[1]:]
return fmt.Sprintf("%s%s%s", fst, bgYellow(match), rest)
}
func makePrefix(config Config, result QueryResult, lineNoAdjust int) string {
lineNoStr := strconv.Itoa(result.Lno + lineNoAdjust)
var prefix string
var filePrefix string
var treePrefix string
if config.printTree {
treePrefix = cyan(result.Tree) + " "
} else {
filePrefix = ""
}
if !config.noPrintHeaders {
filePrefix = fmt.Sprintf(
"%s%s:",
treePrefix,
cyan(result.Path),
)
} else {
filePrefix = ""
}
if !config.noPrintLineNumber {
prefix = fmt.Sprintf("%s%s:",
filePrefix,
fgYellow(lineNoStr),
)
} else {
prefix = filePrefix
}
return prefix
}
func buildLine(config Config, result QueryResult) string {
return fmt.Sprintf("%s%s\n",
makePrefix(config, result, 0),
colorize(result.Line, result.Bounds),
)
}
// Print prints baesed on a CLI config
func Print(config Config, query Query, response QueryResponse) {
color.NoColor = !config.colorize
lineCount := 0
if !config.findInBody || config.findInFilename {
for _, result := range response.FileResults {
if config.numLines >= 0 && lineCount >= config.numLines {
return
}
fmt.Printf("%s\n",
strings.TrimSpace(colorize(result.Path, result.Bounds)),
)
lineCount++
}
}
if !config.findInFilename || config.findInBody {
for _, result := range response.Results {
if config.numLines >= 0 && lineCount >= config.numLines {
return
}
start := len(result.ContextBefore) - config.contextLinesBefore
if start < 0 {
start = 0
}
for i := start; i < len(result.ContextBefore); i++ {
fmt.Printf(
"%s%s\n",
makePrefix(config, result, -(len(result.ContextBefore)-i)),
result.ContextBefore[i],
)
}
fmt.Print(buildLine(config, result))
lineCount++
for i := 0; i < config.contextLinesAfter; i++ {
if i >= len(result.ContextAfter) {
break
}
fmt.Printf(
"%s%s\n",
makePrefix(config, result, i+1),
result.ContextAfter[i],
)
}
}
}
}