Debian Codegraph @

internal/action/events.go

raw
1 package action
2
3 import (
4 "bytes"
5 "errors"
6 "fmt"
7 "strings"
8
9 "github.com/micro-editor/tcell/v2"
10 )
11
12 type Event interface {
13 Name() string
14 }
15
16 // RawEvent is simply an escape code
17 // We allow users to directly bind escape codes
18 // to get around some of a limitations of terminals
19 type RawEvent struct {
20 esc string
21 }
22
23 func (r RawEvent) Name() string {
24 return r.esc
25 }
26
27 // KeyEvent is a key event containing a key code,
28 // some possible modifiers (alt, ctrl, etc...) and
29 // a rune if it was simply a character press
30 // Note: to be compatible with tcell events,
31 // for ctrl keys r=code
32 type KeyEvent struct {
33 code tcell.Key
34 mod tcell.ModMask
35 r rune
36 any bool
37 }
38
39 func metaToAlt(mod tcell.ModMask) tcell.ModMask {
40 if mod&tcell.ModMeta != 0 {
41 mod &= ^tcell.ModMeta
42 mod |= tcell.ModAlt
43 }
44 return mod
45 }
46
47 func keyEvent(e *tcell.EventKey) KeyEvent {
48 ke := KeyEvent{
49 code: e.Key(),
50 mod: metaToAlt(e.Modifiers()),
51 }
52 if e.Key() == tcell.KeyRune {
53 ke.r = e.Rune()
54 }
55 return ke
56 }
57
58 func (k KeyEvent) Name() string {
59 if k.any {
60 return "<any>"
61 }
62 s := ""
63 m := []string{}
64 if k.mod&tcell.ModShift != 0 {
65 m = append(m, "Shift")
66 }
67 if k.mod&tcell.ModAlt != 0 {
68 m = append(m, "Alt")
69 }
70 if k.mod&tcell.ModMeta != 0 {
71 m = append(m, "Meta")
72 }
73 if k.mod&tcell.ModCtrl != 0 {
74 m = append(m, "Ctrl")
75 }
76
77 ok := false
78 if s, ok = tcell.KeyNames[k.code]; !ok {
79 if k.code == tcell.KeyRune {
80 s = string(k.r)
81 } else {
82 s = fmt.Sprintf("Key[%d]", k.code)
83 }
84 }
85 if len(m) != 0 {
86 if k.mod&tcell.ModCtrl != 0 && strings.HasPrefix(s, "Ctrl-") {
87 s = s[5:]
88 if len(s) == 1 {
89 s = strings.ToLower(s)
90 }
91 }
92 return fmt.Sprintf("%s-%s", strings.Join(m, "-"), s)
93 }
94 return s
95 }
96
97 // A KeySequence defines a list of consecutive
98 // events. All events in the sequence must be KeyEvents
99 // or MouseEvents.
100 type KeySequenceEvent struct {
101 keys []Event
102 }
103
104 func (k KeySequenceEvent) Name() string {
105 buf := bytes.Buffer{}
106 for _, e := range k.keys {
107 buf.WriteByte('<')
108 buf.WriteString(e.Name())
109 buf.WriteByte('>')
110 }
111 return buf.String()
112 }
113
114 type MouseState int
115
116 const (
117 MousePress = iota
118 MouseDrag
119 MouseRelease
120 )
121
122 // MouseEvent is a mouse event with a mouse button and
123 // any possible key modifiers
124 type MouseEvent struct {
125 btn tcell.ButtonMask
126 mod tcell.ModMask
127 state MouseState
128 }
129
130 func (m MouseEvent) Name() string {
131 mod := ""
132 if m.mod&tcell.ModShift != 0 {
133 mod = "Shift-"
134 }
135 if m.mod&tcell.ModAlt != 0 {
136 mod = "Alt-"
137 }
138 if m.mod&tcell.ModMeta != 0 {
139 mod = "Meta-"
140 }
141 if m.mod&tcell.ModCtrl != 0 {
142 mod = "Ctrl-"
143 }
144
145 state := ""
146 switch m.state {
147 case MouseDrag:
148 state = "Drag"
149 case MouseRelease:
150 state = "Release"
151 }
152
153 for k, v := range mouseEvents {
154 if v == m.btn {
155 return fmt.Sprintf("%s%s%s", mod, k, state)
156 }
157 }
158 return ""
159 }
160
161 // ConstructEvent takes a tcell event and returns a micro
162 // event. Note that tcell events can't express certain
163 // micro events such as key sequences. This function is
164 // mostly used for debugging/raw panes or constructing
165 // intermediate micro events while parsing a sequence.
166 func ConstructEvent(event tcell.Event) (Event, error) {
167 switch e := event.(type) {
168 case *tcell.EventKey:
169 return keyEvent(e), nil
170 case *tcell.EventRaw:
171 return RawEvent{
172 esc: e.EscSeq(),
173 }, nil
174 case *tcell.EventMouse:
175 return MouseEvent{
176 btn: e.Buttons(),
177 mod: metaToAlt(e.Modifiers()),
178 }, nil
179 }
180 return nil, errors.New("No micro event equivalent")
181 }
182
183 // A Handler will take a tcell event and execute it
184 // appropriately
185 type Handler interface {
186 HandleEvent(tcell.Event)
187 HandleCommand(string)
188 }