Skip to content

Commit db1ec6e

Browse files
fixing string formatting (#85)
1 parent 8bb9c55 commit db1ec6e

File tree

2 files changed

+35
-6
lines changed

2 files changed

+35
-6
lines changed

cmd/pokemon.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ func PokemonCommand() {
5353
}
5454

5555
_, pokemonName, pokemonID, pokemonWeight, pokemonHeight := connections.PokemonApiCall(endpoint, pokemonName, "https://pokeapi.co/api/v2/")
56-
capitalizedString := cases.Title(language.English).String(pokemonName)
56+
capitalizedString := cases.Title(language.English).String(strings.Replace(pokemonName, "-", " ", -1))
5757

5858
// Weight calculation
5959
weightKilograms := float64(pokemonWeight) / 10

flags/pokemonflagset.go

Lines changed: 34 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ import (
77
"fmt"
88
"github.com/charmbracelet/lipgloss"
99
"github.com/digitalghost-dev/poke-cli/connections"
10+
"golang.org/x/text/cases"
11+
"golang.org/x/text/language"
1012
"strings"
1113
)
1214

@@ -58,13 +60,38 @@ func AbilitiesFlag(endpoint string, pokemonName string) error {
5860

5961
header("Abilities")
6062

63+
// Anonymous function to format ability names
64+
formatAbilityName := func(name string) string {
65+
exceptions := map[string]bool{
66+
"of": true,
67+
"the": true,
68+
"to": true,
69+
"as": true,
70+
}
71+
72+
name = strings.Replace(name, "-", " ", -1)
73+
words := strings.Split(name, " ")
74+
titleCaser := cases.Title(language.English)
75+
76+
// Process each word
77+
for i, word := range words {
78+
if _, found := exceptions[strings.ToLower(word)]; found && i != 0 {
79+
words[i] = strings.ToLower(word)
80+
} else {
81+
words[i] = titleCaser.String(word)
82+
}
83+
}
84+
return strings.Join(words, " ")
85+
}
86+
6187
for _, pokeAbility := range pokemonStruct.Abilities {
88+
formattedName := formatAbilityName(pokeAbility.Ability.Name)
6289
if pokeAbility.Slot == 1 {
63-
fmt.Printf("Ability %d: %s\n", pokeAbility.Slot, pokeAbility.Ability.Name)
90+
fmt.Printf("Ability %d: %s\n", pokeAbility.Slot, formattedName)
6491
} else if pokeAbility.Slot == 2 {
65-
fmt.Printf("Ability %d: %s\n", pokeAbility.Slot, pokeAbility.Ability.Name)
92+
fmt.Printf("Ability %d: %s\n", pokeAbility.Slot, formattedName)
6693
} else {
67-
fmt.Printf("Hidden Ability: %s\n", pokeAbility.Ability.Name)
94+
fmt.Printf("Hidden Ability: %s\n", formattedName)
6895
}
6996
}
7097

@@ -181,9 +208,11 @@ func TypesFlag(endpoint string, pokemonName string) error {
181208
colorHex, exists := colorMap[pokeType.Type.Name]
182209
if exists {
183210
color := lipgloss.Color(colorHex)
184-
fmt.Printf("Type %d: %s\n", pokeType.Slot, lipgloss.NewStyle().Bold(true).Foreground(color).Render(pokeType.Type.Name))
211+
style := lipgloss.NewStyle().Bold(true).Foreground(color)
212+
styledName := style.Render(cases.Title(language.English).String(pokeType.Type.Name)) // Apply styling here
213+
fmt.Printf("Type %d: %s\n", pokeType.Slot, styledName) // Interpolate styled text
185214
} else {
186-
fmt.Printf("Type %d: %s\n", pokeType.Slot, pokeType.Type.Name)
215+
fmt.Printf("Type %d: %s\n", pokeType.Slot, cases.Title(language.English).String(pokeType.Type.Name))
187216
}
188217
}
189218

0 commit comments

Comments
 (0)