-
Notifications
You must be signed in to change notification settings - Fork 0
/
select.go
168 lines (144 loc) · 3.98 KB
/
select.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
package gopackages
import (
"errors"
"os"
"syscall"
"time"
"unsafe"
"github.com/nsf/termbox-go"
)
const defaultRatio float64 = 7.0 / 3.0 // The terminal's default cursor width/height ratio
var (
width int
height int
xwritten []int
ywritten []int
// whratio float64
)
func tbprint(x, y int, fg, bg termbox.Attribute, msg string) {
y += 2
for _, c := range msg {
if intInSlice(x, xwritten) && intInSlice(y, ywritten) {
continue
}
termbox.SetCell(x, y, c, fg, bg)
x++
}
}
func tbprintcenter(y int, fg, bg termbox.Attribute, msg string) {
x := int((width / 2) - (len(msg) / 2))
for _, c := range msg {
if intInSlice(x, xwritten) && intInSlice(y, ywritten) {
continue
}
termbox.SetCell(x, y, c, fg, bg)
x++
}
}
func tbprintrev(y int, fg, bg termbox.Attribute, msg string) {
x := width - len(msg) - 1
for _, c := range msg {
if intInSlice(x, xwritten) && intInSlice(y, ywritten) {
continue
}
termbox.SetCell(x, y, c, fg, bg)
x++
}
}
type SelectOptionsStruct struct {
DefaultSelection int
}
func Select(options []string, selectOptions ...SelectOptionsStruct) (int, error) {
selected := -1
selectedDefault := false
// throw error if selectOptions is included more than once
if len(selectOptions) > 1 {
return 0, errors.New("only one select option parameter is valid")
}
if len(selectOptions) > 0 {
selected = selectOptions[0].DefaultSelection
selectedDefault = true
}
err := termbox.Init()
if err != nil {
panic(err)
}
defer termbox.Close()
termbox.SetOutputMode(termbox.Output256)
termbox.SetOutputMode(termbox.Output256)
draw(options, selected)
for {
switch ev := termbox.PollEvent(); ev.Type {
case termbox.EventKey:
if ev.Key == termbox.KeyEsc || ev.Ch == 'q' || ev.Key == termbox.KeyCtrlC {
if selected != -1 && selectedDefault {
return selected, nil
} else {
return -1, errors.New("selection terminated by user")
}
}
if ev.Key == termbox.KeyArrowRight && len(options)-1 > selected {
selected++
draw(options, selected)
}
if ev.Key == termbox.KeyArrowLeft && selected > 0 {
selected--
draw(options, selected)
}
if ev.Key == termbox.KeyEnter {
if selected == -1 {
return -1, errors.New("nothing has been selected")
}
return selected, nil
}
case termbox.EventResize:
draw(options, selected)
case termbox.EventMouse:
default:
time.Sleep(10 * time.Millisecond)
}
}
}
func draw(options []string, selected int) {
termbox.Clear(termbox.ColorDefault, termbox.ColorDefault)
defer termbox.Flush()
width, height, _ = canvasSize()
tbprintcenter(0, termbox.ColorDefault, termbox.ColorDefault, "Select a package")
for i, str := range options {
if i == selected {
tbprint(0, i, termbox.ColorWhite, termbox.ColorDefault, "> ")
tbprint(2, i, termbox.ColorBlue, termbox.ColorDefault, str)
continue
}
tbprint(2, i, termbox.ColorDefault, termbox.ColorDefault, str)
}
closeMsg := "To close, press the 'ESC' key"
helpMsg := "Use the left and right arrow keys to select an option"
tbprintrev(height-1, termbox.ColorWhite, termbox.ColorDefault, closeMsg)
if width-2 > len(closeMsg)+len(helpMsg) { // '2' is subtracted for adequate spacing between sentences
tbprint(0, height-1, termbox.ColorWhite, termbox.ColorDefault, helpMsg)
} else {
tbprint(0, height-2, termbox.ColorWhite, termbox.ColorDefault, helpMsg)
}
}
// canvasSize returns the terminal columns, rows, and cursor aspect ratio
func canvasSize() (int, int, float64) {
var size [4]uint16
if _, _, err := syscall.Syscall6(syscall.SYS_IOCTL, uintptr(os.Stdout.Fd()), uintptr(syscall.TIOCGWINSZ), uintptr(unsafe.Pointer(&size)), 0, 0, 0); err != 0 {
panic(err)
}
rows, cols, width, height := size[0], size[1], size[2], size[3]
var whratio = defaultRatio
if width > 0 && height > 0 {
whratio = float64(height/rows) / float64(width/cols)
}
return int(cols), int(rows), whratio
}
func intInSlice(a int, list []int) bool {
for _, b := range list {
if b == a {
return true
}
}
return false
}