Skip to content

Commit 9f9690a

Browse files
Merge pull request #95 from digitalghost-dev/0.11.0
0.11.0
2 parents 38e2ce1 + ee23c14 commit 9f9690a

File tree

13 files changed

+227
-44
lines changed

13 files changed

+227
-44
lines changed

.github/workflows/ci.yml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,12 @@ on:
2121
- 'go.mod'
2222
- 'go.sum'
2323
- '.goreleaser.yaml'
24+
- 'pokemon.svg'
2425
branches:
2526
- main
2627

2728
env:
28-
VERSION_NUMBER: 'v0.10.0'
29+
VERSION_NUMBER: 'v0.11.0'
2930
DOCKERHUB_REGISTRY_NAME: 'digitalghostdev/poke-cli'
3031
AWS_REGION: 'us-west-2'
3132

.goreleaser.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ builds:
1414
- windows
1515
- darwin
1616
ldflags:
17-
- -s -w -X main.version=v0.10.0
17+
- -s -w -X main.version=v0.11.0
1818

1919
archives:
2020
- format: tar.gz

Dockerfile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ RUN go mod download
88

99
COPY . .
1010

11-
RUN go build -ldflags "-X main.version=v0.10.0" -o poke-cli .
11+
RUN go build -ldflags "-X main.version=v0.11.0" -o poke-cli .
1212

1313
# build 2
1414
FROM gcr.io/distroless/static-debian12:nonroot

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
<img height="250" width="350" src="pokemon.svg" alt="pokemon-logo"/>
33
<h1>Pokémon CLI</h1>
44
<img src="https://img.shields.io/github/v/release/digitalghost-dev/poke-cli?style=flat-square&logo=git&logoColor=FFCC00&label=Release%20Version&labelColor=EEE&color=FFCC00" alt="version-label">
5-
<img src="https://img.shields.io/docker/image-size/digitalghostdev/poke-cli/v0.10.0?arch=arm64&style=flat-square&logo=docker&logoColor=FFCC00&labelColor=EEE&color=FFCC00" alt="docker-image-size">
5+
<img src="https://img.shields.io/docker/image-size/digitalghostdev/poke-cli/v0.11.0?arch=arm64&style=flat-square&logo=docker&logoColor=FFCC00&labelColor=EEE&color=FFCC00" alt="docker-image-size">
66
<img src="https://img.shields.io/github/actions/workflow/status/digitalghost-dev/poke-cli/ci.yml?branch=main&style=flat-square&logo=github&logoColor=FFCC00&label=CI&labelColor=EEE&color=FFCC00" alt="ci-status-badge">
77
</div>
88
<div align="center">
@@ -68,7 +68,7 @@ _Use a Docker Image_
6868
* Necessary.
6969

7070
```bash
71-
docker run --rm -i -t digitalghostdev/poke-cli:v0.10.0 <command> [subcommand] flag]
71+
docker run --rm -i -t digitalghostdev/poke-cli:v0.11.0 <command> [subcommand] flag]
7272
```
7373

7474
### Go Install

cli_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -170,7 +170,7 @@ func TestRunCLI(t *testing.T) {
170170
{
171171
name: "Latest Flag",
172172
args: []string{"-l"},
173-
expectedOutput: "Latest Docker image version: v0.9.3\nLatest release tag: v0.9.3\n",
173+
expectedOutput: "Latest Docker image version: v0.10.0\nLatest release tag: v0.10.0\n",
174174
expectedCode: 0,
175175
},
176176
}

cmd/pokemon.go

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ import (
1515
// PokemonCommand processes the Pokémon command
1616
func PokemonCommand() {
1717

18+
hintMessage := styleItalic.Render("options: [sm, md, lg]")
19+
1820
flag.Usage = func() {
1921
helpMessage := helpBorder.Render(
2022
"Get details about a specific Pokémon.\n\n",
@@ -24,6 +26,8 @@ func PokemonCommand() {
2426
"\n\n",
2527
styleBold.Render("FLAGS:"),
2628
fmt.Sprintf("\n\t%-30s %s", "-a, --abilities", "Prints the Pokémon's abilities."),
29+
fmt.Sprintf("\n\t%-30s %s", "-i=xx, --image=xx", "Prints out the Pokémon's default sprite."),
30+
fmt.Sprintf("\n\t%5s%-15s", "", hintMessage),
2731
fmt.Sprintf("\n\t%-30s %s", "-s, --stats", "Prints the Pokémon's base stats."),
2832
fmt.Sprintf("\n\t%-30s %s", "-t, --types", "Prints the Pokémon's typing."),
2933
fmt.Sprintf("\n\t%-30s %s", "-h, --help", "Prints the help menu."),
@@ -33,7 +37,7 @@ func PokemonCommand() {
3337

3438
flag.Parse()
3539

36-
pokeFlags, abilitiesFlag, shortAbilitiesFlag, statsFlag, shortStatsFlag, typesFlag, shortTypesFlag := flags.SetupPokemonFlagSet()
40+
pokeFlags, abilitiesFlag, shortAbilitiesFlag, imageFlag, shortImageFlag, statsFlag, shortStatsFlag, typesFlag, shortTypesFlag := flags.SetupPokemonFlagSet()
3741

3842
args := os.Args
3943

@@ -76,6 +80,20 @@ func PokemonCommand() {
7680
capitalizedString, pokemonID, weightKilograms, weightPounds, heightFeet, feet, inches,
7781
)
7882

83+
if *imageFlag != "" || *shortImageFlag != "" {
84+
// Determine the size based on the provided flags
85+
size := *imageFlag
86+
if *shortImageFlag != "" {
87+
size = *shortImageFlag
88+
}
89+
90+
// Call the ImageFlag function with the specified size
91+
if err := flags.ImageFlag(endpoint, pokemonName, size); err != nil {
92+
fmt.Println(err)
93+
os.Exit(1)
94+
}
95+
}
96+
7997
if *abilitiesFlag || *shortAbilitiesFlag {
8098
if err := flags.AbilitiesFlag(endpoint, pokemonName); err != nil {
8199
fmt.Printf("Error: %s\n", err)

cmd/pokemon_test.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ func TestValidatePokemonArgs_ValidInput(t *testing.T) {
1414
{"poke-cli", "pokemon", "cloyster", "--abilities"},
1515
{"poke-cli", "pokemon", "mewtwo", "--types", "--abilities"},
1616
{"poke-cli", "pokemon", "BlaZiKen", "-a", "-t"},
17+
{"poke-cli", "pokemon", "gengar", "--image=md"},
1718
}
1819

1920
for _, input := range validInputs {

cmd/validateargs.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ func ValidatePokemonArgs(args []string) error {
5454
}
5555

5656
// Check if there are too many arguments
57-
if len(args) > 6 {
57+
if len(args) > 7 {
5858
errMessage := errorBorder.Render(
5959
errorColor.Render("Error!"),
6060
"\nToo many arguments",

connections/connection.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,9 @@ type PokemonJSONStruct struct {
3636
URL string `json:"url"`
3737
} `json:"type"`
3838
} `json:"types"`
39+
Sprites struct {
40+
FrontDefault string `json:"front_default"`
41+
} `json:"sprites"`
3942
Stats []struct {
4043
BaseStat int `json:"base_stat"`
4144
Stat struct {

flags/pokemonflagset.go

Lines changed: 100 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,16 +7,26 @@ import (
77
"fmt"
88
"github.com/charmbracelet/lipgloss"
99
"github.com/digitalghost-dev/poke-cli/connections"
10+
"github.com/disintegration/imaging"
11+
"github.com/lucasb-eyer/go-colorful"
1012
"golang.org/x/text/cases"
1113
"golang.org/x/text/language"
14+
"image"
15+
"net/http"
16+
"os"
1217
"strings"
1318
)
1419

1520
var (
1621
helpBorder = lipgloss.NewStyle().
1722
BorderStyle(lipgloss.RoundedBorder()).
1823
BorderForeground(lipgloss.Color("#FFCC00"))
19-
styleBold = lipgloss.NewStyle().Bold(true)
24+
styleBold = lipgloss.NewStyle().Bold(true)
25+
errorColor = lipgloss.NewStyle().Foreground(lipgloss.Color("#F2055C"))
26+
errorBorder = lipgloss.NewStyle().
27+
BorderStyle(lipgloss.RoundedBorder()).
28+
BorderForeground(lipgloss.Color("#F2055C"))
29+
styleItalic = lipgloss.NewStyle().Italic(true)
2030
)
2131

2232
func header(header string) {
@@ -30,28 +40,37 @@ func header(header string) {
3040
fmt.Println(HeaderBold)
3141
}
3242

33-
func SetupPokemonFlagSet() (*flag.FlagSet, *bool, *bool, *bool, *bool, *bool, *bool) {
43+
func SetupPokemonFlagSet() (*flag.FlagSet, *bool, *bool, *string, *string, *bool, *bool, *bool, *bool) {
3444
pokeFlags := flag.NewFlagSet("pokeFlags", flag.ExitOnError)
3545

3646
abilitiesFlag := pokeFlags.Bool("abilities", false, "Print the Pokémon's abilities")
3747
shortAbilitiesFlag := pokeFlags.Bool("a", false, "Print the Pokémon's abilities")
3848

49+
imageFlag := pokeFlags.String("image", "", "Print the Pokémon's default sprite")
50+
shortImageFlag := pokeFlags.String("i", "", "Print the Pokémon's default sprite")
51+
3952
statsFlag := pokeFlags.Bool("stats", false, "Print the Pokémon's base stats")
4053
shortStatsFlag := pokeFlags.Bool("s", false, "Print the Pokémon's base stats")
4154

4255
typesFlag := pokeFlags.Bool("types", false, "Print the Pokémon's typing")
4356
shortTypesFlag := pokeFlags.Bool("t", false, "Prints the Pokémon's typing")
4457

58+
hintMessage := styleItalic.Render("options: [sm, md, lg]")
59+
4560
pokeFlags.Usage = func() {
46-
fmt.Println(
47-
helpBorder.Render("poke-cli pokemon <pokemon-name> [flags]",
48-
styleBold.Render("\n\nFLAGS:"), "\n\t", "-a, --abilities", "\t", "Prints out the Pokémon's abilities.",
49-
"\n\t", "-t, --types", "\t\t", "Prints out the Pokémon's typing.", "\n\t", "-s, --stats", "\t\t",
50-
"Prints out the Pokémon's base stats."),
61+
helpMessage := helpBorder.Render("poke-cli pokemon <pokemon-name> [flags]\n\n",
62+
styleBold.Render("FLAGS:"),
63+
fmt.Sprintf("\n\t%-30s %s", "-a, --abilities", "Prints the Pokémon's abilities."),
64+
fmt.Sprintf("\n\t%-30s %s", "-i=xx, --image=xx", "Prints out the Pokémon's default sprite."),
65+
fmt.Sprintf("\n\t%5s%-15s", "", hintMessage),
66+
fmt.Sprintf("\n\t%-30s %s", "-s, --stats", "Prints the Pokémon's base stats."),
67+
fmt.Sprintf("\n\t%-30s %s", "-t, --types", "Prints the Pokémon's typing."),
68+
fmt.Sprintf("\n\t%-30s %s", "-h, --help", "Prints the help menu."),
5169
)
70+
fmt.Println(helpMessage)
5271
}
5372

54-
return pokeFlags, abilitiesFlag, shortAbilitiesFlag, statsFlag, shortStatsFlag, typesFlag, shortTypesFlag
73+
return pokeFlags, abilitiesFlag, shortAbilitiesFlag, imageFlag, shortImageFlag, statsFlag, shortStatsFlag, typesFlag, shortTypesFlag
5574
}
5675

5776
func AbilitiesFlag(endpoint string, pokemonName string) error {
@@ -98,13 +117,85 @@ func AbilitiesFlag(endpoint string, pokemonName string) error {
98117
return nil
99118
}
100119

120+
func ImageFlag(endpoint string, pokemonName string, size string) error {
121+
baseURL := "https://pokeapi.co/api/v2/"
122+
pokemonStruct, _, _, _, _ := connections.PokemonApiCall(endpoint, pokemonName, baseURL)
123+
124+
header("Image")
125+
126+
// Anonymous function to transform the image to a string
127+
// ToString generates an ASCII representation of the image with color
128+
ToString := func(width int, height int, img image.Image) string {
129+
// Resize the image to the specified width, preserving aspect ratio
130+
img = imaging.Resize(img, width, height, imaging.NearestNeighbor)
131+
b := img.Bounds()
132+
imageWidth := b.Max.X - 2 // Adjust width to exclude margins
133+
h := b.Max.Y - 4 // Adjust height to exclude margins
134+
str := strings.Builder{}
135+
136+
// Loop through the image pixels, two rows at a time
137+
for heightCounter := 2; heightCounter < h; heightCounter += 2 {
138+
for x := 1; x < imageWidth; x++ {
139+
// Get the color of the current and next row's pixels
140+
c1, _ := colorful.MakeColor(img.At(x, heightCounter))
141+
color1 := lipgloss.Color(c1.Hex())
142+
c2, _ := colorful.MakeColor(img.At(x, heightCounter+1))
143+
color2 := lipgloss.Color(c2.Hex())
144+
145+
// Render the half-block character with the two colors
146+
str.WriteString(lipgloss.NewStyle().
147+
Foreground(color1).
148+
Background(color2).
149+
Render("▀"))
150+
}
151+
152+
// Add a newline after each row
153+
str.WriteString("\n")
154+
}
155+
156+
return str.String()
157+
}
158+
159+
imageResp, err := http.Get(pokemonStruct.Sprites.FrontDefault)
160+
if err != nil {
161+
fmt.Println("Error downloading sprite image:", err)
162+
os.Exit(1)
163+
}
164+
defer imageResp.Body.Close()
165+
166+
img, err := imaging.Decode(imageResp.Body)
167+
if err != nil {
168+
fmt.Println("Error decoding image:", err)
169+
os.Exit(1)
170+
}
171+
172+
// Define size map
173+
sizeMap := map[string][2]int{
174+
"lg": {120, 120},
175+
"md": {90, 90},
176+
"sm": {55, 55},
177+
}
178+
179+
// Validate size
180+
dimensions, exists := sizeMap[strings.ToLower(size)]
181+
if !exists {
182+
errMessage := errorBorder.Render(errorColor.Render("Error!"), "\nInvalid image size.\nValid sizes are: lg, md, sm")
183+
return fmt.Errorf("%s", errMessage)
184+
}
185+
186+
imgStr := ToString(dimensions[0], dimensions[1], img)
187+
fmt.Println(imgStr)
188+
189+
return nil
190+
}
191+
101192
func StatsFlag(endpoint string, pokemonName string) error {
102193
baseURL := "https://pokeapi.co/api/v2/"
103194
pokemonStruct, _, _, _, _ := connections.PokemonApiCall(endpoint, pokemonName, baseURL)
104195

105196
header("Base Stats")
106197

107-
// Helper function to map stat values to categories
198+
// Anonymous function to map stat values to categories
108199
getStatCategory := func(value int) string {
109200
switch {
110201
case value < 20:

0 commit comments

Comments
 (0)