Creating a Linux Terminal Pokedex
Tags: pokemon, linux, bash
Published: 2024-01-09 16:41:48

---
title: Creating a Linux Terminal Pokedex
published: true
description: Simple bash script for a Pokedex
tags: pokemon, linux, bash
cover_image: http://img-cdn.jg.jugem.jp/300/1162528/20110115_1946573.jpg
# Use a ratio of 100:42 for best results.
# published_at: 2023-12-15 10:22 +0000
---

I recently found an [API for Pokemon](https://pokeapi.co/) and I wrote a simple Pokedex for fun using the data available.

The api is pretty straightforward, you give it the ID or name of a Pokemon, it give you back information in a `json` file. I used `bash` and `jq` to parse the information.

```
curl -s https://pokeapi.co/api/v2/pokemon/<my pokemon>
```

So, imagine we want to get the type of the Pokemon. Taking a quick look at the `json`, I check were the type is and then write:

```
strings $mipoke.json | jq -r '.types[].type.name'
```

While `$mypoke` is the Pokemon name. SOme of them have two types so I save them in variables and check the existence of the second one:

```
first_type=$(strings $mipoke.json | jq -r '.types[0].type.name')
second_type=$(strings $mipoke.json | jq -r '.types[1].type.name')


if  [ -z "$second_type" ]; then
      echo -e "$second_type"
fi
```

I though it would be fun to set colors according to the type, so created a palette in the header:

```
export COLOR_NC='\e[0m' # No Color

export COLOR_BLACK='\e[0;30m'
export COLOR_GRAY='\e[1;30m'
export COLOR_RED='\e[0;31m'
export COLOR_LIGHT_RED='\e[1;31m'
export COLOR_GREEN='\e[0;32m'
export COLOR_LIGHT_GREEN='\e[1;32m'
export COLOR_BROWN='\e[0;33m'
export COLOR_YELLOW='\e[1;33m'
export COLOR_BLUE='\e[0;34m'
export COLOR_LIGHT_BLUE='\e[1;34m'
export COLOR_PURPLE='\e[0;35m'
export COLOR_LIGHT_PURPLE='\e[1;35m'
export COLOR_CYAN='\e[0;36m'
export COLOR_LIGHT_CYAN='\e[1;36m'
export COLOR_LIGHT_GRAY='\e[0;37m'
export COLOR_WHITE='\e[1;37m'
```

And then I created a basic conditional to set the color for the types:

```
# initializing color variables
color1="${COLOR_WHITE}"
color2="${COLOR_WHITE}"
...
#creating a function so I don't 
#commit redundancy in both types
check_colors() {
tipo=$1
color="${COLOR_WHITE}"

if [[ "$tipo" = "fairy" || "$tipo" = "psychic" ]]; then
        color="${COLOR_LIGHT_PURPLE}"
elif [[ "$tipo" = "steel" || "$tipo" = "normal" ]]; then
        color="${COLOR_LIGHT_GRAY}"
elif [[ "$tipo" = "grass" || "$tipo" = "bug" ]]; then
        color="${COLOR_GREEN}"      
elif [[ "$tipo" = "water" || "$tipo" = "ice" ]]; then
        color="${COLOR_BLUE}"
elif [ "$tipo" = "poison" ]; then                           
        color="${COLOR_PURPLE}"
elif [ "$tipo" = "electric" ]; then
        color="${COLOR_YELLOW}"

fi

echo "$color"
}
...
#setting the color for the first type
color1="$(check_colors $first_type)"

#if second type exists, setting the color, too
if  [ -z "$second_type" ]; then
      color2="$(check_colors $second_type)"
      echo -e "$color2 $second_type ${COLOR_NC}"
fi

```

Now the next two interesting things might be moves and abilities.I realized in a quick test that moves are too many, so I opted for showing a small sample and saving all the rest in a different file:

```
strings $mipoke.json | jq -r '.moves[].move.name' | head -n5
echo "...\nCheck the whole list in the file $mipoke.moves"
strings $mipoke.json | jq -r '.moves[].move.name' >> $mipoke.moves
```

Abilities aren't usually too many, so I kept them simple:

```
strings $mipoke.json | jq -r '.abilities[].ability.name'
```
I decided I wanted to do a charming detail so I captured the weight:

```
weight=$(strings $mipoke.json | jq -r '.weight')
```
Converted it to kg (it's in lbs):

```
inkg=$(bc <<< "scale=2; $weight*0.45")
```
And then use it in a conditional so to give advice on how to handle the Pokemon:

```
inkg_int=$(bc <<< "$inkg/1")
inkg_int=$(($inkg_int))

if [[ "$inkg_int" -lt 35 && "$inkg_int" -gt 10 ]]; then
        consejo="That means you can carry it for a while, but you can hurt you back! let them walk"
elif [[ "$inkg_int" -lt 11  && "$inkg_int" -gt 5 ]]; then
        consejo="That means is your pokemon is quite light and you might be able to carry it around a lot."
elif [ "$inkg_int" -lt 6 ]; then
        consejo="That means your pokemon is very light, tiny cute baby, and you might even be able to carry it around in a bag, if it pleases them."
elif [[ "$inkg_int" -lt 60  && "$inkg_int" -gt 34 ]]; then
        consejo="Your pokemon weights like a human teen or an adult even, so try to look for ways of taking care of them without carrying it to much unless you want to hurt your back!"
elif [[ "$inkg_int" -lt 200  && "$inkg_int" -gt 59 ]]; then
        consejo="Your pokemon might be a little bit too heavy for carrying it, so don't attempt to do it. Nevertheless look for alternatives such as patting and saying nice words."
else
        consejo="Your pokemon is a thicc baby, they won't expect you to carry it. But they will probably like pats and nice words."
fi

echo "$consejo"
```

It was pretty much done. I added some visual details (further colors, sparkles) and the possibility of tooting in your mastodon the final information. For that I duplicated every print into a file and then:

```
read -p"Do you want to toot it? y/n: " ANS

if [[ "$ANS" = "y" || "$ANS" = "Y" ]]; then
        toot post "$(strings mytoot)"
else
        echo "okay, not tooting!"
fi

```

And that's it!

![capture of the pokedex](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/8by6pirw4u2nzb0wj5yn.png)
![capture of the pokedex](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/5ffgojmclt9tnkvajv1x.png)

Here's the [repo](https://git.sr.ht/~alienagain/terminal_simple_pokedex).