-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscreen.go
343 lines (300 loc) · 8.9 KB
/
screen.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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
package main
/*
screen and keyboard handling
using termbox, which is redrawing all screen but simple,
ncurses would probably need less display bandwidth
cpu usage minimalized, only redrawing after events
(c) Holger Berger 2016, under GPL
*/
import (
"fmt"
"regexp"
"sync"
termbox "github.com/nsf/termbox-go"
)
// ScreenT class to keep state of termbox
type ScreenT struct {
w, h int // screensize
files []*FlexFileT // list of files
buffer *BufferT // buffer to be shown
offsety int // offset of forst line into buffer = 0 top of file 1 = second line on top of screen
offsetx int // offset of first character in line to be shown
searchInput bool // flag if search input is ongoing
searchForward bool // search direction, false = backward
searchString string // string to be searched
regex *regexp.Regexp // regex created from searcgString
lock sync.Mutex
}
// MatchT describes a match within a line, position + display color
type MatchT struct {
start, end int
color termbox.Attribute
}
// NewScreen inits termbox
func NewScreen(files []*FlexFileT, buffer *BufferT) *ScreenT {
newscreen := new(ScreenT)
newscreen.files = files
newscreen.buffer = buffer
newscreen.offsety = 0
newscreen.offsetx = 0
newscreen.searchInput = false
// init termbox
err := termbox.Init()
if err != nil {
panic(err)
}
// defer termbox.Close()
newscreen.w, newscreen.h = termbox.Size()
termbox.SetOutputMode(termbox.Output256)
termbox.Clear(termbox.ColorDefault, termbox.ColorDefault)
return newscreen
}
// eventHandler catches events and changes state
func (s *ScreenT) eventHandler(eventQueue chan termbox.Event, exitQueue chan bool) {
for {
select {
case ev := <-eventQueue:
// handling of search
if ev.Type == termbox.EventKey && ev.Ch == '/' {
termbox.SetCursor(1, s.h-1)
s.searchInput = true
s.searchForward = true
s.draw()
break
}
if ev.Type == termbox.EventKey && ev.Ch == '?' {
termbox.SetCursor(1, s.h-1)
s.searchInput = true
s.searchForward = false
s.draw()
break
}
if s.searchInput && ev.Type == termbox.EventKey && ev.Key == termbox.KeyEnter {
s.searchInput = false
termbox.HideCursor()
// perform search
if s.searchForward {
for lnr, currentline := range s.buffer.lines[s.offsety+1:] {
// FIXME case insensitive search?!?!
if s.regex.FindAll(currentline.line, -1) != nil {
s.offsety += lnr + 1
break
}
}
} else {
// backward
for lnr := s.offsety - 1; lnr >= 0; lnr-- {
// FIXME case insensitive search?!?!
currentline := s.buffer.lines[lnr]
if s.regex.FindAll(currentline.line, -1) != nil {
s.offsety = lnr
break
}
}
}
s.draw()
break
}
// delete last character from searchstring
if s.searchInput && ev.Type == termbox.EventKey && (ev.Key == termbox.KeyBackspace || ev.Key == termbox.KeyBackspace2 || ev.Key == termbox.KeyDelete) {
if len(s.searchString) > 0 {
s.searchString = s.searchString[:len(s.searchString)-1]
// update regex for search
if s.searchString != "" {
s.regex, _ = regexp.Compile(s.searchString)
}
}
s.draw()
break
}
// append character to searcgstring
if s.searchInput && ev.Type == termbox.EventKey {
if ev.Key != termbox.KeyArrowDown &&
ev.Key != termbox.KeyArrowUp &&
ev.Key != termbox.KeyArrowRight &&
ev.Key != termbox.KeyArrowLeft {
s.searchString = s.searchString + string(ev.Ch)
// update regex for search
if s.searchString != "" {
s.regex, _ = regexp.Compile(s.searchString)
}
}
if ev.Key == termbox.KeyEsc {
s.searchString = ""
s.searchInput = false
termbox.HideCursor()
}
s.draw()
break
}
// all other keys
if ev.Type == termbox.EventKey && (ev.Key == termbox.KeyEsc || ev.Ch == 'q') {
exitQueue <- true
}
if ev.Type == termbox.EventKey && (ev.Key == termbox.KeyHome || ev.Ch == '0') {
s.offsety = 0
s.draw()
break
}
if ev.Type == termbox.EventKey && (ev.Key == termbox.KeyEnd || ev.Ch == 'G') {
s.offsety = s.buffer.linecount - s.h
s.draw()
break
}
if ev.Type == termbox.EventKey && ev.Key == termbox.KeyArrowDown {
if s.offsety < s.buffer.linecount-s.h {
s.offsety++
}
s.draw()
break
}
if ev.Type == termbox.EventKey && ev.Key == termbox.KeyArrowUp {
if s.offsety > 0 {
s.offsety--
}
s.draw()
break
}
if ev.Type == termbox.EventKey && (ev.Key == termbox.KeyPgdn || ev.Key == termbox.KeySpace) {
if s.offsety < s.buffer.linecount-s.h {
s.offsety += s.h
} else {
s.offsety = s.buffer.linecount - s.h
}
s.draw()
break
}
if ev.Type == termbox.EventKey && ev.Key == termbox.KeyPgup {
if s.offsety-s.h > 0 {
s.offsety -= s.h
} else {
s.offsety = 0
}
s.draw()
break
}
if ev.Type == termbox.EventKey && ev.Key == termbox.KeyArrowRight {
s.offsetx++
s.draw()
break
}
if ev.Type == termbox.EventKey && ev.Key == termbox.KeyArrowLeft {
if s.offsetx > 0 {
s.offsetx--
}
s.draw()
break
}
if ev.Type == termbox.EventResize {
s.w, s.h = termbox.Size()
s.draw()
}
}
}
}
// eventLoop will not return unless program is ended
func (s *ScreenT) eventLoop() {
eventQueue := make(chan termbox.Event, 1)
exitQueue := make(chan bool, 1)
// handle events like keypress and resize
go s.eventHandler(eventQueue, exitQueue)
// catch events and send to event handler
go func() {
for {
eventQueue <- termbox.PollEvent()
}
}()
// inital draw
s.draw()
// endless loop, waiting for end event
loop:
for {
select {
case <-exitQueue:
break loop
}
}
}
// draw paints whatever is needed
func (s *ScreenT) draw() {
// as we use go routines, we need a lock here,
// to avoid a redraw triggered by goroutine to interfere
// with another, and we protect termbox.Flush at the same time
s.lock.Lock()
termbox.Clear(termbox.ColorDefault, termbox.ColorDefault)
// loop over lines of display
for y := 0; y < s.h-1; y++ {
if y+s.offsety >= s.buffer.linecount {
break
}
matches := make([]MatchT, 0, 0)
linep := s.buffer.lines[y+s.offsety].line
var color termbox.Attribute
// if line is not empty, match the line
if len(s.buffer.lines[y+s.offsety].line) > 0 {
color = s.buffer.rules.Match(linep)
} else {
color = termbox.ColorDefault
}
// search highlight handling
if s.searchString != "" {
// new code for regexp handling and multiple matches per line
index := s.regex.FindAllIndex(linep, -1)
for _, m := range index {
matches = append(matches, MatchT{m[0], m[1], termbox.ColorCyan + termbox.AttrReverse})
}
}
// render the line, loop over columns
for x := 0; x < s.w; x++ {
if x+s.offsetx >= len(linep) || linep[x+s.offsetx] == '\n' {
break
}
rune := rune(linep[x+s.offsetx])
if x+s.offsetx >= s.buffer.lines[y+s.offsety].hoststart &&
x+s.offsetx < s.buffer.lines[y+s.offsety].hostend {
hostname := string(linep[s.buffer.lines[y+s.offsety].hoststart:s.buffer.lines[y+s.offsety].hostend])
termbox.SetCell(x, y, rune, s.buffer.hostcolors[hostname], termbox.ColorDefault)
} else {
// first draw normal, might be overruled
termbox.SetCell(x, y, rune, color, termbox.ColorDefault)
// highlight reverse + magenta the current searchstring
for _, m := range matches {
if x+s.offsetx >= m.start && x+s.offsetx < m.end {
termbox.SetCell(x, y, rune, m.color, termbox.ColorDefault)
}
}
}
}
}
// status line
for x := 0; x <= s.w; x++ {
termbox.SetCell(x, s.h-1, ' ', termbox.ColorBlack|termbox.AttrReverse, termbox.ColorDefault)
}
// input mode for search
if s.searchInput {
if s.searchForward {
termbox.SetCell(0, s.h-1, '/', termbox.ColorBlack|termbox.AttrReverse, termbox.ColorDefault)
} else {
termbox.SetCell(0, s.h-1, '?', termbox.ColorBlack|termbox.AttrReverse, termbox.ColorDefault)
}
for x := 0; x < len(s.searchString); x++ {
termbox.SetCell(1+x, s.h-1, rune(s.searchString[x]), termbox.ColorBlack|termbox.AttrReverse, termbox.ColorDefault)
}
termbox.SetCursor(len(s.searchString)+1, s.h-1)
} else {
// helpstring
helpstring := "matchlog - ESC,q: quit /,?: search Home,End,PgUp,PgDown,Up,Down,Left,Right: navigation"
for x := 0; x < len(helpstring); x++ {
termbox.SetCell(1+x, s.h-1, rune(helpstring[x]), termbox.ColorBlack|termbox.AttrReverse, termbox.ColorDefault)
}
}
// ruler
ruler := int(float32(s.offsety) / float32(s.buffer.linecount-s.h) * 100.0)
rulerstring := fmt.Sprintf("%7d/%7d %3d%% ", s.offsety, s.buffer.linecount, ruler)
for x := 0; x < len(rulerstring); x++ {
termbox.SetCell(s.w-22+x, s.h-1, rune(rulerstring[x]), termbox.ColorBlack|termbox.AttrReverse, termbox.ColorDefault)
}
// full redraw
termbox.Flush()
s.lock.Unlock()
}