From c16a99c456da79d62c04007d735edbd01c2bde44 Mon Sep 17 00:00:00 2001 From: Atsukoro1 Date: Thu, 1 Dec 2022 21:42:13 +0100 Subject: [PATCH] =?UTF-8?q?feat:=20=F0=9F=8E=B8=20Command=20line=20argumen?= =?UTF-8?q?ts?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Cargo.toml | 5 ++++- src/args.rs | 15 +++++++++++++++ src/helpers/colors.rs | 45 ++++++++++++++++++++++++++++--------------- src/helpers/print.rs | 20 +++++++++---------- src/main.rs | 17 ++++++++++++---- 5 files changed, 71 insertions(+), 31 deletions(-) create mode 100644 src/args.rs diff --git a/Cargo.toml b/Cargo.toml index e2382f8..92ba5b1 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,4 +1,7 @@ [package] name = "ponyfetch" version = "0.1.0" -edition = "2021" \ No newline at end of file +edition = "2021" + +[dependencies] +clap = { version = "4.0.29", features = ["derive"] } \ No newline at end of file diff --git a/src/args.rs b/src/args.rs new file mode 100644 index 0000000..9ec967d --- /dev/null +++ b/src/args.rs @@ -0,0 +1,15 @@ +#[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 = "0")] + /// Defines what color to print the output it + pub color: String +} \ No newline at end of file diff --git a/src/helpers/colors.rs b/src/helpers/colors.rs index 6f45bfb..9e65d7a 100644 --- a/src/helpers/colors.rs +++ b/src/helpers/colors.rs @@ -1,23 +1,36 @@ -pub fn print_cyan(text: &str, inline: bool) { - if inline { - print!("\x1b[36m{}\x1b[0m", text); - } else { - println!("\x1b[36m{}\x1b[0m", text); - } -} +// make const with color names and match them with unicode symbol for color +const COLORS : [(&str, &str); 17] = [ + ("black", "\u{001b}[30m"), + ("red", "\u{001b}[31m"), + ("green", "\u{001b}[32m"), + ("yellow", "\u{001b}[33m"), + ("blue", "\u{001b}[34m"), + ("magenta", "\u{001b}[35m"), + ("cyan", "\u{001b}[36m"), + ("white", "\u{001b}[37m"), + ("bright_black", "\u{001b}[90m"), + ("bright_red", "\u{001b}[91m"), + ("bright_green", "\u{001b}[92m"), + ("bright_yellow", "\u{001b}[93m"), + ("bright_blue", "\u{001b}[94m"), + ("bright_magenta", "\u{001b}[95m"), + ("bright_cyan", "\u{001b}[96m"), + ("bright_white", "\u{001b}[97m"), + ("cyan_bold", "\u{001b}[36m\u{001b}[1m"), +]; -pub fn print_cyan_bold(text: &str, inline: bool) { - if inline { - print!("\x1b[36m\x1b[1m{}\x1b[0m", text); - } else { - println!("\x1b[36m\x1b[1m{}\x1b[0m", text); +pub fn print(text: &str, inline: bool, color: &str) { + let mut color = color; + if color == "rainbowdash" { + color = "bright_magenta"; } -} -pub fn print_bold(text: &str, inline: bool) { + let color = color; + let color = COLORS.iter().find(|(name, _)| name == &color).unwrap().1; + if inline { - print!("\x1b[1m{}\x1b[0m", text); + print!("{}{}", color, text); } else { - println!("\x1b[1m{}\x1b[0m", text); + println!("{}{}", color, text); } } \ No newline at end of file diff --git a/src/helpers/print.rs b/src/helpers/print.rs index 08131fc..36c53c0 100644 --- a/src/helpers/print.rs +++ b/src/helpers/print.rs @@ -1,39 +1,39 @@ -use super::{file::file_open, colors::print_cyan_bold}; -use crate::{helpers::{self, colors::print_bold}, ActionType}; +use super::{file::file_open, colors::print}; +use crate::{helpers::{self}, ActionType}; -pub fn print_detail(title: &str, value: String, atype: ActionType) { +pub fn print_detail(title: &str, value: String, atype: ActionType, color: &str) { print!(" "); match atype { ActionType::Details => { - helpers::colors::print_cyan_bold(&title, true); + helpers::colors::print(&title, true, color); for _ in 0..(10 - title.len()) { print!(" "); } - helpers::colors::print_bold(" : ", true); + helpers::colors::print(" : ", true, "white"); print!("{}", &value); }, ActionType::Delimiter => { - print_bold("-----------------------------", true); + print("-----------------------------", true, "white"); }, ActionType::HostInfo => { - print_bold(&format!( + print(&format!( "{}@{}", title, value - ), true); + ), true, color); } }; } -pub fn print_ponyline(line: u16, pony: &str) { +pub fn print_ponyline(line: u16, pony: &str, color: &str) { file_open(&format!("ponies/{}.txt", pony).to_string()) .lines() .skip(line as usize) .take(1) .for_each(|line| { - print_cyan_bold(line, true); + print(line, true, color); }); } \ No newline at end of file diff --git a/src/main.rs b/src/main.rs index 3f23b30..eb758cc 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,5 +1,9 @@ +use clap::Parser; +use args::Args; + mod helpers; mod system; +mod args; #[derive(Clone, Copy)] pub enum ActionType { @@ -68,8 +72,10 @@ const ACTIONS: [Action; 10] = [ ]; fn main() { + let args: Args = Args::parse(); + for i in 0..12 { - helpers::print::print_ponyline(i, "rainbowdash"); + helpers::print::print_ponyline(i, &args.pony, &args.color); if ACTIONS.get(i as usize).is_none() { println!(); @@ -81,7 +87,8 @@ fn main() { helpers::print::print_detail( &system::host::get_user(), system::host::get_hostname(), - ActionType::HostInfo + ActionType::HostInfo, + &args.color ); }, @@ -89,7 +96,8 @@ fn main() { helpers::print::print_detail( "", "".to_string(), - ActionType::Delimiter + ActionType::Delimiter, + args.color.as_str() ); }, @@ -97,7 +105,8 @@ fn main() { helpers::print::print_detail( ACTIONS[i as usize].name.unwrap(), ACTIONS[i as usize].func.unwrap()(), - ACTIONS[i as usize].action_type + ACTIONS[i as usize].action_type, + args.color.as_str() ); } }