-
Notifications
You must be signed in to change notification settings - Fork 787
/
events.go
211 lines (192 loc) · 4.71 KB
/
events.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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
// Copyright 2017 Zack Guo <[email protected]>. All rights reserved.
// Use of this source code is governed by a MIT license that can
// be found in the LICENSE file.
package termui
import (
"fmt"
tb "github.com/nsf/termbox-go"
)
/*
List of events:
mouse events:
<MouseLeft> <MouseRight> <MouseMiddle>
<MouseWheelUp> <MouseWheelDown>
keyboard events:
any uppercase or lowercase letter like j or J
<C-d> etc
<M-d> etc
<Up> <Down> <Left> <Right>
<Insert> <Delete> <Home> <End> <Previous> <Next>
<Backspace> <Tab> <Enter> <Escape> <Space>
<C-<Space>> etc
terminal events:
<Resize>
keyboard events that do not work:
<C-->
<C-2> <C-~>
<C-h>
<C-i>
<C-m>
<C-[> <C-3>
<C-\\>
<C-]>
<C-/> <C-_>
<C-8>
*/
type EventType uint
const (
KeyboardEvent EventType = iota
MouseEvent
ResizeEvent
)
type Event struct {
Type EventType
ID string
Payload interface{}
}
// Mouse payload.
type Mouse struct {
Drag bool
X int
Y int
}
// Resize payload.
type Resize struct {
Width int
Height int
}
// PollEvents gets events from termbox, converts them, then sends them to each of its channels.
func PollEvents() <-chan Event {
ch := make(chan Event)
go func() {
for {
ch <- convertTermboxEvent(tb.PollEvent())
}
}()
return ch
}
var keyboardMap = map[tb.Key]string{
tb.KeyF1: "<F1>",
tb.KeyF2: "<F2>",
tb.KeyF3: "<F3>",
tb.KeyF4: "<F4>",
tb.KeyF5: "<F5>",
tb.KeyF6: "<F6>",
tb.KeyF7: "<F7>",
tb.KeyF8: "<F8>",
tb.KeyF9: "<F9>",
tb.KeyF10: "<F10>",
tb.KeyF11: "<F11>",
tb.KeyF12: "<F12>",
tb.KeyInsert: "<Insert>",
tb.KeyDelete: "<Delete>",
tb.KeyHome: "<Home>",
tb.KeyEnd: "<End>",
tb.KeyPgup: "<PageUp>",
tb.KeyPgdn: "<PageDown>",
tb.KeyArrowUp: "<Up>",
tb.KeyArrowDown: "<Down>",
tb.KeyArrowLeft: "<Left>",
tb.KeyArrowRight: "<Right>",
tb.KeyCtrlSpace: "<C-<Space>>", // tb.KeyCtrl2 tb.KeyCtrlTilde
tb.KeyCtrlA: "<C-a>",
tb.KeyCtrlB: "<C-b>",
tb.KeyCtrlC: "<C-c>",
tb.KeyCtrlD: "<C-d>",
tb.KeyCtrlE: "<C-e>",
tb.KeyCtrlF: "<C-f>",
tb.KeyCtrlG: "<C-g>",
tb.KeyBackspace: "<C-<Backspace>>", // tb.KeyCtrlH
tb.KeyTab: "<Tab>", // tb.KeyCtrlI
tb.KeyCtrlJ: "<C-j>",
tb.KeyCtrlK: "<C-k>",
tb.KeyCtrlL: "<C-l>",
tb.KeyEnter: "<Enter>", // tb.KeyCtrlM
tb.KeyCtrlN: "<C-n>",
tb.KeyCtrlO: "<C-o>",
tb.KeyCtrlP: "<C-p>",
tb.KeyCtrlQ: "<C-q>",
tb.KeyCtrlR: "<C-r>",
tb.KeyCtrlS: "<C-s>",
tb.KeyCtrlT: "<C-t>",
tb.KeyCtrlU: "<C-u>",
tb.KeyCtrlV: "<C-v>",
tb.KeyCtrlW: "<C-w>",
tb.KeyCtrlX: "<C-x>",
tb.KeyCtrlY: "<C-y>",
tb.KeyCtrlZ: "<C-z>",
tb.KeyEsc: "<Escape>", // tb.KeyCtrlLsqBracket tb.KeyCtrl3
tb.KeyCtrl4: "<C-4>", // tb.KeyCtrlBackslash
tb.KeyCtrl5: "<C-5>", // tb.KeyCtrlRsqBracket
tb.KeyCtrl6: "<C-6>",
tb.KeyCtrl7: "<C-7>", // tb.KeyCtrlSlash tb.KeyCtrlUnderscore
tb.KeySpace: "<Space>",
tb.KeyBackspace2: "<Backspace>", // tb.KeyCtrl8:
}
// convertTermboxKeyboardEvent converts a termbox keyboard event to a more friendly string format.
// Combines modifiers into the string instead of having them as additional fields in an event.
func convertTermboxKeyboardEvent(e tb.Event) Event {
ID := "%s"
if e.Mod == tb.ModAlt {
ID = "<M-%s>"
}
if e.Ch != 0 {
ID = fmt.Sprintf(ID, string(e.Ch))
} else {
converted, ok := keyboardMap[e.Key]
if !ok {
converted = ""
}
ID = fmt.Sprintf(ID, converted)
}
return Event{
Type: KeyboardEvent,
ID: ID,
}
}
var mouseButtonMap = map[tb.Key]string{
tb.MouseLeft: "<MouseLeft>",
tb.MouseMiddle: "<MouseMiddle>",
tb.MouseRight: "<MouseRight>",
tb.MouseRelease: "<MouseRelease>",
tb.MouseWheelUp: "<MouseWheelUp>",
tb.MouseWheelDown: "<MouseWheelDown>",
}
func convertTermboxMouseEvent(e tb.Event) Event {
converted, ok := mouseButtonMap[e.Key]
if !ok {
converted = "Unknown_Mouse_Button"
}
Drag := e.Mod == tb.ModMotion
return Event{
Type: MouseEvent,
ID: converted,
Payload: Mouse{
X: e.MouseX,
Y: e.MouseY,
Drag: Drag,
},
}
}
// convertTermboxEvent turns a termbox event into a termui event.
func convertTermboxEvent(e tb.Event) Event {
if e.Type == tb.EventError {
panic(e.Err)
}
switch e.Type {
case tb.EventKey:
return convertTermboxKeyboardEvent(e)
case tb.EventMouse:
return convertTermboxMouseEvent(e)
case tb.EventResize:
return Event{
Type: ResizeEvent,
ID: "<Resize>",
Payload: Resize{
Width: e.Width,
Height: e.Height,
},
}
}
return Event{}
}