embedded files
This commit is contained in:
parent
f19fdadb78
commit
7fe497ebb1
56
Cargo.lock
generated
56
Cargo.lock
generated
|
@ -11,6 +11,12 @@ dependencies = [
|
|||
"winapi",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "anyhow"
|
||||
version = "1.0.81"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "0952808a6c2afd1aa8947271f3a60f1a6763c7b912d210184c5149b5cf147247"
|
||||
|
||||
[[package]]
|
||||
name = "atty"
|
||||
version = "0.2.14"
|
||||
|
@ -60,6 +66,12 @@ dependencies = [
|
|||
"wasi",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "glob"
|
||||
version = "0.3.1"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "d2fabcfbdc87f4758337ca535fb41a6d701b65693ce38287d856d1674551ec9b"
|
||||
|
||||
[[package]]
|
||||
name = "hermit-abi"
|
||||
version = "0.1.19"
|
||||
|
@ -69,6 +81,30 @@ dependencies = [
|
|||
"libc",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "include_dir"
|
||||
version = "0.6.2"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "24b56e147e6187d61e9d0f039f10e070d0c0a887e24fe0bb9ca3f29bfde62cab"
|
||||
dependencies = [
|
||||
"glob",
|
||||
"include_dir_impl",
|
||||
"proc-macro-hack",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "include_dir_impl"
|
||||
version = "0.6.2"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "0a0c890c85da4bab7bce4204c707396bbd3c6c8a681716a51c8814cfc2b682df"
|
||||
dependencies = [
|
||||
"anyhow",
|
||||
"proc-macro-hack",
|
||||
"proc-macro2",
|
||||
"quote",
|
||||
"syn 1.0.109",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "itoa"
|
||||
version = "1.0.10"
|
||||
|
@ -92,6 +128,7 @@ name = "pokerust"
|
|||
version = "0.1.0"
|
||||
dependencies = [
|
||||
"clap",
|
||||
"include_dir",
|
||||
"once_cell",
|
||||
"rand",
|
||||
"serde",
|
||||
|
@ -104,6 +141,12 @@ version = "0.2.17"
|
|||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "5b40af805b3121feab8a3c29f04d8ad262fa8e0561883e7653e024ae4479e6de"
|
||||
|
||||
[[package]]
|
||||
name = "proc-macro-hack"
|
||||
version = "0.5.20+deprecated"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "dc375e1527247fe1a97d8b7156678dfe7c1af2fc075c9a4db3690ecd2a148068"
|
||||
|
||||
[[package]]
|
||||
name = "proc-macro2"
|
||||
version = "1.0.79"
|
||||
|
@ -175,7 +218,7 @@ checksum = "7eb0b34b42edc17f6b7cac84a52a1c5f0e1bb2227e997ca9011ea3dd34e8610b"
|
|||
dependencies = [
|
||||
"proc-macro2",
|
||||
"quote",
|
||||
"syn",
|
||||
"syn 2.0.55",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
|
@ -195,6 +238,17 @@ version = "0.8.0"
|
|||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "8ea5119cdb4c55b55d432abb513a0429384878c15dde60cc77b1c99de1a95a6a"
|
||||
|
||||
[[package]]
|
||||
name = "syn"
|
||||
version = "1.0.109"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "72b64191b275b66ffe2469e8af2c1cfe3bafa67b529ead792a6d0160888b4237"
|
||||
dependencies = [
|
||||
"proc-macro2",
|
||||
"quote",
|
||||
"unicode-ident",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "syn"
|
||||
version = "2.0.55"
|
||||
|
|
|
@ -6,8 +6,10 @@ edition = "2021"
|
|||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||
|
||||
[dependencies]
|
||||
|
||||
rand = "0.8"
|
||||
serde = { version = "1.0", features = ["derive"] }
|
||||
serde_json = "1.0"
|
||||
once_cell = "1.8.0"
|
||||
clap = { version = "2" }
|
||||
include_dir = "0.6.0"
|
||||
|
|
75
src/main.rs
75
src/main.rs
|
@ -1,16 +1,9 @@
|
|||
use rand::Rng;
|
||||
use std::io::prelude::*;
|
||||
|
||||
// set global constants
|
||||
const PROGRAM_DIR: once_cell::sync::Lazy<std::path::PathBuf> = once_cell::sync::Lazy::new(|| {
|
||||
std::path::PathBuf::from(std::env::current_exe().unwrap().parent().unwrap())
|
||||
});
|
||||
const COLORSCRIPTS_DIR: include_dir::Dir = include_dir::include_dir!("./colorscripts");
|
||||
|
||||
const COLORSCRIPTS_DIR: once_cell::sync::Lazy<std::path::PathBuf> =
|
||||
once_cell::sync::Lazy::new(|| PROGRAM_DIR.join("colorscripts"));
|
||||
|
||||
const POKEMON_DATA_PATH: once_cell::sync::Lazy<std::path::PathBuf> =
|
||||
once_cell::sync::Lazy::new(|| PROGRAM_DIR.join("pokemon.json"));
|
||||
const POKEMON_JSON: &str = std::include_str!("../pokemon.json");
|
||||
|
||||
const REGULAR_SUBDIR: &str = "regular";
|
||||
const SHINY_SUBDIR: &str = "shiny";
|
||||
|
@ -31,44 +24,21 @@ const GENERATIONS: [(&str, (u32, u32)); 8] = [
|
|||
("8", (810, 898)),
|
||||
];
|
||||
|
||||
fn validate_files() -> std::io::Result<()> {
|
||||
let colorscripts_dir = COLORSCRIPTS_DIR;
|
||||
let pokemon_data_path = POKEMON_DATA_PATH;
|
||||
|
||||
if !colorscripts_dir.exists() {
|
||||
println!(
|
||||
"Colorscripts directory not found at {}",
|
||||
colorscripts_dir.display()
|
||||
);
|
||||
std::process::exit(1);
|
||||
}
|
||||
|
||||
if !pokemon_data_path.exists() {
|
||||
println!(
|
||||
"Pokemon data file not found at {}",
|
||||
pokemon_data_path.display()
|
||||
);
|
||||
std::process::exit(1);
|
||||
}
|
||||
|
||||
fn print_file(filepath: &str) -> std::io::Result<()> {
|
||||
if let Some(file) = COLORSCRIPTS_DIR.get_file(filepath) {
|
||||
let content = std::str::from_utf8(file.contents()).unwrap();
|
||||
println!("{}", content);
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn print_file(filepath: &std::path::Path) -> std::io::Result<()> {
|
||||
let file = std::fs::File::open(filepath)?;
|
||||
let reader = std::io::BufReader::new(file);
|
||||
|
||||
for line in reader.lines() {
|
||||
println!("{}", line?);
|
||||
} else {
|
||||
Err(std::io::Error::new(
|
||||
std::io::ErrorKind::NotFound,
|
||||
"File not found",
|
||||
))
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
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)?;
|
||||
let pokemon_json: serde_json::Value = serde_json::from_str(POKEMON_JSON)?;
|
||||
|
||||
let mut count = 0;
|
||||
|
||||
|
@ -99,13 +69,10 @@ fn show_pokemon_by_name(
|
|||
form: Option<&str>,
|
||||
) -> std::io::Result<()> {
|
||||
// set variables
|
||||
let base_path = COLORSCRIPTS_DIR;
|
||||
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(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_json: serde_json::Value = serde_json::from_str(POKEMON_JSON)?;
|
||||
|
||||
let pokemon_names: Vec<&str> = pokemon_json
|
||||
.as_array()
|
||||
|
@ -150,8 +117,6 @@ fn show_pokemon_by_name(
|
|||
}
|
||||
}
|
||||
|
||||
let pokemon_file = base_path.join(size_subdir).join(color_subdir).join(&name);
|
||||
|
||||
if show_title {
|
||||
if shiny {
|
||||
println!("{} (shiny)", name);
|
||||
|
@ -160,7 +125,13 @@ fn show_pokemon_by_name(
|
|||
}
|
||||
}
|
||||
|
||||
print_file(&pokemon_file)
|
||||
// Construct the embedded file path
|
||||
let file_path = format!("{}/{}/{}", size_subdir, color_subdir, name);
|
||||
|
||||
// Use the adjusted function to print file contents from embedded resources
|
||||
print_file(&file_path)?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn show_random_pokemon(
|
||||
|
@ -184,9 +155,7 @@ fn show_random_pokemon(
|
|||
generations
|
||||
};
|
||||
|
||||
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_json: serde_json::Value = serde_json::from_str(POKEMON_JSON)?;
|
||||
let pokemon: Vec<String> = pokemon_json
|
||||
.as_array()
|
||||
.unwrap()
|
||||
|
@ -215,7 +184,7 @@ fn show_random_pokemon(
|
|||
|
||||
fn main() {
|
||||
// validate files first
|
||||
validate_files().unwrap();
|
||||
// validate_files().unwrap();
|
||||
|
||||
let matches = clap::App::new("pokemon-colorscripts")
|
||||
.about("CLI utility to print out unicode image of a pokemon in your shell")
|
||||
|
|
Loading…
Reference in a new issue