-
Notifications
You must be signed in to change notification settings - Fork 6
/
search.go
189 lines (166 loc) · 3.63 KB
/
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
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
package readline
import (
"bytes"
"container/list"
"fmt"
"sync"
)
type searchState uint
const (
searchStateFound searchState = iota
searchStateFailing
)
type searchDirection uint
const (
searchDirectionForward searchDirection = iota
searchDirectionBackward
)
type opSearch struct {
mutex sync.Mutex
inMode bool
state searchState
dir searchDirection
source *list.Element
w *terminal
buf *runeBuffer
data []rune
history *opHistory
markStart int
markEnd int
}
func newOpSearch(w *terminal, buf *runeBuffer, history *opHistory) *opSearch {
return &opSearch{
w: w,
buf: buf,
history: history,
}
}
func (o *opSearch) IsSearchMode() bool {
o.mutex.Lock()
defer o.mutex.Unlock()
return o.inMode
}
func (o *opSearch) SearchBackspace() {
o.mutex.Lock()
defer o.mutex.Unlock()
if len(o.data) > 0 {
o.data = o.data[:len(o.data)-1]
o.search(true)
}
}
func (o *opSearch) findHistoryBy(isNewSearch bool) (int, *list.Element) {
if o.dir == searchDirectionBackward {
return o.history.FindBck(isNewSearch, o.data, o.buf.idx)
}
return o.history.FindFwd(isNewSearch, o.data, o.buf.idx)
}
func (o *opSearch) search(isChange bool) bool {
if len(o.data) == 0 {
o.state = searchStateFound
o.searchRefresh(-1)
return true
}
idx, elem := o.findHistoryBy(isChange)
if elem == nil {
o.searchRefresh(-2)
return false
}
o.history.current = elem
item := o.history.showItem(o.history.current.Value)
start, end := 0, 0
if o.dir == searchDirectionBackward {
start, end = idx, idx+len(o.data)
} else {
start, end = idx, idx+len(o.data)
idx += len(o.data)
}
o.buf.SetWithIdx(idx, item)
o.markStart, o.markEnd = start, end
o.searchRefresh(idx)
return true
}
func (o *opSearch) SearchChar(r rune) {
o.mutex.Lock()
defer o.mutex.Unlock()
o.data = append(o.data, r)
o.search(true)
}
func (o *opSearch) SearchMode(dir searchDirection) bool {
o.mutex.Lock()
defer o.mutex.Unlock()
tWidth, _ := o.w.GetWidthHeight()
if tWidth == 0 {
return false
}
alreadyInMode := o.inMode
o.inMode = true
o.dir = dir
o.source = o.history.current
if alreadyInMode {
o.search(false)
} else {
o.searchRefresh(-1)
}
return true
}
func (o *opSearch) ExitSearchMode(revert bool) {
o.mutex.Lock()
defer o.mutex.Unlock()
if revert {
o.history.current = o.source
var redrawValue []rune
if o.history.current != nil {
redrawValue = o.history.showItem(o.history.current.Value)
}
o.buf.Set(redrawValue)
}
o.markStart, o.markEnd = 0, 0
o.state = searchStateFound
o.inMode = false
o.source = nil
o.data = nil
}
func (o *opSearch) searchRefresh(x int) {
tWidth, _ := o.w.GetWidthHeight()
if x == -2 {
o.state = searchStateFailing
} else if x >= 0 {
o.state = searchStateFound
}
if x < 0 {
x = o.buf.idx
}
x = o.buf.CurrentWidth(x)
x += o.buf.PromptLen()
x = x % tWidth
if o.markStart > 0 {
o.buf.SetStyle(o.markStart, o.markEnd, "4")
}
lineCnt := o.buf.CursorLineCount()
buf := bytes.NewBuffer(nil)
buf.Write(bytes.Repeat([]byte("\n"), lineCnt))
buf.WriteString("\033[J")
if o.state == searchStateFailing {
buf.WriteString("failing ")
}
if o.dir == searchDirectionBackward {
buf.WriteString("bck")
} else if o.dir == searchDirectionForward {
buf.WriteString("fwd")
}
buf.WriteString("-i-search: ")
buf.WriteString(string(o.data)) // keyword
buf.WriteString("\033[4m \033[0m") // _
fmt.Fprintf(buf, "\r\033[%dA", lineCnt) // move prev
if x > 0 {
fmt.Fprintf(buf, "\033[%dC", x) // move forward
}
o.w.Write(buf.Bytes())
}
func (o *opSearch) RefreshIfNeeded() {
o.mutex.Lock()
defer o.mutex.Unlock()
if o.inMode {
o.searchRefresh(-1)
}
}