Skip to content

Commit 145788f

Browse files
adding learnable moves to lipgloss table (#127)
1 parent 54b7449 commit 145788f

File tree

1 file changed

+44
-30
lines changed

1 file changed

+44
-30
lines changed

flags/pokemonflagset.go

Lines changed: 44 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -210,7 +210,7 @@ func MovesFlag(w io.Writer, endpoint string, pokemonName string) error {
210210
baseURL := "https://pokeapi.co/api/v2/"
211211
pokemonStruct, _, _, _, _, _ := connections.PokemonApiCall(endpoint, pokemonName, baseURL)
212212

213-
_, err := fmt.Fprintln(w, header("Moves"))
213+
_, err := fmt.Fprintln(w, header("Learnable Moves"))
214214
if err != nil {
215215
return err
216216
}
@@ -238,18 +238,24 @@ func MovesFlag(w io.Writer, endpoint string, pokemonName string) error {
238238
continue
239239
}
240240

241+
capitalizedMove := cases.Title(language.English).String(strings.ReplaceAll(moveName, "-", " "))
242+
capitalizedType := cases.Title(language.English).String(moveStruct.Type.Name)
243+
241244
moves = append(moves, MoveInfo{
242245
Accuracy: moveStruct.Accuracy,
243246
Level: detail.LevelLearnedAt,
244-
Name: moveName,
247+
Name: capitalizedMove,
245248
Power: moveStruct.Power,
246-
Type: moveStruct.Type.Name,
249+
Type: capitalizedType,
247250
})
248251
}
249252
}
250253

251254
if len(moves) == 0 {
252-
fmt.Fprintln(w, "No level-up moves found for Scarlet & Violet.")
255+
_, err := fmt.Fprintln(w, "No level-up moves found for Scarlet & Violet.")
256+
if err != nil {
257+
return err
258+
}
253259
return nil
254260
}
255261

@@ -261,24 +267,53 @@ func MovesFlag(w io.Writer, endpoint string, pokemonName string) error {
261267
// Convert to table rows
262268
var rows [][]string
263269
for _, m := range moves {
270+
styledType := lipgloss.
271+
NewStyle().
272+
Bold(true).
273+
Foreground(lipgloss.Color(styling.ColorMap[strings.ToLower(m.Type)])).
274+
Render(m.Type)
275+
264276
rows = append(rows, []string{
265277
m.Name,
266-
m.Type,
267-
strconv.Itoa(m.Accuracy),
268278
strconv.Itoa(m.Level),
279+
styledType,
280+
strconv.Itoa(m.Accuracy),
269281
strconv.Itoa(m.Power),
270282
})
271283
}
272284

273285
// Build and print table
274286
color := lipgloss.AdaptiveColor{Light: "#4B4B4B", Dark: "#D3D3D3"}
287+
275288
t := table.New().
276289
Border(lipgloss.NormalBorder()).
277290
BorderStyle(lipgloss.NewStyle().Foreground(color)).
278-
Headers("Type", "Name", "Accuracy", "Level", "Power").
291+
StyleFunc(func(row, column int) lipgloss.Style {
292+
var style lipgloss.Style
293+
294+
switch column {
295+
case 0:
296+
style = style.Width(18)
297+
case 1:
298+
style = style.Width(8)
299+
case 2:
300+
style = style.Width(10)
301+
case 3:
302+
style = style.Width(10)
303+
case 4:
304+
style = style.Width(8)
305+
}
306+
307+
return style
308+
}).
309+
Headers("Name", "Level", "Type", "Accuracy", "Power").
279310
Rows(rows...)
280311

281-
fmt.Fprintln(w, t)
312+
_, err = fmt.Fprintln(w, t)
313+
if err != nil {
314+
return err
315+
}
316+
282317
return nil
283318
}
284319

@@ -382,35 +417,14 @@ func TypesFlag(w io.Writer, endpoint string, pokemonName string) error {
382417
baseURL := "https://pokeapi.co/api/v2/"
383418
pokemonStruct, _, _, _, _, _ := connections.PokemonApiCall(endpoint, pokemonName, baseURL)
384419

385-
colorMap := map[string]string{
386-
"normal": "#B7B7A9",
387-
"fire": "#FF4422",
388-
"water": "#3499FF",
389-
"electric": "#FFCC33",
390-
"grass": "#77CC55",
391-
"ice": "#66CCFF",
392-
"fighting": "#BB5544",
393-
"poison": "#AA5699",
394-
"ground": "#DEBB55",
395-
"flying": "#889AFF",
396-
"psychic": "#FF5599",
397-
"bug": "#AABC22",
398-
"rock": "#BBAA66",
399-
"ghost": "#6666BB",
400-
"dragon": "#7766EE",
401-
"dark": "#775544",
402-
"steel": "#AAAABB",
403-
"fairy": "#EE99EE",
404-
}
405-
406420
// Print the header from header func
407421
_, err := fmt.Fprintln(w, header("Typing"))
408422
if err != nil {
409423
return err
410424
}
411425

412426
for _, pokeType := range pokemonStruct.Types {
413-
colorHex, exists := colorMap[pokeType.Type.Name]
427+
colorHex, exists := styling.ColorMap[pokeType.Type.Name]
414428
if exists {
415429
color := lipgloss.Color(colorHex)
416430
style := lipgloss.NewStyle().Bold(true).Foreground(color)

0 commit comments

Comments
 (0)