-
Notifications
You must be signed in to change notification settings - Fork 17
/
pagermode-search.go
122 lines (99 loc) · 2.87 KB
/
pagermode-search.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
package m
import (
"regexp"
"unicode"
"unicode/utf8"
log "github.com/sirupsen/logrus"
"github.com/walles/moar/twin"
)
type PagerModeSearch struct {
pager *Pager
}
func (m PagerModeSearch) drawFooter(_ string, _ string) {
width, height := m.pager.screen.Size()
pos := 0
for _, token := range "Search: " + m.pager.searchString {
m.pager.screen.SetCell(pos, height-1, twin.NewCell(token, twin.StyleDefault))
pos++
}
// Add a cursor
m.pager.screen.SetCell(pos, height-1, twin.NewCell(' ', twin.StyleDefault.WithAttr(twin.AttrReverse)))
pos++
// Clear the rest of the line
for pos < width {
m.pager.screen.SetCell(pos, height-1, twin.NewCell(' ', twin.StyleDefault))
pos++
}
}
func (m *PagerModeSearch) updateSearchPattern() {
m.pager.searchPattern = toPattern(m.pager.searchString)
m.pager.scrollToSearchHits()
// FIXME: If the user is typing, indicate to user if we didn't find anything
}
// toPattern compiles a search string into a pattern.
//
// If the string contains only lower-case letter the pattern will be case insensitive.
//
// If the string is empty the pattern will be nil.
//
// If the string does not compile into a regexp the pattern will match the string verbatim
func toPattern(compileMe string) *regexp.Regexp {
if len(compileMe) == 0 {
return nil
}
hasUppercase := false
for _, char := range compileMe {
if unicode.IsUpper(char) {
hasUppercase = true
}
}
// Smart case; be case insensitive unless there are upper case chars
// in the search string
prefix := "(?i)"
if hasUppercase {
prefix = ""
}
pattern, err := regexp.Compile(prefix + compileMe)
if err == nil {
// Search string is a regexp
return pattern
}
pattern, err = regexp.Compile(prefix + regexp.QuoteMeta(compileMe))
if err == nil {
// Pattern matching the string exactly
return pattern
}
// Unable to create a match-string-verbatim pattern
panic(err)
}
// From: https://stackoverflow.com/a/57005674/473672
func removeLastChar(s string) string {
r, size := utf8.DecodeLastRuneInString(s)
if r == utf8.RuneError && (size == 0 || size == 1) {
size = 0
}
return s[:len(s)-size]
}
func (m PagerModeSearch) onKey(key twin.KeyCode) {
switch key {
case twin.KeyEscape, twin.KeyEnter:
//nolint:gosimple // The linter's advice is just wrong here
m.pager.mode = PagerModeViewing{pager: m.pager}
case twin.KeyBackspace, twin.KeyDelete:
if len(m.pager.searchString) == 0 {
return
}
m.pager.searchString = removeLastChar(m.pager.searchString)
m.updateSearchPattern()
case twin.KeyUp, twin.KeyDown, twin.KeyPgUp, twin.KeyPgDown:
//nolint:gosimple // The linter's advice is just wrong here
m.pager.mode = PagerModeViewing{pager: m.pager}
m.pager.mode.onKey(key)
default:
log.Debugf("Unhandled search key event %v", key)
}
}
func (m PagerModeSearch) onRune(char rune) {
m.pager.searchString = m.pager.searchString + string(char)
m.updateSearchPattern()
}