-
Notifications
You must be signed in to change notification settings - Fork 1
/
keyboard.go
180 lines (165 loc) · 3.06 KB
/
keyboard.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
package prompt
import (
editor "github.com/JosephNaberhaus/texteditor"
"github.com/eiannone/keyboard"
)
type Key interface {
IsText() bool
Rune() rune
}
type RuneKey rune
func (r RuneKey) IsText() bool {
return true
}
func (r RuneKey) Rune() rune {
return rune(r)
}
type ControlKey uint8
func (c ControlKey) IsText() bool {
if c == ControlSpace {
return true
}
return false
}
func (c ControlKey) Rune() rune {
if c == ControlSpace {
return ' '
}
return 0
}
const (
Noop ControlKey = iota
ControlLeft
ControlRight
ControlUp
ControlDown
ControlEnter
ControlBackspace
ControlSpace
ControlHome
ControlEnd
ControlCtrlA
ControlCtrlB
ControlCtrlC
ControlCtrlD
ControlCtrlE
ControlCtrlF
ControlCtrlG
ControlCtrlI
ControlCtrlJ
ControlCtrlK
ControlCtrlL
ControlCtrlN
ControlCtrlO
ControlCtrlP
ControlCtrlQ
ControlCtrlR
ControlCtrlS
ControlCtrlT
ControlCtrlU
ControlCtrlV
ControlCtrlW
ControlCtrlX
ControlCtrlY
ControlCtrlZ
)
func ToKey(rune rune, key keyboard.Key) Key {
if rune != 0 {
return RuneKey(rune)
}
switch key {
case keyboard.KeyArrowLeft:
return ControlLeft
case keyboard.KeyArrowRight:
return ControlRight
case keyboard.KeyArrowUp:
return ControlUp
case keyboard.KeyArrowDown:
return ControlDown
case keyboard.KeyEnter:
return ControlEnter
case keyboard.KeyBackspace:
fallthrough
case keyboard.KeyBackspace2:
return ControlBackspace
case keyboard.KeySpace:
return ControlSpace
case keyboard.KeyHome:
return ControlHome
case keyboard.KeyEnd:
return ControlEnd
case keyboard.KeyCtrlA:
return ControlCtrlA
case keyboard.KeyCtrlB:
return ControlCtrlB
case keyboard.KeyCtrlC:
return ControlCtrlC
case keyboard.KeyCtrlD:
return ControlCtrlD
case keyboard.KeyCtrlE:
return ControlCtrlE
case keyboard.KeyCtrlF:
return ControlCtrlF
case keyboard.KeyCtrlG:
return ControlCtrlG
case keyboard.KeyCtrlI:
return ControlCtrlI
case keyboard.KeyCtrlJ:
return ControlCtrlJ
case keyboard.KeyCtrlK:
return ControlCtrlK
case keyboard.KeyCtrlL:
return ControlCtrlL
case keyboard.KeyCtrlN:
return ControlCtrlN
case keyboard.KeyCtrlO:
return ControlCtrlO
case keyboard.KeyCtrlP:
return ControlCtrlP
case keyboard.KeyCtrlQ:
return ControlCtrlQ
case keyboard.KeyCtrlR:
return ControlCtrlR
case keyboard.KeyCtrlS:
return ControlCtrlS
case keyboard.KeyCtrlT:
return ControlCtrlT
case keyboard.KeyCtrlU:
return ControlCtrlU
case keyboard.KeyCtrlV:
return ControlCtrlV
case keyboard.KeyCtrlW:
return ControlCtrlW
case keyboard.KeyCtrlX:
return ControlCtrlX
case keyboard.KeyCtrlY:
return ControlCtrlY
case keyboard.KeyCtrlZ:
return ControlCtrlZ
default:
return Noop
}
}
func applyKeyToEditor(k Key, editor *editor.TextEditor) {
if k.IsText() {
editor.Write(string(k.Rune()))
}
switch k {
case ControlLeft:
editor.Left()
case ControlRight:
editor.Right()
case ControlUp:
editor.Up()
case ControlDown:
editor.Down()
case ControlEnter:
editor.Newline()
case ControlBackspace:
editor.Backspace()
case ControlHome:
editor.Home()
case ControlEnd:
editor.End()
}
}