-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmainMenu.go
More file actions
92 lines (78 loc) · 2.04 KB
/
Copy pathmainMenu.go
File metadata and controls
92 lines (78 loc) · 2.04 KB
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
package main
import (
"fmt"
"io"
"github.com/charmbracelet/bubbles/list"
tea "github.com/charmbracelet/bubbletea"
"github.com/charmbracelet/lipgloss"
)
type mainMenu struct {
model *model
cursor int
list list.Model
message tea.Msg
}
var (
mainMenuStyle = lipgloss.NewStyle().Margin(1, 2)
itemStyle = lipgloss.NewStyle().PaddingLeft(4)
selectedItemStyle = lipgloss.NewStyle().PaddingLeft(4).Foreground(lipgloss.Color("170"))
)
type item string
func (i item) FilterValue() string { return string(i) }
type delegate struct{}
func (d delegate) Height() int { return 1 }
func (d delegate) Spacing() int { return 0 }
func (d delegate) Update(msg tea.Msg, m *list.Model) tea.Cmd { return nil }
func (d delegate) Render(w io.Writer, m list.Model, index int, listItem list.Item) {
i, ok := listItem.(item)
if !ok {
return
}
str := fmt.Sprintf(" %s", i)
fn := itemStyle.Render
if index == m.Index() {
fn = func(s string) string {
return selectedItemStyle.Render("> " + s[2:])
}
}
fmt.Fprint(w, fn(str))
}
func NewMainMenu(m *model) *mainMenu {
items := []list.Item{
item("Play"),
item("How to play"),
item("Scores"),
}
list := list.New(items, delegate{}, 20, 14)
list.SetShowStatusBar(false)
list.SetFilteringEnabled(false)
list.Title = "Welcome to Vim-Minesweeper"
return &mainMenu{m, 0, list, nil}
}
func (m *mainMenu) update(msg tea.Msg) (tea.Model, tea.Cmd) {
switch msg := msg.(type) {
case tea.WindowSizeMsg:
m.list.SetWidth(msg.Width)
return m.model, nil
case tea.KeyMsg:
switch msg.String() {
case "ctrl+c", "q":
return m.model, tea.Quit
case "enter":
switch m.list.SelectedItem().FilterValue() {
case "Play":
m.model.current = m.model.playMenu
case "How to play":
m.model.current = m.model.instructions
case "Scores":
m.model.current = m.model.scores
}
}
}
var cmd tea.Cmd
m.list, cmd = m.list.Update(msg)
return m.model, cmd
}
func (m *mainMenu) view() string {
return mainMenuStyle.Render(m.list.View())
}