-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdisplay.go
141 lines (124 loc) · 3.48 KB
/
display.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
package main
import (
"fmt"
"math/rand"
"strings"
"github.com/charmbracelet/lipgloss"
)
func getEnglishName(names []PokemonSpeciesName) string {
for _, name := range names {
if name.Language.Name == "en" {
return name.Name
}
}
return ""
}
func getEnglishGenus(genera []PokemonSpeciesGenera) string {
for _, genus := range genera {
if genus.Language.Name == "en" {
return genus.Genus
}
}
return ""
}
func getEnglishFlavorText(entries []PokemonSpeciesFlavorText) string {
for i := len(entries) - 1; i >= 0; i-- {
if entries[i].Language.Name == "en" {
return strings.ReplaceAll(entries[i].FlavorText, "\n", " ")
}
}
return ""
}
func getTypeBadges(types []PokemonType) string {
var badges []string
for _, t := range types {
color := pokemonTypeColor(t.TypeData.Name)
badges = append(badges, createTextBadge(strings.ToUpper(t.TypeData.Name), color, false))
}
return strings.Join(badges, " ")
}
func createTextBadge(text string, color lipgloss.Color, bold bool) string {
style := lipgloss.NewStyle().Foreground(lipgloss.Color("0")).Background(color).Padding(0, 1)
if bold {
style = style.Bold(true)
}
return style.Render(text)
}
func pokemonTypeColor(pokemonType string) lipgloss.Color {
colors := map[string]lipgloss.Color{
"normal": lipgloss.Color("15"),
"fire": lipgloss.Color("9"),
"water": lipgloss.Color("12"),
"electric": lipgloss.Color("11"),
"grass": lipgloss.Color("10"),
"ice": lipgloss.Color("14"),
"fighting": lipgloss.Color("1"),
"poison": lipgloss.Color("5"),
"ground": lipgloss.Color("3"),
"flying": lipgloss.Color("13"),
"psychic": lipgloss.Color("13"),
"bug": lipgloss.Color("2"),
"rock": lipgloss.Color("3"),
"ghost": lipgloss.Color("5"),
"dragon": lipgloss.Color("13"),
"dark": lipgloss.Color("8"),
"steel": lipgloss.Color("7"),
"fairy": lipgloss.Color("13"),
}
if color, ok := colors[pokemonType]; ok {
return color
}
return lipgloss.Color("0")
}
func getShinyOrRegular(shiny bool) string {
if shiny {
return "shiny"
}
return "regular"
}
func getShinyOrRegularColor(shiny bool) lipgloss.Color {
if shiny {
return lipgloss.Color("11")
}
return lipgloss.Color("15")
}
func rollShiny(chance float64) bool {
return rand.Float64() < chance
}
func formatPokemonInfo(dexBadge, name, genus, typeBadges, height, weight, flavorText string, mainColor lipgloss.Color) string {
title := formatTitle(dexBadge, name, genus, mainColor)
details := formatDetails(height, weight)
flavorTextBox := formatFlavorText(flavorText)
return lipgloss.JoinVertical(
lipgloss.Top,
title,
lipgloss.JoinVertical(
lipgloss.Center,
"",
typeBadges,
"",
details,
"",
flavorTextBox,
),
)
}
func formatTitle(dexBadge, name, genus string, mainColor lipgloss.Color) string {
return dexBadge + lipgloss.NewStyle().Bold(true).Foreground(mainColor).Render(fmt.Sprintf(" %s - %s", name, genus))
}
func formatDetails(height, weight string) string {
return lipgloss.JoinHorizontal(
lipgloss.Top,
lipgloss.NewStyle().Foreground(lipgloss.Color("15")).Render("Height: "+lipgloss.NewStyle().Bold(true).Render(height)),
lipgloss.NewStyle().Foreground(lipgloss.Color("15")).Render(" Weight: "+lipgloss.NewStyle().Bold(true).Render(weight)),
)
}
func formatFlavorText(flavorText string) string {
return lipgloss.NewStyle().
Border(lipgloss.RoundedBorder()).
BorderForeground(lipgloss.Color("15")).
Foreground(lipgloss.Color("15")).
Padding(0, 1).
Width(40).
Render(flavorText)
}