This repository has been archived by the owner on Feb 12, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathselect.go
231 lines (182 loc) · 4.23 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
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
package component
import (
"fmt"
"github.com/jroimartin/gocui"
)
// Select struct
type Select struct {
*InputField
options []string
currentOpt int
isExpanded bool
ctype ComponentType
listColor *Attributes
listHandlers Handlers
}
// NewSelect new select
func NewSelect(gui *gocui.Gui, label string, x, y, labelWidth, fieldWidth int) *Select {
s := &Select{
InputField: NewInputField(gui, label, x, y, labelWidth, fieldWidth),
listHandlers: make(Handlers),
ctype: TypeSelect,
}
s.AddHandler(gocui.KeyEnter, s.expandOpt)
s.AddAttribute(gocui.ColorBlack, gocui.ColorWhite, gocui.ColorBlack, gocui.ColorGreen).
AddListHandler('j', s.nextOpt).
AddListHandler('k', s.preOpt).
AddListHandler(gocui.KeyArrowDown, s.nextOpt).
AddListHandler(gocui.KeyArrowUp, s.preOpt).
AddListHandler(gocui.KeyEnter, s.selectOpt).
SetEditable(false)
return s
}
// AddOptions add select options
func (s *Select) AddOptions(opts ...string) *Select {
for _, opt := range opts {
s.options = append(s.options, opt)
}
return s
}
// AddOption add select option
func (s *Select) AddOption(opt string) *Select {
s.options = append(s.options, opt)
return s
}
// AddAttribute add select attribute
func (s *Select) AddAttribute(textColor, textBgColor, fgColor, bgColor gocui.Attribute) *Select {
s.listColor = &Attributes{
textColor: textColor,
textBgColor: textBgColor,
hilightColor: fgColor,
hilightBgColor: bgColor,
}
return s
}
// AddListHandler add list handler
func (s *Select) AddListHandler(key Key, handler Handler) *Select {
s.listHandlers[key] = handler
return s
}
// GetSelected get selected option
func (s *Select) GetSelected() string {
return s.options[s.currentOpt]
}
// Focus set focus to select
func (s *Select) Focus() {
s.Gui.Cursor = true
s.Gui.SetCurrentView(s.GetLabel())
}
// UnFocus un focus
func (s *Select) UnFocus() {
s.Gui.Cursor = false
}
// GetType get component type
func (s *Select) GetType() ComponentType {
return s.ctype
}
// Close close select
func (s *Select) Close() {
s.InputField.Close()
if s.isExpanded {
for _, opt := range s.options {
s.DeleteView(opt)
s.DeleteKeybindings(opt)
}
}
}
// Draw draw select
func (s *Select) Draw() {
if len(s.options) > 0 {
s.InputField.SetText(s.options[0])
}
s.InputField.Draw()
}
func (s *Select) nextOpt(g *gocui.Gui, v *gocui.View) error {
maxOpt := len(s.options)
if maxOpt == 0 {
return nil
}
v.Highlight = false
next := s.currentOpt + 1
if next >= maxOpt {
next = s.currentOpt
}
s.currentOpt = next
v, _ = g.SetCurrentView(s.options[next])
v.Highlight = true
return nil
}
func (s *Select) preOpt(g *gocui.Gui, v *gocui.View) error {
maxOpt := len(s.options)
if maxOpt == 0 {
return nil
}
v.Highlight = false
next := s.currentOpt - 1
if next < 0 {
next = 0
}
s.currentOpt = next
v, _ = g.SetCurrentView(s.options[next])
v.Highlight = true
return nil
}
func (s *Select) selectOpt(g *gocui.Gui, v *gocui.View) error {
if !s.isExpanded {
s.expandOpt(g, v)
} else {
s.closeOpt(g, v)
}
return nil
}
func (s *Select) expandOpt(g *gocui.Gui, vi *gocui.View) error {
if s.hasOpts() {
s.isExpanded = true
g.Cursor = false
x := s.field.X
w := s.field.W
y := s.field.Y
h := y + 2
for _, opt := range s.options {
y++
h++
if v, err := g.SetView(opt, x, y, w, h); err != nil {
if err != gocui.ErrUnknownView {
panic(err)
}
v.Frame = false
v.SelFgColor = s.listColor.textColor
v.SelBgColor = s.listColor.textBgColor
v.FgColor = s.listColor.hilightColor
v.BgColor = s.listColor.hilightBgColor
for key, handler := range s.listHandlers {
if err := g.SetKeybinding(v.Name(), key, gocui.ModNone, handler); err != nil {
panic(err)
}
}
fmt.Fprint(v, opt)
}
}
v, _ := g.SetCurrentView(s.options[s.currentOpt])
v.Highlight = true
}
return nil
}
func (s *Select) closeOpt(g *gocui.Gui, v *gocui.View) error {
s.isExpanded = false
g.Cursor = true
for _, opt := range s.options {
g.DeleteView(opt)
g.DeleteKeybindings(opt)
}
v, _ = g.SetCurrentView(s.GetLabel())
v.Clear()
fmt.Fprint(v, s.GetSelected())
return nil
}
func (s *Select) hasOpts() bool {
if len(s.options) > 0 {
return true
}
return false
}