forked from u-root/u-root
/
less.go
535 lines (446 loc) · 10.8 KB
/
less.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
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
// Copyright 2018 the u-root Authors. All rights reserved
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package less
import (
"fmt"
"io"
"log"
"os"
"regexp"
"sync"
"github.com/nsf/termbox-go"
"github.com/u-root/u-root/pkg/lineio"
"github.com/u-root/u-root/pkg/sortedmap"
)
type size struct {
x int
y int
}
type Event int
const (
// EventQuit requests an application exit.
EventQuit Event = iota
// EventRefresh requests a display refresh.
EventRefresh
)
type Mode int
const (
// ModeNormal is the standard mode, allowing file navigation.
ModeNormal Mode = iota
// ModeSearchEntry is search entry mode. Key presses are added
// to the search string.
ModeSearchEntry
)
type Less struct {
// src is the source file being displayed.
src *lineio.LineReader
// tabStop is the number of spaces per tab.
tabStop int
// events is used to notify the main goroutine of events.
events chan Event
// mu locks the fields below.
mu sync.Mutex
// size is the size of the file display.
// There is a statusbar beneath the display.
size size
// line is the line number of the first line of the display.
line int64
// mode is the viewer mode.
mode Mode
// regexp is the search regexp specified by the user.
// Must only be modified by the event goroutine.
regexp string
// searchResults are the results for the current search.
// They should be highlighted.
searchResults *searchResults
}
// lastLine returns the last line on the display. It may be beyond the end
// of the file, if the file is short enough.
// mu must be held on call.
func (l *Less) lastLine() int64 {
return l.line + int64(l.size.y) - 1
}
// Scroll describes a scroll action.
type Scroll int
const (
// ScrollTop goes to the first line.
ScrollTop Scroll = iota
// ScrollBottom goes to the last line.
ScrollBottom
// ScrollUp goes up one line.
ScrollUp
// ScrollDown goes down one line.
ScrollDown
// ScrollUpPage goes up one page full.
ScrollUpPage
// ScrollDownPage goes down one page full.
ScrollDownPage
// ScrollUpHalfPage goes up one half page full.
ScrollUpHalfPage
// ScrollDownHalfPage goes down one half page full.
ScrollDownHalfPage
)
// scrollLine tries to scroll the display to the given line,
// but will not scroll beyond the first or last lines in the file.
// l.mu must be held when calling scrollLine.
func (l *Less) scrollLine(dest int64) {
var delta int64
if dest > l.line {
delta = 1
} else {
delta = -1
}
for l.line != dest && l.line+delta > 0 && l.src.LineExists(l.lastLine()+delta) {
l.line += delta
}
}
// scroll moves the display based on the passed scroll action, without
// going past the beginning or end of the file.
func (l *Less) scroll(s Scroll) {
l.mu.Lock()
defer l.mu.Unlock()
var dest int64
switch s {
case ScrollTop:
dest = 1
case ScrollBottom:
// Just try to go to int64 max.
dest = 0x7fffffffffffffff
case ScrollUp:
dest = l.line - 1
case ScrollDown:
dest = l.line + 1
case ScrollUpPage:
dest = l.line - int64(l.size.y)
case ScrollDownPage:
dest = l.line + int64(l.size.y)
case ScrollUpHalfPage:
dest = l.line - int64(l.size.y)/2
case ScrollDownHalfPage:
dest = l.line + int64(l.size.y)/2
}
l.scrollLine(dest)
}
func (l *Less) handleEvent(e termbox.Event) {
l.mu.Lock()
mode := l.mode
l.mu.Unlock()
if e.Type != termbox.EventKey {
return
}
c := e.Ch
k := e.Key
// Key is only valid is Ch is 0
if c != 0 {
k = 0
}
switch mode {
case ModeNormal:
switch {
case c == 'q':
l.events <- EventQuit
case c == 'j':
l.scroll(ScrollDown)
l.events <- EventRefresh
case c == 'k':
l.scroll(ScrollUp)
l.events <- EventRefresh
case c == 'g':
l.scroll(ScrollTop)
l.events <- EventRefresh
case c == 'G':
l.scroll(ScrollBottom)
l.events <- EventRefresh
case k == termbox.KeyPgup:
l.scroll(ScrollUpPage)
l.events <- EventRefresh
case k == termbox.KeyPgdn:
l.scroll(ScrollDownPage)
l.events <- EventRefresh
case k == termbox.KeyCtrlU:
l.scroll(ScrollUpHalfPage)
l.events <- EventRefresh
case k == termbox.KeyCtrlD:
l.scroll(ScrollDownHalfPage)
l.events <- EventRefresh
case c == '/':
l.mu.Lock()
l.mode = ModeSearchEntry
l.mu.Unlock()
l.events <- EventRefresh
case c == 'n':
l.mu.Lock()
if r, ok := l.searchResults.Next(l.line); ok {
l.scrollLine(r.line)
l.events <- EventRefresh
}
l.mu.Unlock()
case c == 'N':
l.mu.Lock()
if r, ok := l.searchResults.Prev(l.line); ok {
l.scrollLine(r.line)
l.events <- EventRefresh
}
l.mu.Unlock()
}
case ModeSearchEntry:
switch {
case k == termbox.KeyEnter:
r := l.search(l.regexp)
l.mu.Lock()
l.mode = ModeNormal
l.regexp = ""
l.searchResults = r
// Jump to nearest result
if r, ok := l.searchResults.Next(l.line); ok {
l.scrollLine(r.line)
}
l.mu.Unlock()
l.events <- EventRefresh
default:
l.mu.Lock()
l.regexp += string(c)
l.mu.Unlock()
l.events <- EventRefresh
}
}
}
func (l *Less) listenEvents() {
for {
e := termbox.PollEvent()
l.handleEvent(e)
}
}
// searchResult describes search matches on a single line.
type searchResult struct {
line int64
matches [][]int
err error
}
// matchesChar returns true if the search result contains a match for
// character index c.
func (s searchResult) matchesChar(c int) bool {
for _, match := range s.matches {
if len(match) < 2 {
continue
}
if c >= match[0] && c < match[1] {
return true
}
}
return false
}
type searchResults struct {
// mu locks the fields below.
mu sync.Mutex
// lines maps search results for a specific line to an index in results.
lines sortedmap.Map
// results contains the actual search results, in no particular order.
results []searchResult
}
func NewSearchResults() *searchResults {
return &searchResults{
lines: sortedmap.NewMap(),
}
}
// Add adds a result.
func (s *searchResults) Add(r searchResult) {
s.mu.Lock()
defer s.mu.Unlock()
i := int64(len(s.results))
s.results = append(s.results, r)
s.lines.Insert(r.line, i)
}
// Get finds the result for a specific line, returning ok if found
func (s *searchResults) Get(line int64) (searchResult, bool) {
s.mu.Lock()
defer s.mu.Unlock()
if i, ok := s.lines.Get(line); ok {
return s.results[i], true
}
return searchResult{}, false
}
// Next returns the search result for the nearest line after line,
// noninclusive, if one exists.
func (s *searchResults) Next(line int64) (searchResult, bool) {
s.mu.Lock()
defer s.mu.Unlock()
_, i, err := s.lines.NearestGreater(line)
if err != nil {
// Probably ErrNoSuchKey, aka none found.
return searchResult{}, false
}
return s.results[i], true
}
// Prev returns the search result for the nearest line before line,
// noninclusive, if one exists.
func (s *searchResults) Prev(line int64) (searchResult, bool) {
s.mu.Lock()
defer s.mu.Unlock()
// Search for line - 1, since it may be equal.
_, i, err := s.lines.NearestLessEqual(line - 1)
if err != nil {
// Probably ErrNoSuchKey, aka none found.
return searchResult{}, false
}
return s.results[i], true
}
func (l *Less) search(s string) *searchResults {
reg, err := regexp.Compile(s)
if err != nil {
// TODO(prattmic): display a better error
log.Printf("regexp failed to compile: %v", err)
return NewSearchResults()
}
resultChan := make(chan searchResult, 100)
searchLine := func(line int64) {
r, err := l.src.SearchLine(reg, line)
if err != nil {
r = nil
}
resultChan <- searchResult{
line: line,
matches: r,
err: err,
}
}
nextLine := int64(1)
// Spawn initial search goroutines
for ; nextLine <= 5; nextLine++ {
go searchLine(nextLine)
}
results := NewSearchResults()
var count int64
waitResult := func() searchResult {
ret := <-resultChan
count++
// Only store results with matches.
if len(ret.matches) > 0 {
results.Add(ret)
}
return ret
}
// Collect results, start searching next lines until we start
// hitting EOF.
for {
r := waitResult()
// We started hitting errors on a previous line,
// there is no reason to search later lines.
if r.err != nil {
break
}
go searchLine(nextLine)
nextLine++
}
// Collect the remaining results.
for count < nextLine-1 {
waitResult()
}
return results
}
// statusBar renders the status bar.
// mu must be held on call.
func (l *Less) statusBar() {
// The statusbar is just below the display.
// Clear the statusbar
for i := 0; i < l.size.x; i++ {
termbox.SetCell(i, l.size.y, ' ', 0, 0)
}
switch l.mode {
case ModeNormal:
// Just a colon and a cursor
termbox.SetCell(0, l.size.y, ':', 0, 0)
termbox.SetCursor(1, l.size.y)
case ModeSearchEntry:
// / and search string
termbox.SetCell(0, l.size.y, '/', 0, 0)
for i, c := range l.regexp {
termbox.SetCell(1+i, l.size.y, c, 0, 0)
}
termbox.SetCursor(1+len(l.regexp), l.size.y)
}
}
// alignUp aligns n up to the next multiple of divisor.
func alignUp(n, divisor int) int {
return n + (divisor - (n % divisor))
}
func (l *Less) refreshScreen() error {
l.mu.Lock()
defer l.mu.Unlock()
for y := 0; y < l.size.y; y++ {
buf := make([]byte, l.size.x)
line := l.line + int64(y)
_, err := l.src.ReadLine(buf, line)
// EOF just means the line was shorter than the display.
if err != nil && err != io.EOF {
return err
}
highlight, ok := l.searchResults.Get(line)
var displayColumn int
for i, c := range buf {
if displayColumn >= l.size.x {
break
}
fg := termbox.ColorDefault
bg := termbox.ColorDefault
// Highlight matches
if ok && highlight.matchesChar(i) {
fg = termbox.ColorBlack
bg = termbox.ColorWhite
}
if c == '\t' {
// Tabs align the display up to the next
// multiple of tabstop.
next := alignUp(displayColumn, l.tabStop)
// Clear the tab spaces
for j := displayColumn; j < next; j++ {
termbox.SetCell(j, y, ' ', 0, 0)
}
displayColumn = next
} else {
termbox.SetCell(displayColumn, y, rune(c), fg, bg)
displayColumn++
}
}
}
l.statusBar()
termbox.Flush()
return nil
}
func (l *Less) Run() {
// Start populating the LineReader cache, to speed things up later.
go l.src.Populate()
go l.listenEvents()
err := l.refreshScreen()
if err != nil {
fmt.Fprintf(os.Stderr, "Failed to refresh screen: %v\n", err)
return
}
for {
e := <-l.events
switch e {
case EventQuit:
return
case EventRefresh:
err = l.refreshScreen()
if err != nil {
fmt.Fprintf(os.Stderr, "Failed to refresh screen: %v\n", err)
return
}
}
}
}
func NewLess(r io.ReaderAt, ts int) Less {
x, y := termbox.Size()
return Less{
src: lineio.NewLineReader(r),
tabStop: ts,
// Save one line for statusbar.
size: size{x: x, y: y - 1},
line: 1,
events: make(chan Event, 1),
mode: ModeNormal,
searchResults: NewSearchResults(),
}
}