diff --git a/Cargo.lock b/Cargo.lock index b2b05e1..dd84c79 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2,12 +2,53 @@ # It is not intended for manual editing. version = 3 +[[package]] +name = "ansi_term" +version = "0.12.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d52a9bb7ec0cf484c551830a7ce27bd20d67eac647e1befb56b0be4ee39a55d2" +dependencies = [ + "winapi", +] + +[[package]] +name = "atty" +version = "0.2.14" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d9b39be18770d11421cdb1b9947a45dd3f37e93092cbf377614828a319d5fee8" +dependencies = [ + "hermit-abi", + "libc", + "winapi", +] + +[[package]] +name = "bitflags" +version = "1.3.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bef38d45163c2f1dde094a7dfd33ccf595c92905c8f8f4fdc18d06fb1037718a" + [[package]] name = "cfg-if" version = "1.0.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd" +[[package]] +name = "clap" +version = "2.34.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a0610544180c38b88101fecf2dd634b174a62eef6946f84dfc6a7127512b381c" +dependencies = [ + "ansi_term", + "atty", + "bitflags", + "strsim", + "textwrap", + "unicode-width", + "vec_map", +] + [[package]] name = "getrandom" version = "0.2.12" @@ -19,6 +60,15 @@ dependencies = [ "wasi", ] +[[package]] +name = "hermit-abi" +version = "0.1.19" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "62b467343b94ba476dcb2500d242dadbb39557df889310ac77c5d99100aaac33" +dependencies = [ + "libc", +] + [[package]] name = "itoa" version = "1.0.10" @@ -41,6 +91,7 @@ checksum = "3fdb12b2476b595f9358c5161aa467c2438859caa136dec86c26fdd2efe17b92" name = "pokerust" version = "0.1.0" dependencies = [ + "clap", "once_cell", "rand", "serde", @@ -138,6 +189,12 @@ dependencies = [ "serde", ] +[[package]] +name = "strsim" +version = "0.8.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8ea5119cdb4c55b55d432abb513a0429384878c15dde60cc77b1c99de1a95a6a" + [[package]] name = "syn" version = "2.0.55" @@ -149,14 +206,57 @@ dependencies = [ "unicode-ident", ] +[[package]] +name = "textwrap" +version = "0.11.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d326610f408c7a4eb6f51c37c330e496b08506c9457c9d34287ecc38809fb060" +dependencies = [ + "unicode-width", +] + [[package]] name = "unicode-ident" version = "1.0.12" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "3354b9ac3fae1ff6755cb6db53683adb661634f67557942dea4facebec0fee4b" +[[package]] +name = "unicode-width" +version = "0.1.11" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e51733f11c9c4f72aa0c160008246859e340b00807569a0da0e7a1079b27ba85" + +[[package]] +name = "vec_map" +version = "0.8.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f1bddf1187be692e79c5ffeab891132dfb0f236ed36a43c7ed39f1165ee20191" + [[package]] name = "wasi" version = "0.11.0+wasi-snapshot-preview1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "9c8d87e72b64a3b4db28d11ce29237c246188f4f51057d65a7eab63b7987e423" + +[[package]] +name = "winapi" +version = "0.3.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5c839a674fcd7a98952e593242ea400abe93992746761e38641405d28b00f419" +dependencies = [ + "winapi-i686-pc-windows-gnu", + "winapi-x86_64-pc-windows-gnu", +] + +[[package]] +name = "winapi-i686-pc-windows-gnu" +version = "0.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ac3b87c63620426dd9b991e5ce0329eff545bccbbb34f3be09ff6fb6ab51b7b6" + +[[package]] +name = "winapi-x86_64-pc-windows-gnu" +version = "0.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "712e227841d057c1ee1cd2fb22fa7e5a5461ae8e48fa2ca79ec42cfc1931183f" diff --git a/Cargo.toml b/Cargo.toml index e394dee..d71f169 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -10,3 +10,4 @@ rand = "0.8" serde = { version = "1.0", features = ["derive"] } serde_json = "1.0" once_cell = "1.8.0" +clap = { version = "2" } diff --git a/src/main.rs b/src/main.rs index 0984506..a25e304 100644 --- a/src/main.rs +++ b/src/main.rs @@ -2,9 +2,6 @@ use rand::Rng; use std::io::prelude::*; // set global constants -const PROGRAM: once_cell::sync::Lazy = - once_cell::sync::Lazy::new(|| std::env::current_exe().unwrap()); - const PROGRAM_DIR: once_cell::sync::Lazy = once_cell::sync::Lazy::new(|| { std::path::PathBuf::from(std::env::current_exe().unwrap().parent().unwrap()) }); @@ -12,6 +9,9 @@ const PROGRAM_DIR: once_cell::sync::Lazy = once_cell::sync:: const COLORSCRIPTS_DIR: once_cell::sync::Lazy = once_cell::sync::Lazy::new(|| PROGRAM_DIR.join("colorscripts")); +const POKEMON_DATA_PATH: once_cell::sync::Lazy = + once_cell::sync::Lazy::new(|| PROGRAM_DIR.join("pokemon.json")); + const REGULAR_SUBDIR: &str = "regular"; const SHINY_SUBDIR: &str = "shiny"; @@ -42,8 +42,8 @@ fn print_file(filepath: &std::path::Path) -> std::io::Result<()> { Ok(()) } -fn list_pokemon_names(filepath: &std::path::Path) -> std::io::Result<()> { - let file = std::fs::File::open(filepath)?; +fn list_pokemon_names() -> std::io::Result<()> { + let file = std::fs::File::open(POKEMON_DATA_PATH.as_path())?; let reader = std::io::BufReader::new(file); let pokemon_json: serde_json::Value = serde_json::from_reader(reader)?; @@ -77,7 +77,7 @@ fn show_pokemon_by_name( let color_subdir = if shiny { SHINY_SUBDIR } else { REGULAR_SUBDIR }; let size_subdir = if is_large { LARGE_SUBDIR } else { SMALL_SUBDIR }; - let file = std::fs::File::open(PROGRAM_DIR.join("pokemon.json"))?; + let file = std::fs::File::open(POKEMON_DATA_PATH.as_path())?; let reader = std::io::BufReader::new(file); let pokemon_json: serde_json::Value = serde_json::from_reader(reader)?; @@ -145,20 +145,20 @@ fn show_random_pokemon( ) -> std::io::Result<()> { let mut rng = rand::thread_rng(); - let (start_gen, end_gen) = if generations.is_empty() { - ("1", "8") + let start_gen = if generations.is_empty() { + "1" } else if generations.contains(",") { let gens: Vec<&str> = generations.split(",").collect(); let gen = gens[rng.gen_range(0..gens.len())]; - (gen, gen) + gen } else if generations.contains("-") { let gens: Vec<&str> = generations.split("-").collect(); - (gens[0], gens[1]) + gens[0] } else { - (generations, generations) + generations }; - let file = std::fs::File::open(PROGRAM_DIR.join("pokemon.json"))?; + let file = std::fs::File::open(POKEMON_DATA_PATH.as_path())?; let reader = std::io::BufReader::new(file); let pokemon_json: serde_json::Value = serde_json::from_reader(reader)?; let pokemon: Vec = pokemon_json @@ -187,10 +187,84 @@ fn show_random_pokemon( Ok(()) } +// fn main() { +// // println!("{}", PROGRAM.display()); +// // println!("{}", PROGRAM_DIR.display()); +// // println!("{}", COLORSCRIPTS_DIR.display()); +// // show_pokemon_by_name("eevee", false, false, false, Some("gmax")).unwrap(); +// show_random_pokemon("7-8", true, false, false); +// list_pokemon_names(); +// } + fn main() { - // println!("{}", PROGRAM.display()); - // println!("{}", PROGRAM_DIR.display()); - // println!("{}", COLORSCRIPTS_DIR.display()); - // show_pokemon_by_name("eevee", false, false, false, Some("gmax")).unwrap(); - show_random_pokemon("1-999", true, false, false); + let matches = clap::App::new("pokemon-colorscripts") + .about("CLI utility to print out unicode image of a pokemon in your shell") + .arg( + clap::Arg::with_name("list") + .short("l") + .long("list") + .help("Print list of all pokemon"), + ) + .arg( + clap::Arg::with_name("name") + .short("n") + .long("name") + .value_name("POKEMON NAME") + .help("Select pokemon by name. Generally spelled like in the games."), + ) + .arg( + clap::Arg::with_name("form") + .short("f") + .long("form") + .value_name("FORM") + .help("Show an alternate form of a pokemon"), + ) + .arg( + clap::Arg::with_name("no-title") + .long("no-title") + .help("Do not display pokemon name"), + ) + .arg( + clap::Arg::with_name("shiny") + .short("s") + .long("shiny") + .help("Show the shiny version of the pokemon instead"), + ) + .arg( + clap::Arg::with_name("big") + .short("b") + .long("big") + .help("Show a larger version of the sprite"), + ) + .arg( + clap::Arg::with_name("random") + .short("r") + .long("random") + .value_name("GENERATION") + .help("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)"), + ) + .get_matches(); + + if matches.is_present("list") { + list_pokemon_names().unwrap(); + } else if matches.is_present("name") { + let name = matches.value_of("name").unwrap(); + let no_title = matches.is_present("no-title"); + let shiny = matches.is_present("shiny"); + let big = matches.is_present("big"); + let form = matches.value_of("form"); + show_pokemon_by_name(name, no_title, shiny, big, form).unwrap(); + } else if matches.is_present("random") { + let random = matches.value_of("random").unwrap_or(""); + let no_title = matches.is_present("no-title"); + let shiny = matches.is_present("shiny"); + let big = matches.is_present("big"); + if matches.is_present("form") { + println!("--form flag unexpected with --random"); + std::process::exit(1); + } + show_random_pokemon(random, no_title, shiny, big).unwrap(); + } else { + show_random_pokemon("", true, false, false).unwrap(); + } }