-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmodel_detail.go
244 lines (221 loc) · 6.3 KB
/
model_detail.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
package main
import (
"fmt"
"image"
_ "image/png"
"strings"
tea "github.com/charmbracelet/bubbletea"
"github.com/charmbracelet/lipgloss"
"github.com/ethanefung/pokedexcli/internal/namefinder"
"github.com/ethanefung/pokedexcli/internal/pokeapi"
"github.com/qeesung/image2ascii/convert"
)
var detailStyle = lipgloss.NewStyle()
type detail struct {
client pokeapi.Client
err error
no int
viewHeight int
name string
form string
description string
asciiSprite string
height string
weight string
evolutions []string
types []string
descriptions []string
stats stats
}
type stats struct {
hp string
attack string
defense string
specialAttack string
specialDefense string
speed string
}
type pType string
const (
normalType pType = "normal"
fireType = "fire"
waterType = "water"
electricType = "electric"
grassType = "grass"
iceType = "ice"
fightingType = "fighting"
poisonType = "poison"
groundType = "ground"
flyingType = "flying"
psychicType = "psychic"
bugType = "bug"
rockType = "rock"
ghostType = "ghost"
dragonType = "dragon"
darkType = "dark"
steelType = "steel"
fairyType = "fairy"
)
type pTypeStyle struct {
pType pType
bgColor string
color string
}
var pTypeStylesMap = map[string]pTypeStyle{
"normal": {normalType, "#A8A77A", "#FFF"},
"fire": {fireType, "#EE8130", "#FFF"},
"water": {waterType, "#6390F0", "#FFF"},
"electric": {electricType, "#F7D02C", "#FFF"},
"grass": {grassType, "#7AC74C", "#FFF"},
"ice": {iceType, "#96D9D6", "#FFF"},
"fighting": {fightingType, "#C22E28", "#FFF"},
"poison": {poisonType, "#A33EA1", "#FFF"},
"ground": {groundType, "#E2BF65", "#FFF"},
"flying": {flyingType, "#A98FF3", "#FFF"},
"psychic": {psychicType, "#F95587", "#FFF"},
"bug": {bugType, "#A6B91A", "#FFF"},
"rock": {rockType, "#B6A136", "#FFF"},
"ghost": {ghostType, "#735797", "#FFF"},
"dragon": {dragonType, "#6F35FC", "#FFF"},
"dark": {darkType, "#705746", "#FFF"},
"steel": {steelType, "#B7B7CE", "#FFF"},
"fairy": {fairyType, "#D685AD", "#FFF"},
}
func (d detail) Init() tea.Cmd { return nil }
func (d detail) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
var cmd tea.Cmd
d.err = nil
switch msg := msg.(type) {
case error:
d.err = msg
case tea.WindowSizeMsg:
d.viewHeight = msg.Height - 2
case namefinder.BasicPokemonInfo:
d.name = msg.Name
d.no = msg.ID
d.form = msg.Form
case pokeapi.Pokemon:
d.height = decimetresToImperialUnits(msg.Height)
d.weight = hectogramsToPounds(msg.Weight)
d.types = []string{}
for _, t := range msg.Types {
d.types = append(d.types, t.Type.Name)
}
for _, s := range msg.Stats {
switch s.Stat.Name {
case "hp":
d.stats.hp = fmt.Sprintf("%d", s.BaseStat)
case "attack":
d.stats.attack = fmt.Sprintf("%d", s.BaseStat)
case "defense":
d.stats.defense = fmt.Sprintf("%d", s.BaseStat)
case "special-attack":
d.stats.specialAttack = fmt.Sprintf("%d", s.BaseStat)
case "special-defense":
d.stats.specialDefense = fmt.Sprintf("%d", s.BaseStat)
case "speed":
d.stats.speed = fmt.Sprintf("%d", s.BaseStat)
}
}
cmd = getPokemonSpecies(d.client, msg)
case pokeapi.PokemonSpecies:
// find the first english entry and use as the description
for _, entry := range msg.FlavorTextEntries {
if entry.Language.Name == "en" {
d.description = strings.Join(strings.Fields(entry.FlavorText), " ")
break
}
}
case image.Image:
converter := convert.NewImageConverter()
options := convert.Options{
FixedWidth: 64,
FixedHeight: 32,
FitScreen: true,
Colored: true,
}
d.asciiSprite = converter.Image2ASCIIString(msg, &options)
}
return d, cmd
}
func (d detail) View() string {
if d.err == ErrPokemon {
return fmt.Sprintf("Error: %v", d.err)
}
var s string
name := strings.ToLower(d.name)
name = strings.ReplaceAll(name, "-", " ")
if name != "" {
name = strings.ToTitle(string(name[0])) + name[1:]
}
s += fmt.Sprintf("\n\n No. %04d %s %s\n", d.no, name, d.form)
if d.asciiSprite != "" {
s += d.asciiSprite + "\n"
}
s += d.viewNumbers()
s += d.viewDescription() + "\n"
return detailStyle.Height(d.viewHeight).Render(s)
}
func (d detail) viewNumbers() string {
// var s string
// lipgloss.
// s += strings.Repeat("-", 64) + "\n\n"
var left string
left += " types : "
for _, t := range d.types {
pts := pTypeStylesMap[t]
left += pts.String()
}
left += "\n"
left += " height: " + d.height + "\n"
left += " weight: " + d.weight + "\n\n"
left = lipgloss.PlaceHorizontal(34, lipgloss.Left, left)
var right string
var statsLeft string
var statsRight string
right += " base stats:\n"
statsLeft += " hp : " + d.stats.hp + "\n"
statsLeft += " atk: " + d.stats.attack + "\n"
statsLeft += " def: " + d.stats.defense + "\n"
statsRight += " speed : " + d.stats.speed + "\n"
statsRight += " spl atk: " + d.stats.specialAttack + "\n"
statsRight += " spl def: " + d.stats.specialDefense + "\n"
right += lipgloss.JoinHorizontal(lipgloss.Left, statsLeft, statsRight)
right = lipgloss.PlaceHorizontal(30, lipgloss.Left, right)
return lipgloss.JoinHorizontal(lipgloss.Left, left, right) + "\n"
}
func (d detail) viewDescription() string {
var desc string
if d.err == ErrSpecies {
desc = "Error: getting species"
} else {
desc = d.description
}
return lipgloss.NewStyle().Width(64).Padding(0, 4, 2).Render(desc)
}
func (pts pTypeStyle) String() string {
return lipgloss.NewStyle().
Background(lipgloss.Color(pts.bgColor)).
Foreground(lipgloss.Color(pts.color)).
Padding(0, 1).
Render(string(pts.pType))
}
func decimetresToImperialUnits(decimetres int) string {
inches := (decimetres * 1000) / 254
feet := inches / 12
inches = inches - (feet * 12)
return fmt.Sprintf("%d'%d\"", feet, inches)
}
func hectogramsToPounds(hectograms int) string {
var pounds float64 = float64(hectograms) * 0.220462
return fmt.Sprintf("%.2f lbs", pounds)
}
func getPokemonSpecies(client pokeapi.Client, p pokeapi.Pokemon) tea.Cmd {
return func() tea.Msg {
ps, err := client.GetPokemonSpecies(p.Species.Name)
if err != nil {
return ErrSpecies
}
return ps
}
}