-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathpokemon-colorscripts.sh
executable file
·185 lines (166 loc) · 5.6 KB
/
pokemon-colorscripts.sh
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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
#!/bin/sh
# Checking the OS so as to use mac specific utilities on MacOS
OS=$(uname)
if [ "$OS" = 'Darwin' ]; then
PROGRAM=$(greadlink -f "$0")
else
PROGRAM=$(readlink -f "$0")
fi
PROGRAM_DIR=$(dirname "$PROGRAM")
# directory where all the art files exist
POKEART_DIR="$PROGRAM_DIR/colorscripts"
POKEART_REGULAR_DIR="$POKEART_DIR/regular"
POKEART_SHINY_DIR="$POKEART_DIR/shiny"
# Rate (1/x) of getting shinies when using the random flag
SHINY_RATE=128
# formatting for the help strings
fmt_help=" %-20s\t%-54s\n"
_help() {
#Function that prints out the help text
echo "Description: CLI utility to print out unicode image of a pokemon in your shell"
echo ""
echo "Usage: pokemon-colorscripts [OPTION] [POKEMON NAME]"
printf "${fmt_help}" \
"-h, --help, help" "Print this help." \
"-l, --list, list" "Print list of all pokemon" \
"-r, --random, random" "Show a random pokemon. This flag can optionally be
followed by a generation number or range (1-8) to show random
pokemon from a specific generation or range of generations.
The generations can be provided as a continuous range (eg. 1-3)
or as a list of generations (1 3 6)" \
"-n, --name" "Select pokemon by name. Generally spelled like in the games.
a few exceptions are nidoran-f,nidoran-m,mr-mime,farfetchd,flabebe
type-null etc. Perhaps grep the output of --list if in
doubt. This flag can optionally be followed by -s or --shiny to print
the shiny version of the pokemon instead"
echo "Examples: pokemon-colorscripts --name pikachu"
echo " pokemon-colorscripts -n mudkip -s"
echo " pokemon-colorscripts -r"
echo " pokemon-colorscripts -r 1-3"
echo " pokemon-colorscripts -r 1 2 6"
}
# Index values where the different generations are seperated in the names list
# Cannot think of a better way to do arrays with narrow POSIX compliance
# 0-151 gen 1, 810-898 gen 8
indices="1 152 251 387 494 650 722 810 898"
_show_random_pokemon() {
#selecting a random art file from the whole set
# if there are no arguments show across all generations
if [ $# = 0 ]; then
start_gen=1
end_gen=8
# show from single generation or continuous range of generations
elif [ $# = 1 ]; then
generation=$1
start_gen=${generation%-*}
end_gen=${generation#*-}
# show from list of generations
else
generations=$@
if [ "$OS" = 'Darwin' ]; then
start_gen=$(gshuf -e $generations -n 1)
else
start_gen=$(shuf -e $generations -n 1)
fi
end_gen=$start_gen
fi
if [ "$end_gen" -gt 8 ] || [ "$start_gen" -lt 1 ]; then
echo "Invalid generation"
exit 1
fi
start_index=$(_get_start_index "$start_gen")
end_index=$(_get_end_index "$end_gen")
# getting a random index (using shuf instead of $RANDOM for POSIX compliance)
# Using mac coreutils if on MacOS
if [ "$OS" = 'Darwin' ]; then
random_index=$(gshuf -i "$start_index"-"$end_index" -n 1)
shiny=$(gshuf -i 1-"$SHINY_RATE" -n 1)
else
random_index=$(shuf -i "$start_index"-"$end_index" -n 1)
shiny=$(shuf -i 1-"$SHINY_RATE" -n 1)
fi
random_pokemon=$(sed "$random_index"'q;d' "$PROGRAM_DIR/nameslist.txt")
# print out the pokemon art for the pokemon
if [ "$shiny" = "$SHINY_RATE" ]; then
echo "$random_pokemon (shiny)"
cat "$POKEART_SHINY_DIR/$random_pokemon.txt"
else
echo "$random_pokemon"
cat "$POKEART_REGULAR_DIR/$random_pokemon.txt"
fi
}
_get_end_index() {
local i=0
local gen="$1"
for index in $indices; do
if [ "$i" = "$gen" ]; then
echo "$index"
break
fi
i=$((i + 1))
done
}
_get_start_index() {
local i=0
local gen="$1"
for index in $indices; do
if [ "$i" = "$((gen - 1))" ]; then
echo "$index"
break
fi
i=$((i + 1))
done
}
_show_pokemon_by_name() {
pokemon_name=$1
echo "$pokemon_name"
# Error handling. Can't think of a better way to do it
cat "$POKEART_REGULAR_DIR/$pokemon_name.txt" 2> /dev/null || echo "Invalid pokemon"
}
_show_shiny_pokemon_by_name() {
echo "$1 (shiny)"
cat "$POKEART_SHINY_DIR/$1.txt" 2> /dev/null || echo "Invalid pokemon"
}
_list_pokemon_names() {
less < "$PROGRAM_DIR/nameslist.txt"
}
# Handling command line arguments
case "$#" in
0)
# display help if no arguments are given
_help
;;
1)
# Check flag and show appropriate output
case $1 in
-h | --help | help)
_help
;;
-r | --random | random)
_show_random_pokemon
;;
-l | --list | list)
_list_pokemon_names
;;
*)
echo "Input error."
exit 1
;;
esac
;;
*)
if [ "$1" = '-n' ] || [ "$1" = '--name' ] || [ "$1" = 'name' ]; then
if [ "$3" = '-s' ] || [ "$3" = '--shiny' ]; then
_show_shiny_pokemon_by_name "$2"
else
_show_pokemon_by_name "$2"
fi
elif [ "$1" = -r ] || [ "$1" = '--random' ] || [ "$1" = 'random' ]; then
shift
_show_random_pokemon "$@"
else
echo "Input error, too many arguments."
exit 1
fi
;;
esac