Skip to content

Commit

Permalink
feat: organize pokemon attributes in it's own struct
Browse files Browse the repository at this point in the history
  • Loading branch information
talwat committed Aug 9, 2024
1 parent 5175f83 commit 0f8cd06
Show file tree
Hide file tree
Showing 4 changed files with 72 additions and 58 deletions.
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
[package]
name = "pokeget"
authors = [ "talwat" ]
version = "1.5.0"
version = "1.5.1"
edition = "2021"
description = "Display pokemon sprites in your terminal."
license = "MIT"
Expand Down
13 changes: 6 additions & 7 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

use clap::Parser;
use pokeget::cli::Args;
use pokeget::pokemon::{get_form, Pokemon};
use pokeget::pokemon::{Attributes, Pokemon};
use pokeget::sprites::combine_sprites;
use std::process::exit;

Expand All @@ -19,17 +19,16 @@ fn main() {
exit(1);
}

let form = get_form(&args);
let pokemons: Vec<Pokemon> = args
.pokemon
.iter()
.map(|x| Pokemon::new(x.to_owned(), &pokemon_list, form.clone(), &args))
let attributes = Attributes::new(&args);
let pokemons: Vec<Pokemon> = args.pokemon
.into_iter()
.map(|x| Pokemon::new(x, &pokemon_list, &attributes))
.collect();

let combined = combine_sprites(&pokemons);

if !args.hide_name {
let names: Vec<String> = pokemons.iter().map(|x| x.name.clone()).collect();
let names: Vec<&str> = pokemons.iter().map(|x| x.name.as_ref()).collect();

eprintln!("{}", names.join(", "));
}
Expand Down
113 changes: 64 additions & 49 deletions src/pokemon.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,51 +13,6 @@ pub fn random(list: &[&str]) -> String {
String::from(list[rand.gen_range(0..list.len())])
}

/// Uses the arguments like gmax, mega, etc... to get a form which is appended to the pokemon filename.
pub fn get_form(args: &Args) -> String {
let mut form = match args {
Args { mega: true, .. } => "mega",
Args { mega_x: true, .. } => "mega-x",
Args { mega_y: true, .. } => "mega-y",
Args { alolan: true, .. } => "alola",
Args { gmax: true, .. } => "gmax",
Args { hisui: true, .. } => "hisui",
Args { galar: true, .. } => "galar",
_ => &args.form,
}
.to_string();

if args.noble {
form.push_str("-noble");
}

form
}

fn format_path(name: &str, form: String, random: bool, shiny: bool, female: bool) -> String {
let mut filename = name.to_owned();

// The form shouldn't be applied to random pokemon.
if !form.is_empty() && !random {
filename.push_str(&format!("-{}", form));
}

// I hate Mr. Mime and Farfetch'd.
filename = filename
.replace([' ', '_'], "-")
.replace(['.', '\'', ':'], "")
.to_lowercase();

let path = format!(
"{}/{}{}.png",
if shiny { "shiny" } else { "regular" },
if female && !random { "female/" } else { "" }, // Random pokemon also shouldn't follow the female rule.
filename.trim()
);

return path;
}

fn format_name(name: String) -> String {
name.replace('-', " ").replace('\'', "").to_title_case()
}
Expand Down Expand Up @@ -89,7 +44,7 @@ impl Selection {
}
}

pub struct Pokemon {
pub struct Pokemon<'a> {
/// The path of the Pokemon in pokesprite.
/// Eg. `regular/abra.png`
pub path: String,
Expand All @@ -104,10 +59,69 @@ pub struct Pokemon {

/// The sprite of the Pokemon, as a [DynamicImage].
pub sprite: DynamicImage,

/// Data, like the form and whether a pokemon is shiny or not.
pub attributes: &'a Attributes
}

pub struct Attributes {
pub form: String,
pub female: bool,
pub shiny: bool,
}

impl Attributes {
pub fn new(args: &Args) -> Self {
let mut form = match args {
Args { mega: true, .. } => "mega",
Args { mega_x: true, .. } => "mega-x",
Args { mega_y: true, .. } => "mega-y",
Args { alolan: true, .. } => "alola",
Args { gmax: true, .. } => "gmax",
Args { hisui: true, .. } => "hisui",
Args { galar: true, .. } => "galar",
_ => &args.form,
}
.to_string();

if args.noble {
form.push_str("-noble");
}

Self {
form,
female: args.female,
shiny: args.shiny
}
}

pub fn path(&self, name: &str, random: bool) -> String {
let mut filename = name.to_owned();

// The form shouldn't be applied to random pokemon.
if !self.form.is_empty() && !random {
filename.push_str(&format!("-{}", self.form));
}

// I hate Mr. Mime and Farfetch'd.
filename = filename
.replace([' ', '_'], "-")
.replace(['.', '\'', ':'], "")
.to_lowercase();

let path = format!(
"{}/{}{}.png",
if self.shiny { "shiny" } else { "regular" },
if self.female && !random { "female/" } else { "" }, // Random pokemon also shouldn't follow the female rule.
filename.trim()
);

path
}
}

impl Pokemon {
pub fn new(id: String, list: &[&'static str], form: String, args: &Args) -> Self {
impl<'a> Pokemon<'a> {
pub fn new(id: String, list: &[&'static str], attributes: &'a Attributes) -> Self {
let mut selection = Selection::parse(id, list);
let is_random = selection == Selection::Random;

Expand All @@ -127,7 +141,7 @@ impl Pokemon {
panic!("selection should have been converted, but wasn't")
};

let path = format_path(&name, form, is_random, args.shiny, args.female);
let path = attributes.path(&name, is_random);
let bytes = Data::get(&path)
.unwrap_or_else(|| {
eprintln!("pokemon not found");
Expand All @@ -143,6 +157,7 @@ impl Pokemon {
path,
name: format_name(name),
sprite: trimmed,
attributes
}
}
}

0 comments on commit 0f8cd06

Please sign in to comment.