-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathscoreboards.go
116 lines (103 loc) · 3.11 KB
/
scoreboards.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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
package ui
import (
"time"
"github.com/dylantientcheu/nbacli/nba"
"github.com/dylantientcheu/nbacli/ui/constants"
"github.com/charmbracelet/bubbles/key"
"github.com/charmbracelet/bubbles/list"
tea "github.com/charmbracelet/bubbletea"
)
type mode int
type SelectMsg struct {
ActiveScorebardID uint
}
const (
nav mode = iota
edit
)
type Model struct {
mode mode
list list.Model
currentDate time.Time
quitting bool
gameview bool
}
func InitScoreboard(date time.Time) tea.Model {
items := newScoreboardList(nba.Sb, date)
m := Model{mode: nav, currentDate: date, list: list.NewModel(items, list.NewDefaultDelegate(), 8, 8)}
if constants.WindowSize.Height != 0 {
top, right, bottom, left := constants.DocStyle.GetMargin()
m.list.SetSize(constants.WindowSize.Width-left-right, constants.WindowSize.Height-top-bottom-1)
}
m.list.Title = "NBA Games - " + m.currentDate.Format("Monday, 2 Jan 06")
m.list.AdditionalShortHelpKeys = func() []key.Binding {
return []key.Binding{
constants.Keymap.Tomorrow,
constants.Keymap.Yesterday,
constants.Keymap.Back,
}
}
return m
}
func newScoreboardList(scbrd *nba.ScoreboardRepository, date time.Time) []list.Item {
games := scbrd.GetGames(date)
return gamesToItems(games)
}
// Init run any intial IO on program start
func (m Model) Init() tea.Cmd {
return nil
}
// Update handle IO and commands
func (m Model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
var cmd tea.Cmd
var cmds []tea.Cmd
switch msg := msg.(type) {
case tea.WindowSizeMsg:
constants.WindowSize = msg
top, right, bottom, left := constants.DocStyle.GetMargin()
m.list.SetSize(msg.Width-left-right, msg.Height-top-bottom-1)
case tea.KeyMsg:
switch {
case key.Matches(msg, constants.Keymap.Yesterday):
var previousDay nba.ScoreboardRepository
m.currentDate = m.currentDate.AddDate(0, 0, -1)
games := previousDay.GetGames(m.currentDate)
items := gamesToItems(games)
m.list.Title = "NBA Games - " + m.currentDate.Format("Monday, 2 Jan 06")
m.list.SetItems(items)
case key.Matches(msg, constants.Keymap.Tomorrow):
var nextDay nba.ScoreboardRepository
m.currentDate = m.currentDate.AddDate(0, 0, 1)
games := nextDay.GetGames(m.currentDate)
items := gamesToItems(games)
m.list.Title = "NBA Games - " + m.currentDate.Format("Monday, 2 Jan 06")
m.list.SetItems(items)
case key.Matches(msg, constants.Keymap.Quit):
m.quitting = true
return m, tea.Quit
case key.Matches(msg, constants.Keymap.Enter):
m.gameview = true
activeGame := m.list.SelectedItem().(nba.BoxScoreSummary)
gameView := InitGameView(activeGame.GameId, activeGame, m)
return gameView.Update(constants.WindowSize)
default:
m.list, cmd = m.list.Update(msg)
}
cmds = append(cmds, cmd)
}
return m, tea.Batch(cmds...)
}
// View return the text UI to be output to the terminal
func (m Model) View() string {
if m.quitting {
return ""
}
return constants.DocStyle.Render(m.list.View() + "\n")
}
func gamesToItems(games []nba.BoxScoreSummary) []list.Item {
items := make([]list.Item, len(games))
for i, proj := range games {
items[i] = list.Item(proj)
}
return items
}