-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
323 lines (272 loc) · 6.97 KB
/
main.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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
package main
import (
"fmt"
"io"
"os"
"strings"
"time"
"github.com/charmbracelet/bubbles/help"
"github.com/charmbracelet/bubbles/key"
"github.com/charmbracelet/bubbles/list"
"github.com/charmbracelet/bubbles/paginator"
"github.com/charmbracelet/bubbles/progress"
tea "github.com/charmbracelet/bubbletea"
"github.com/charmbracelet/lipgloss"
"github.com/muesli/termenv"
"github.com/sahilmulla/poke-tui/pkgs/domain"
"github.com/sahilmulla/poke-tui/pkgs/pokemon"
"github.com/sahilmulla/poke-tui/pkgs/styles"
)
type NavState int
const (
POKEMON_LIST NavState = iota
LOADING_POKEMON_LIST
POKEMON_DETAILS
LOADING_POKEMON_DETAILS
QUITTING
)
type model struct {
pokemonList list.Model
navState NavState
details pokemon.PokemonDetail
statBars []progress.Model
unit domain.Unit
}
func initialModel() model {
m :=
model{
navState: LOADING_POKEMON_LIST,
statBars: make([]progress.Model, 6),
}
m.initPokemonList()
return m
}
func (m *model) initPokemonList() {
l := list.New([]list.Item{}, itemDelegate{}, 0, 0)
l.SetShowTitle(false)
l.SetStatusBarItemName("pokemon", "pokemons")
l.Paginator.Type = paginator.Arabic
l.AdditionalShortHelpKeys = func() []key.Binding {
return []key.Binding{listAdditionalKeys.Enter}
}
l.AdditionalFullHelpKeys = func() []key.Binding {
return []key.Binding{listAdditionalKeys.Enter, keys.Quit}
}
l.DisableQuitKeybindings()
m.pokemonList = l
}
func (m model) Init() tea.Cmd {
return getAllPokemonItems
}
type (
keyMap struct {
Quit key.Binding
}
listAdditionalKeyMap struct {
Enter key.Binding
}
detailKeyMap struct {
Back key.Binding
Tab key.Binding
}
)
var (
keys = keyMap{
Quit: key.NewBinding(
key.WithKeys("ctrl+c"),
key.WithHelp("ctrl+c", "quit"),
),
}
listAdditionalKeys = listAdditionalKeyMap{
Enter: key.NewBinding(
key.WithKeys("enter", " "),
key.WithHelp("enter/space", "view"),
),
}
detailKeys = detailKeyMap{
Back: key.NewBinding(
key.WithKeys("backspace"),
key.WithHelp("backspace", "list"),
),
Tab: key.NewBinding(
key.WithKeys("tab"),
key.WithHelp("tab", "units"),
),
}
)
func (k keyMap) ShortHelp() []key.Binding {
return []key.Binding{detailKeys.Tab, detailKeys.Back, k.Quit}
}
func (k keyMap) FullHelp() [][]key.Binding {
return [][]key.Binding{k.ShortHelp(), {}}
}
type (
DetailsMsg struct {
data pokemon.PokemonDetail
}
GotPokemonsMsg struct {
data []list.Item
}
QuitAppMsg int
)
func QuitApp() tea.Msg {
time.Sleep(600 * time.Millisecond)
return QuitAppMsg(0)
}
func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
cmds := []tea.Cmd{}
switch msg := msg.(type) {
case tea.KeyMsg:
switch {
case key.Matches(msg, keys.Quit):
m.navState = QUITTING
return m, QuitApp
case key.Matches(msg, detailKeys.Back):
switch m.navState {
case POKEMON_DETAILS:
m.navState = POKEMON_LIST
}
case key.Matches(msg, detailKeys.Tab):
switch m.navState {
case POKEMON_DETAILS:
if m.unit == domain.US {
m.unit = domain.NOT_US
} else {
m.unit = domain.US
}
}
case key.Matches(msg, listAdditionalKeys.Enter):
switch m.navState {
case POKEMON_LIST:
if m.pokemonList.FilterState() != list.Filtering {
i, ok := m.pokemonList.SelectedItem().(item)
if ok {
cmds = append(cmds, getPokemonDetails(i.title))
m.navState = LOADING_POKEMON_DETAILS
}
}
}
}
case QuitAppMsg:
return m, tea.Quit
case DetailsMsg:
m.details = msg.data
for idx, stat := range m.details.Info.Stats {
newStat := progress.New(
progress.WithScaledGradient("#e44", "#2b8"),
progress.WithWidth(25))
newStat.Full = rune('∎')
newStat.Empty = rune('-')
newStat.ShowPercentage = false
newStat.SetSpringOptions(15.0, 1)
cmds = append(cmds, newStat.SetPercent(float64(stat.Base)/200))
m.statBars[idx] = newStat
}
m.navState = POKEMON_DETAILS
case GotPokemonsMsg:
m.pokemonList.SetItems(msg.data)
m.navState = POKEMON_LIST
case tea.WindowSizeMsg:
w, h := styles.DocStyle.GetFrameSize()
m.pokemonList.SetSize(msg.Width-w, min(msg.Height-h, 20))
case progress.FrameMsg:
for idx, pModel := range m.statBars {
newModel, newCmd := pModel.Update(msg)
m.statBars[idx] = newModel.(progress.Model)
cmds = append(cmds, newCmd)
}
}
var cmd tea.Cmd
switch m.navState {
case POKEMON_LIST:
m.pokemonList, cmd = m.pokemonList.Update(msg)
cmds = append(cmds, cmd)
}
return m, tea.Batch(cmds...)
}
func renderPokemonDetails(m model) string {
header := pokemon.RenderHeader(m.details)
content := lipgloss.JoinVertical(lipgloss.Left,
header,
pokemon.RenderInfo(m.details, m.unit),
pokemon.RenderStats(m.details, &m.statBars),
pokemon.RenderAbilities(m.details),
pokemon.RenderEvolutionTree(m.details),
pokemon.RenderVarieties(m.details),
strings.Repeat("─", lipgloss.Width(header)))
return content
}
func (m model) View() string {
s := ""
switch m.navState {
case LOADING_POKEMON_DETAILS, LOADING_POKEMON_LIST, QUITTING:
msg := " LOADING "
if m.navState == QUITTING {
msg = " Gotta Catch 'Em All "
}
s += lipgloss.Place(48, 20,
lipgloss.Center, lipgloss.Center,
lipgloss.NewStyle().Render(msg),
lipgloss.WithWhitespaceChars("`"),
lipgloss.WithWhitespaceForeground(lipgloss.ANSIColor(termenv.ANSIBrightBlack)))
case POKEMON_LIST:
s += styles.DocStyle.Render(m.pokemonList.View())
case POKEMON_DETAILS:
s += renderPokemonDetails(m)
s += "\n" + lipgloss.NewStyle().Margin(1, 2).Render(help.New().View(keys))
}
return s
}
func main() {
p := tea.NewProgram(
initialModel(),
tea.WithAltScreen(),
)
if _, err := p.Run(); err != nil {
fmt.Println("Error running program:", err)
os.Exit(1)
}
}
func getAllPokemonItems() tea.Msg {
result := []list.Item{}
data := pokemon.GetPokemons()
for _, el := range data.Results {
result = append(result, item{title: el.Name})
}
return GotPokemonsMsg{data: result}
}
var pokemonDetailsCache = map[string]pokemon.PokemonDetail{}
func getPokemonDetails(name string) tea.Cmd {
return func() tea.Msg {
cached, hit := pokemonDetailsCache[name]
if hit {
return DetailsMsg{data: cached}
}
fetched := pokemon.GetPokemon(name)
pokemonDetailsCache[name] = fetched
return DetailsMsg{data: fetched}
}
}
type item struct {
title string
}
func (i item) Title() string { return i.title }
func (i item) FilterValue() string { return strings.ReplaceAll(i.title, "-", " ") }
type itemDelegate struct{}
func (d itemDelegate) Height() int { return 1 }
func (d itemDelegate) Spacing() int { return 0 }
func (d itemDelegate) Update(_ tea.Msg, _ *list.Model) tea.Cmd { return nil }
func (d itemDelegate) Render(w io.Writer, m list.Model, index int, listItem list.Item) {
i, ok := listItem.(item)
if !ok {
return
}
str := styles.TransformTitle(i.Title())
render := styles.ItemStyle.Render
if index == m.Index() {
render = func(s ...string) string {
return styles.SelectedItemStyle.Render(strings.Join(s, " "))
}
}
fmt.Fprint(w, render(str))
}