-
Notifications
You must be signed in to change notification settings - Fork 0
/
match.go
98 lines (81 loc) · 2.48 KB
/
match.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
package main
import (
"fmt"
"io/ioutil"
"log"
"regexp"
"strings"
termbox "github.com/nsf/termbox-go"
)
type Match struct {
path string // Filepath
lineNo int // Line number
line string // Line with maches
newline string // Line with replacements
repl string // Replacement string
linematches [][]int // Positions of matches
marked bool // Line should be replaced? (TODO: How to replace only a few in a line)
}
func (m Match) Replace(re *regexp.Regexp, repl string) {
input, err := ioutil.ReadFile(m.path)
if err != nil {
termbox.Close()
log.Fatalln(err)
}
lines := strings.Split(string(input), "\n")
// Replacement in the buffe
// TODO: Replaces all matches on the line, which is WRONG.
// This represents a single Match
lines[m.lineNo-1] = re.ReplaceAllString(lines[m.lineNo-1], repl)
output := strings.Join(lines, "\n") // TODO: detect other line endings
err = ioutil.WriteFile(m.path, []byte(output), 0644)
if err != nil {
termbox.Close()
log.Fatalln(err)
}
// if m.marked {
// }
}
func (m Match) Print(initialX, initialY int, isSelected bool) int {
x, y := initialX, initialY
lineColor := defaultLineColor
fgColor := defaultFgColor
bgColor := defaultBgColor
removedColor := defaultRemovedColor
addedColor := defaultAddedColor
if isSelected {
fgColor = fgColor | termbox.AttrReverse
bgColor = bgColor | termbox.AttrReverse
removedColor = removedColor | termbox.AttrReverse
addedColor = addedColor | termbox.AttrReverse
}
// First line
lineNumber := fmt.Sprintf("%4d\t", m.lineNo)
tbPrint(x, y, lineColor, termbox.ColorDefault, lineNumber)
for _, sm := range m.linematches {
beg := sm[0]
end := sm[1]
tbPrint(x+len(lineNumber), y, fgColor, bgColor, m.line[x:beg])
tbPrint(beg+len(lineNumber), y, removedColor, bgColor, m.line[beg:end])
x = end
}
tbPrint(x+len(lineNumber), y, fgColor, bgColor, m.line[x:])
// // Second line
// x = initialX
// y++
// origStringIdx := 0
// // w, _ := termbox.Size()
// xoff := 0
// // tbPrint(x, y, termbox.ColorGreen|termbox.AttrBold, bgColor, lineNumber)
// for _, sm := range m.linematches {
// beg := sm[0]
// end := sm[1]
// tbPrint(xoff+x+len(lineNumber), y, fgColor, bgColor, m.line[origStringIdx:beg])
// x += (beg - origStringIdx)
// tbPrint(xoff+x+len(lineNumber), y, addedColor, bgColor, m.repl)
// x += len(m.repl)
// origStringIdx = end
// }
// tbPrint(xoff+x+len(lineNumber), y, fgColor, bgColor, m.line[origStringIdx:])
return y + 1
}