-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathtimer.go
188 lines (150 loc) · 3.94 KB
/
timer.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
package readline
import (
"context"
"sync/atomic"
"github.com/lmorg/murex/utils/lists"
)
func delayedSyntaxTimer(rl *Instance, i int32) {
if rl.PasswordMask != 0 || rl.DelayedSyntaxWorker == nil {
return
}
if rl.cacheSyntax.Get(rl.line.Runes()) != "" {
return
}
if rl.line.CellLen()+rl.promptLen > rl.termWidth {
// line wraps, which is hard to do with random ANSI escape sequences
// so better we don't bother trying.
return
}
newLine := rl.DelayedSyntaxWorker(rl.line.Runes())
var sLine string
if rl.SyntaxHighlighter != nil {
sLine = rl.SyntaxHighlighter(newLine)
} else {
sLine = string(newLine)
}
rl.cacheSyntax.Append(rl.line.Runes(), sLine)
if atomic.LoadInt32(&rl.delayedSyntaxCount) != i {
return
}
output := rl.moveCursorToStartStr()
output += sLine
output += rl.moveCursorFromEndToLinePosStr()
print(output)
}
// DelayedTabContext is a custom context interface for async updates to the tab completions
type DelayedTabContext struct {
rl *Instance
Context context.Context
cancel context.CancelFunc
}
// AppendSuggestions updates the tab completions with additional suggestions asynchronously
func (dtc *DelayedTabContext) AppendSuggestions(suggestions []string) {
if dtc == nil || dtc.rl == nil {
return
}
if !dtc.rl.modeTabCompletion {
return
}
max := dtc.rl.MaxTabCompleterRows * 20
if len(dtc.rl.tcSuggestions) == 0 {
dtc.rl.ForceHintTextUpdate(" ")
}
dtc.rl.tabMutex.Lock()
if dtc.rl.tcDescriptions == nil {
dtc.rl.tcDescriptions = make(map[string]string)
}
for i := range suggestions {
select {
case <-dtc.Context.Done():
dtc.rl.tabMutex.Unlock()
return
default:
if dtc.rl.tcDescriptions[suggestions[i]] != "" ||
(len(dtc.rl.tcSuggestions) < max && lists.Match(dtc.rl.tcSuggestions, suggestions[i])) {
// dedup
continue
}
dtc.rl.tcDescriptions[suggestions[i]] = dtc.rl.tcPrefix + suggestions[i]
dtc.rl.tcSuggestions = append(dtc.rl.tcSuggestions, suggestions[i])
}
}
dtc.rl.tabMutex.Unlock()
output := dtc.rl.clearHelpersStr()
//dtc.rl.ForceHintTextUpdate(" ")
output += dtc.rl.renderHelpersStr()
print(output)
}
// AppendDescriptions updates the tab completions with additional suggestions + descriptions asynchronously
func (dtc *DelayedTabContext) AppendDescriptions(suggestions map[string]string) {
if dtc.rl == nil {
// This might legitimately happen with some tests
return
}
if !dtc.rl.modeTabCompletion {
return
}
max := dtc.rl.MaxTabCompleterRows * 20
if len(dtc.rl.tcSuggestions) == 0 {
dtc.rl.ForceHintTextUpdate(" ")
}
dtc.rl.tabMutex.Lock()
for k := range suggestions {
select {
case <-dtc.Context.Done():
dtc.rl.tabMutex.Unlock()
return
default:
if dtc.rl.tcDescriptions[k] != "" ||
(len(dtc.rl.tcSuggestions) < max && lists.Match(dtc.rl.tcSuggestions, k)) {
// dedup
continue
}
dtc.rl.tcDescriptions[k] = suggestions[k]
dtc.rl.tcSuggestions = append(dtc.rl.tcSuggestions, k)
}
}
dtc.rl.tabMutex.Unlock()
output := dtc.rl.clearHelpersStr()
//dtc.rl.ForceHintTextUpdate(" ")
output += dtc.rl.renderHelpersStr()
print(output)
}
func delayedPreviewTimer(rl *Instance, fn PreviewFuncT, size *PreviewSizeT, item string) {
var ctx context.Context
callback := func(lines []string, pos int, err error) {
if pos == -1 {
if rl.previewCache != nil && rl.previewCache.pos < len(lines) {
pos = rl.previewCache.pos
} else {
pos = 0
}
}
select {
case <-ctx.Done():
return
default:
// continue
}
if err != nil {
rl.ForceHintTextUpdate(err.Error())
return
}
rl.previewCache = &previewCacheT{
item: item,
pos: pos,
len: size.Height,
lines: lines,
size: size,
}
output, err := rl.previewDrawStr(lines[pos:], size)
if err != nil {
rl.previewCache = nil
print(output)
return
}
print(output)
}
ctx, rl.previewCancel = context.WithCancel(context.Background())
fn(ctx, rl.line.Runes(), item, rl.PreviewImages, size, callback)
}