-
Notifications
You must be signed in to change notification settings - Fork 0
/
choose.go
63 lines (53 loc) · 1.19 KB
/
choose.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
package main
import (
"github.com/nsf/termbox-go"
)
func DisplayAliasesMenu(options []Command, selected int) {
termbox.Clear(termbox.ColorDefault, termbox.ColorDefault)
for i, option := range options {
offsetX := 2
offsetY := i + 1
if i == selected {
termbox.SetCell(offsetX, offsetY, '>', termbox.ColorLightCyan, termbox.ColorDefault)
}
toWrite := option.Name + ": " + option.Command
for _, ch := range toWrite {
termbox.SetCell(offsetX+2, offsetY, ch, termbox.ColorDefault, termbox.ColorDefault)
offsetX++
}
}
termbox.Flush()
}
func ChooseAlias(options []Command) int {
termbox.Init()
defer termbox.Close()
selected := 0
if len(options) == 0 {
return -2
}
DisplayAliasesMenu(options, selected)
for {
ev := termbox.PollEvent()
switch ev.Key {
case termbox.KeyArrowUp:
if selected > 0 {
selected--
DisplayAliasesMenu(options, selected)
}
case termbox.KeyArrowDown:
if selected < (len(options) - 1) {
selected++
DisplayAliasesMenu(options, selected)
}
case termbox.KeyEnter:
termbox.Close()
return selected
case termbox.KeyEsc:
termbox.Close()
return -1
case termbox.KeyCtrlC:
termbox.Close()
return -1
}
}
}