diff --git a/Cargo.toml b/Cargo.toml index 92ba5b1..e2382f8 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,7 +1,4 @@ [package] name = "ponyfetch" version = "0.1.0" -edition = "2021" - -[dependencies] -clap = { version = "4.0.29", features = ["derive"] } \ No newline at end of file +edition = "2021" \ No newline at end of file diff --git a/src/args.rs b/src/args.rs deleted file mode 100644 index c4335a7..0000000 --- a/src/args.rs +++ /dev/null @@ -1,15 +0,0 @@ -#[derive(clap::Parser)] -#[clap( - version = "1.0", - author = "Atsukoro1", - about = "Just a simple cross-platform neofetch for all the bronies out there." -)] -pub struct Args { - #[clap(short, long, required = false, default_value = "rainbowdash")] - /// Defines what ASCII pony to print. - pub pony: String, - - #[clap(short, long, required = false, default_value = "green")] - /// Defines what color to print the output it - pub color: String -} \ No newline at end of file diff --git a/src/helpers/arguments.rs b/src/helpers/arguments.rs new file mode 100644 index 0000000..b52567e --- /dev/null +++ b/src/helpers/arguments.rs @@ -0,0 +1,106 @@ +use crate::helpers::colors::COLORS; + +#[derive(Debug)] +pub struct Arguments { + pub help: bool, + pub color: String, + pub pony: String +} + +impl Arguments { + fn validate_color(color: String) -> String { + for c in COLORS.iter() { + if c.0 == color { + return color.to_string(); + } + }; + + Self::print_err("Invalid color provided."); + std::process::exit(1); + } + + fn validate_pony(pony: String) -> String { + let ponies = crate::helpers::paths::get_ponies(); + + for p in ponies.iter() { + if p == &pony { + return pony.to_string(); + } + }; + + Self::print_err("Invalid pony provided."); + std::process::exit(1); + } + + fn print_help() { + println!("Usage: ponyfetch [OPTION]..."); + println!("Prints a pony with system information."); + println!(" + -h, --help Display this help and exit + -c, --color Set the color of the pony + -p, --pony Set the pony to display + "); + + std::process::exit(0); + } + + fn print_err(err: &str) { + println!("Error: {}", err); + println!("Usage: ponyfetch [OPTION]..."); + println!("Try 'ponyfetch --help' for more information."); + std::process::exit(1); + } + + fn get_args(arg: String) -> String { + let args = arg.split("=").collect::>(); + + if args.len() < 2 { + Self::print_err("Invalid argument provided."); + }; + + args[1].to_string() + } + + pub fn parse() -> Arguments { + let mut args = Arguments { + help: false, + color: String::from(""), + pony: String::from("") + }; + + let args_vec: Vec = std::env::args().collect(); + + args_vec.into_iter().for_each(|arg| { + match arg { + arg if arg == "--help" || arg == "-h" => args.help = true, + + arg if arg.contains("--color") || arg.contains("-c") => { + args.color = Self::validate_color( + Self::get_args(arg) + ); + }, + + arg if arg.contains("--pony") || arg.contains("-p") => { + args.pony = Self::validate_pony( + Self::get_args(arg) + ); + }, + _ => () + } + }); + + if args.color == "" { + args.color = String::from("blue"); + } + + if args.pony == "" { + args.pony = String::from("rainbowdash"); + } + + if args.help { + Self::print_help(); + } + + args + } +} \ No newline at end of file diff --git a/src/helpers/colors.rs b/src/helpers/colors.rs index 7a425a8..2e23a58 100644 --- a/src/helpers/colors.rs +++ b/src/helpers/colors.rs @@ -1,4 +1,4 @@ -const COLORS : [(&str, &str); 34] = [ +pub const COLORS : [(&str, &str); 34] = [ ("black", "\u{001b}[30m"), ("red", "\u{001b}[31m"), ("green", "\u{001b}[32m"), diff --git a/src/helpers/mod.rs b/src/helpers/mod.rs index 669692b..9548138 100644 --- a/src/helpers/mod.rs +++ b/src/helpers/mod.rs @@ -1,3 +1,4 @@ +pub mod arguments; pub mod file; pub mod colors; pub mod print; diff --git a/src/helpers/paths.rs b/src/helpers/paths.rs index 8249f6f..1b57440 100644 --- a/src/helpers/paths.rs +++ b/src/helpers/paths.rs @@ -1,5 +1,19 @@ use std::path::PathBuf; +pub fn get_ponies() -> Vec { + let mut ponies: Vec = Vec::new(); + let pony_files = std::fs::read_dir(get_pony_path()).unwrap(); + + pony_files.for_each(|file| { + let file = file.unwrap(); + let file_name = file.file_name().into_string().unwrap(); + let file_name = file_name.split(".").collect::>()[0].to_string(); + ponies.push(file_name); + }); + + ponies +} + #[cfg(target_os = "linux")] pub fn get_pony_path() -> String { if std::env::current_exe().unwrap().to_str().unwrap().contains("target") { diff --git a/src/main.rs b/src/main.rs index d8cc225..79999e5 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,9 +1,7 @@ -use clap::Parser; -use args::Args; +use helpers::arguments::Arguments; mod helpers; mod system; -mod args; #[derive(Clone, Copy, Debug)] pub enum ActionType { @@ -110,7 +108,8 @@ const ACTIONS: [Action; 13] = [ ]; fn main() { - let args: Args = Args::parse(); + let args = Arguments::parse(); + println!("{:?}", args); let line_count = helpers::file::get_file_linecount( &format!("{}{}.txt", helpers::paths::get_pony_path(), &args.pony)