forked from peco/peco
/
keyseq.go
189 lines (156 loc) · 3.26 KB
/
keyseq.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 keyseq
import (
"strings"
"sync"
"time"
"github.com/lalitadithya/termbox-go"
"github.com/pkg/errors"
)
var ErrInSequence = errors.New("expected a key sequence")
var ErrNoMatch = errors.New("could not match key to any action")
type ModifierKey int
const (
ModNone ModifierKey = iota
ModAlt
ModMax
)
// Key is data in one trie node in the KeySequence
type Key struct {
Modifier ModifierKey // Alt, etc
Key termbox.Key
Ch rune
}
func (kl KeyList) String() string {
list := make([]string, len(kl))
for i := 0; i < len(kl); i++ {
list[i] = kl[i].String()
}
return strings.Join(list, ",")
}
func (m ModifierKey) String() string {
switch m {
case ModAlt:
return "M"
default:
return ""
}
}
func (k Key) String() string {
var s string
if m := k.Modifier.String(); m != "" {
s += m + "-"
}
if k.Key == 0 {
s += string([]rune{k.Ch})
} else {
s += keyToString[k.Key]
}
return s
}
func NewKeyFromKey(k termbox.Key) Key {
return Key{
Modifier: 0,
Key: k,
Ch: rune(0),
}
}
// KeyList is just the list of keys
type KeyList []Key
func (k Key) Compare(x Key) int {
if k.Modifier < x.Modifier {
return -1
} else if k.Modifier > x.Modifier {
return 1
}
if k.Key < x.Key {
return -1
} else if k.Key > x.Key {
return 1
}
if k.Ch < x.Ch {
return -1
} else if k.Ch > x.Ch {
return 1
}
return 0
}
func (k KeyList) Equals(x KeyList) bool {
if len(k) != len(x) {
return false
}
for i := 0; i < len(k); i++ {
if k[i].Compare(x[i]) != 0 {
return false
}
}
return true
}
type keyseqMatcher interface {
Get(Key) Node
GetList(KeyList) Node
}
type Keyseq struct {
*Matcher
current keyseqMatcher
mutex sync.Mutex
prevInputTime time.Time
}
func New() *Keyseq {
return &Keyseq{
Matcher: NewMatcher(),
current: nil,
}
}
func (k *Keyseq) InMiddleOfChain() bool {
return k.current != nil && k.current != k.Matcher
}
func (k *Keyseq) CancelChain() {
k.mutex.Lock()
defer k.mutex.Unlock()
k.setCurrent(k.Matcher)
}
func (k *Keyseq) setCurrent(m keyseqMatcher) {
k.current = m
}
func (k *Keyseq) Current() keyseqMatcher {
if k.current == nil {
k.current = k.Matcher
}
return k.current
}
func (k *Keyseq) updateInputTime() {
k.prevInputTime = time.Now()
}
func (k *Keyseq) AcceptKey(key Key) (interface{}, error) {
// XXX should we return Action instead of interface{}?
k.mutex.Lock()
defer k.mutex.Unlock()
defer k.updateInputTime()
c := k.Current()
n := c.Get(key)
// nothing matched
if n == nil {
k.setCurrent(k.Matcher)
return nil, ErrNoMatch
}
// Matched node has children. It MAY BE a part of a key sequence,
// but the longest one ALWAYS wins. So for example, if you had
// "C-x,C-n" and "C-x" mapped to something, "C-x" alone will never
// fire any action
if n.HasChildren() {
// Set the current matcher to the matched node, so the next
// AcceptKey matches AFTER the current node
k.setCurrent(n)
return nil, ErrInSequence
}
// If it got here, we should just rest the matcher, and return
// whatever we matched
k.setCurrent(k.Matcher)
// This case should never be true, but we make sure to check
// for it in order to avoid the possibility of a crash
data := n.Value()
if data == nil {
return nil, ErrNoMatch
}
return data.(*nodeData).Value(), nil
}