-
Notifications
You must be signed in to change notification settings - Fork 0
/
radio.go
101 lines (84 loc) · 2.3 KB
/
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
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
package prompts
import (
"fmt"
"strings"
tea "github.com/charmbracelet/bubbletea"
)
type radioModel struct {
choices []string
cursor int
label string
checked int
}
// prompt user to choose one of several choices , and return the checked indexe
//
// example :
//
// prompts.RadioBox("you are intersted at ", []string{"gaming", "coding"})
func RadioBox(label string, choices []string) (int, error) {
res := tea.NewProgram(radioModel{
choices: choices,
label: label,
})
selected, err := res.Run()
s := selected.(radioModel)
return s.checked, err
}
func (s radioModel) Init() tea.Cmd {
return nil
}
func (s radioModel) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
switch msg := msg.(type) {
case tea.KeyMsg:
switch msg.String() {
case "ctrl+c", "q":
return s, tea.Quit
case "up", "k", "left":
if s.cursor > 0 {
s.cursor--
} else {
s.cursor = len(s.choices) - 1
}
case "down", "j", "right":
if s.cursor < len(s.choices)-1 {
s.cursor++
} else {
s.cursor = 0
}
case "enter", " ":
s.checked = s.cursor
}
}
return s, nil
}
func (s radioModel) View() string {
m := "\n"
m += style().Margin(0, 0, 0, 1).Foreground(color("#495867")).Render("┌─")
m += style().Margin(0, 1, 0, 1).Foreground(color("#07beb8")).Render(s.label)
m += style().Foreground(color("#495867")).Render(strings.Repeat("─", getTrmW()-charWidth(s.label)-7))
m += style().Foreground(color("#495867")).Render("┐")
m += "\n"
for i, choice := range s.choices {
cursor := style().Foreground(color("#495867")).Render(" │")
if s.cursor == i {
cursor += style().Foreground(color("#07beb8")).Render("➧")
} else {
cursor += " "
}
checked := style().Foreground(color("#495867")).Render("○ ")
if s.checked == i {
checked = style().Foreground(color("#07beb8")).Render("◉ ")
}
m += fmt.Sprintf("%s %s %s", cursor, checked, choice)
m += strings.Repeat(" ", getTrmW()-9-len(choice))
m += style().Foreground(color("#495867")).Render("│")
m += "\n"
}
m += style().Foreground(color("#495867")).Render(" └")
m += style().Foreground(color("#495867")).Render(strings.Repeat("─", getTrmW()-4))
m += style().Foreground(color("#495867")).Render("┘")
m += "\n"
m += style().Foreground(color("#444")).Render(" q quit ⬩ ↵ select.")
m += "\n\n"
return m
}