summaryrefslogtreecommitdiffstats
path: root/cointop/keybindings.go
blob: 67def1ab0318ecca3a78c5d73b8f08ebb70f8cd6 (plain)
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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
package cointop

import (
	"strings"

	"github.com/miguelmota/gocui"
)

// ParseKeys returns string keyboard key as gocui key type
func (ct *Cointop) ParseKeys(s string) (interface{}, gocui.Modifier) {
	var key interface{}
	mod := gocui.ModNone
	split := strings.Split(s, "+")
	if len(split) > 1 {
		m := strings.ToLower(strings.TrimSpace(split[0]))
		k := strings.ToLower(strings.TrimSpace(split[1]))
		if m == "alt" {
			mod = gocui.ModAlt
			s = k
		} else if m == "ctrl" {
			switch k {
			case "0":
				key = '0'
			case "1":
				key = '1'
			case "2":
				key = gocui.KeyCtrl2
			case "3":
				key = gocui.KeyCtrl3
			case "4":
				key = gocui.KeyCtrl4
			case "5":
				key = gocui.KeyCtrl5
			case "6":
				key = gocui.KeyCtrl6
			case "7":
				key = gocui.KeyCtrl7
			case "8":
				key = gocui.KeyCtrl8
			case "9":
				key = '9'
			case "a":
				key = gocui.KeyCtrlA
			case "b":
				key = gocui.KeyCtrlB
			case "c":
				key = gocui.KeyCtrlC
			case "d":
				key = gocui.KeyCtrlD
			case "e":
				key = gocui.KeyCtrlE
			case "f":
				key = gocui.KeyCtrlF
			case "g":
				key = gocui.KeyCtrlG
			case "h":
				key = gocui.KeyCtrlH
			case "i":
				key = gocui.KeyCtrlI
			case "j":
				key = gocui.KeyCtrlJ
			case "k":
				key = gocui.KeyCtrlK
			case "l":
				key = gocui.KeyCtrlL
			case "m":
				key = gocui.KeyCtrlL
			case "n":
				key = gocui.KeyCtrlN
			case "o":
				key = gocui.KeyCtrlO
			case "p":
				key = gocui.KeyCtrlP
			case "q":
				key = gocui.KeyCtrlQ
			case "r":
				key = gocui.KeyCtrlR
			case "s":
				key = gocui.KeyCtrlS
			case "t":
				key = gocui.KeyCtrlT
			case "u":
				key = gocui.KeyCtrlU
			case "v":
				key = gocui.KeyCtrlV
			case "w":
				key = gocui.KeyCtrlW
			case "x":
				key = gocui.KeyCtrlX
			case "y":
				key = gocui.KeyCtrlY
			case "z":
				key = gocui.KeyCtrlZ
			case "~":
				key = gocui.KeyCtrlTilde
			case "[", "lsqrbracket", "leftsqrbracket", "leftsquarebracket":
				key = gocui.KeyCtrlLsqBracket
			case "]", "rsqrbracket", "rightsqrbracket", "rightsquarebracket":
				key = gocui.KeyCtrlRsqBracket
			case "space":
				key = gocui.KeyCtrlSpace
			case "backslash":
				key = gocui.KeyCtrlBackslash
			case "underscore":
				key = gocui.KeyCtrlUnderscore
			case "\\\\":
				key = '\\'
			}
			return key, mod
		}
	}

	if len(s) == 1 {
		r := []rune(s)
		key = r[0]
		return key, mod
	}

	s = strings.ToLower(s)
	switch s {
	case "arrowup", "uparrow", "up":
		key = gocui.KeyArrowUp
	case "arrowdown", "downarrow", "down":
		key = gocui.KeyArrowDown
	case "arrowleft", "leftarrow", "left":
		key = gocui.KeyArrowLeft
	case "arrowright", "rightarrow", "right":
		key = gocui.KeyArrowRight
	case "enter", "return":
		key = gocui.KeyEnter
	case "space", "spacebar":
		key = gocui.KeySpace
	case "esc", "escape":
		key = gocui.KeyEsc
	case "f1":
		key = gocui.KeyF1
	case "f2":
		key = gocui.KeyF2
	case "f3":
		key = gocui.KeyF3
	case "f4":
		key = gocui.KeyF4
	case "f5":
		key = gocui.KeyF5
	case "f6":
		key = gocui.KeyF6
	case "f7":
		key = gocui.KeyF7
	case "f8":
		key = gocui.KeyF8
	case "f9":
		key = gocui.KeyF9
	case "tab":
		key = gocui.KeyTab
	case "pageup", "pgup":
		key = gocui.KeyPgup
	case "pagedown", "pgdown", "pgdn":
		key = gocui.KeyPgdn
	case "home":
		key = gocui.KeyHome
	case "end":
		key = gocui.KeyEnd
	case "\\\\":
		key = '\\'
	}

	return key, mod
}

// Keybindings sets keyboard shortcut key bindings
func (ct *Cointop) Keybindings(g *gocui.Gui) error {
	for k, v := range ct.State.shortcutKeys {
		if k == "" {
			continue
		}
		v = strings.TrimSpace(strings.ToLower(v))
		var fn func(g *gocui.Gui, v *gocui.View) error
		key, mod := ct.ParseKeys(k)
		view := "table"
		switch v {
		case "move_up":
			fn = ct.Keyfn(ct.CursorUp)
		case "move_down":
			fn = ct.Keyfn(ct.CursorDown)
		case "previous_page":
			fn = ct.handleHkey(key)
		case "next_page":
			fn = ct.Keyfn(ct.NextPage)
		case "page_down":
			fn = ct.Keyfn(ct.PageDown)
		case "page_up":
			fn = ct.Keyfn(ct.PageUp)
		case "sort_column_symbol":
			fn = ct.Sortfn("symbol", false)
		case "move_to_page_first_row":
			fn = ct.Keyfn(ct.NavigateFirstLine)
		case "move_to_page_last_row":
			fn = ct.Keyfn(ct.NavigateLastLine)
		case "open_link":
			fn = ct.Keyfn(ct.OpenLink)
		case "refresh":
			fn = ct.Keyfn(ct.Refresh)
		case "sort_column_asc":
			fn = ct.Keyfn(ct.SortAsc)
		case "sort_column_desc":
			fn = ct.Keyfn(ct.SortDesc)
		case "sort_left_column":
			fn = ct.Keyfn(ct.SortPrevCol)
		case "sort_right_column":
			fn = ct.Keyfn(ct.SortNextCol)
		case "help", "toggle_show_help":
			fn = ct.Keyfn(ct.ToggleHelp)
			view = ""
		case "show_help":
			fn = ct.Keyfn(ct.ShowHelp)
			view = ""
		case "hide_help":
			fn = ct.Keyfn(ct.HideHelp)
			view = "help"
		case "first_page":
			fn