forked from dhamidi/leader
-
Notifications
You must be signed in to change notification settings - Fork 0
/
context.go
48 lines (42 loc) · 1.45 KB
/
context.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
package main
// Context provides dependencies for running UI commands
type Context struct {
Files FileSystem
Shell Shell
Terminal Terminal
CurrentKeyMap *KeyMap
History []*KeyMap
KeyPath []rune
Executor Executor
ErrorLogger *ErrorLogger
}
// Navigate changes the current key map and adds the previous value to this context's history,
func (ctx *Context) Navigate(nextKeyMap *KeyMap) {
ctx.PushHistory(ctx.CurrentKeyMap)
ctx.CurrentKeyMap = nextKeyMap
}
// PushHistory adds a history entry to this context.
func (ctx *Context) PushHistory(keyMap *KeyMap) {
ctx.History = append(ctx.History, keyMap)
}
// PopHistory removes the most recent history entry from this context.
func (ctx *Context) PopHistory() *KeyMap {
if len(ctx.History) == 0 {
return ctx.CurrentKeyMap
}
lastItem := ctx.History[len(ctx.History)-1]
ctx.History = ctx.History[:len(ctx.History)-1]
return lastItem
}
// CurrentBindingIsLooping returns true if the currently selected key binding is a looping key binding.
func (ctx *Context) CurrentBindingIsLooping() bool {
binding := ctx.CurrentKeyMap.LookupKey(ctx.KeyPath[len(ctx.KeyPath)-1])
return binding.IsLooping()
}
// PushKey records the given key in the key path. A key is never recorded twice in a row.
func (ctx *Context) PushKey(key rune) {
if len(ctx.KeyPath) > 0 && ctx.KeyPath[len(ctx.KeyPath)-1] == key && !ctx.CurrentBindingIsLooping() {
return
}
ctx.KeyPath = append(ctx.KeyPath, key)
}