-
Notifications
You must be signed in to change notification settings - Fork 3
/
achievements.go
88 lines (73 loc) · 2.01 KB
/
achievements.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
package main
import (
"fmt"
"strings"
tea "github.com/charmbracelet/bubbletea"
"github.com/charmbracelet/lipgloss"
"github.com/sturdy-dev/marblezero/achievements"
)
type showAllAchievementsModel struct {
events []achievements.HistoryEvent
pages [][]achievements.Achievement
page int
}
func NewShowAllAchievementsModel(events []achievements.HistoryEvent) tea.Model {
var pages [][]achievements.Achievement
var page []achievements.Achievement
const perPage = 8
for _, a := range achievements.Achievements {
page = append(page, a)
if len(page) == perPage {
pages = append(pages, page)
page = []achievements.Achievement{}
}
}
if len(page) > 0 {
pages = append(pages, page)
}
return &showAllAchievementsModel{
events: events,
pages: pages,
}
}
func (m *showAllAchievementsModel) Init() tea.Cmd {
return nil
}
func (m *showAllAchievementsModel) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
switch msg := msg.(type) {
case tea.KeyMsg:
switch msg.String() {
case "down", "right", "tab", "n", "j", "l":
if m.page < len(m.pages)-1 {
m.page++
}
case "up", "left", "p", "h", "k":
if m.page > 0 {
m.page--
}
case "home":
m.page = 0
case "q", "esc", "enter":
return m, goToHomeCmd
}
}
return m, nil
}
func (m *showAllAchievementsModel) View() string {
var showAchievements []string = []string{
listHeader("All Achievements"),
}
for _, a := range m.pages[m.page] {
if ok, _ := a.Func(m.events); ok {
showAchievements = append(showAchievements, listDone(a.Name))
} else {
showAchievements = append(showAchievements, listItem(a.Name))
}
}
// align unstructions with bottom
if len(m.pages[m.page]) < 8 {
showAchievements = append(showAchievements, strings.Repeat("\n", 7-len(m.pages[m.page])))
}
showAchievements = append(showAchievements, lipgloss.NewStyle().Foreground(subtle).Render(fmt.Sprintf("Page %d/%d (n/p/q)", m.page+1, len(m.pages))))
return deviceRightStyle.Copy().PaddingLeft(1).Render(lipgloss.JoinVertical(lipgloss.Left, showAchievements...))
}