-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathroulette.go
89 lines (67 loc) · 1.46 KB
/
roulette.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
package main
import (
"strings"
"time"
tea "github.com/charmbracelet/bubbletea"
"github.com/charmbracelet/lipgloss"
)
type Model struct {
options []string
currId int
}
func InitialModel(options []string) Model {
return Model{
options: options,
currId: len(options) / 2,
}
}
func (m Model) Init() tea.Cmd {
return doTick()
}
func (m Model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
switch msg := msg.(type) {
case tea.KeyMsg:
switch msg.String() {
case "ctrl+c", "q":
return m, tea.Quit
}
case tickMsg:
m.currId = (m.currId + 1) % len(m.options)
return m, doTick()
}
return m, nil
}
type tickMsg time.Time
func doTick() tea.Cmd {
return tea.Tick(time.Millisecond*80, func(t time.Time) tea.Msg {
return tickMsg(t)
})
}
var (
itemStyle = lipgloss.NewStyle().
Padding(0, 5).
Width(20).
Height(1).
Align(lipgloss.Center)
selectedItemStyle = itemStyle.Copy().
Background(lipgloss.Color("1")).
Foreground(lipgloss.Color("0")).
Bold(true)
)
func (m Model) View() string {
s := strings.Builder{}
renderedOptions := make([]string, len(m.options))
for i := 0; i < len(m.options); i++ {
option := m.options[(i+m.currId)%len(m.options)]
var style lipgloss.Style
if i == len(m.options)/2 {
style = selectedItemStyle
} else {
style = itemStyle
}
renderedOptions[i] = style.Render(option)
}
s.WriteString(lipgloss.JoinVertical(lipgloss.Center, renderedOptions...))
s.WriteString("\n")
return s.String()
}