-
Notifications
You must be signed in to change notification settings - Fork 0
/
radio.go
61 lines (53 loc) · 984 Bytes
/
radio.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
package customise
import (
"fmt"
"strings"
tea "github.com/charmbracelet/bubbletea"
)
type RadioButton struct {
choices []string
selected int
}
func (r RadioButton) Init() tea.Cmd {
return nil
}
func (r *RadioButton) SetValues(v []string, selected string) {
r.choices = v
r.selected = 0
for i := range v {
if strings.EqualFold(selected, v[i]) {
r.selected = i
break
}
}
}
func (r RadioButton) Update(msg tea.Msg) (RadioButton, tea.Cmd) {
switch msg := msg.(type) {
case tea.KeyMsg:
switch msg.String() {
case "up":
if 0 < r.selected {
r.selected--
}
case "down":
if r.selected < len(r.choices)-1 {
r.selected++
}
}
}
return r, nil
}
func (r RadioButton) View() string {
var buf strings.Builder
for i, val := range r.choices {
cur := "( ) "
if i == r.selected {
cur = "(*) "
}
fmt.Fprintf(&buf, "%s%s\n", cur, val)
}
return buf.String()
}
func (r RadioButton) Value() string {
return r.choices[r.selected]
}