feat: 🎸 Command line arguments

This commit is contained in:
Atsukoro1 2022-12-01 21:42:13 +01:00
parent da7121e889
commit c16a99c456
5 changed files with 71 additions and 31 deletions

View file

@ -2,3 +2,6 @@
name = "ponyfetch" name = "ponyfetch"
version = "0.1.0" version = "0.1.0"
edition = "2021" edition = "2021"
[dependencies]
clap = { version = "4.0.29", features = ["derive"] }

15
src/args.rs Normal file
View file

@ -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
}

View file

@ -1,23 +1,36 @@
pub fn print_cyan(text: &str, inline: bool) { // make const with color names and match them with unicode symbol for color
if inline { const COLORS : [(&str, &str); 17] = [
print!("\x1b[36m{}\x1b[0m", text); ("black", "\u{001b}[30m"),
} else { ("red", "\u{001b}[31m"),
println!("\x1b[36m{}\x1b[0m", text); ("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(text: &str, inline: bool, color: &str) {
let mut color = color;
if color == "rainbowdash" {
color = "bright_magenta";
} }
pub fn print_cyan_bold(text: &str, inline: bool) { let color = color;
if inline { let color = COLORS.iter().find(|(name, _)| name == &color).unwrap().1;
print!("\x1b[36m\x1b[1m{}\x1b[0m", text);
} else {
println!("\x1b[36m\x1b[1m{}\x1b[0m", text);
}
}
pub fn print_bold(text: &str, inline: bool) {
if inline { if inline {
print!("\x1b[1m{}\x1b[0m", text); print!("{}{}", color, text);
} else { } else {
println!("\x1b[1m{}\x1b[0m", text); println!("{}{}", color, text);
} }
} }

View file

@ -1,39 +1,39 @@
use super::{file::file_open, colors::print_cyan_bold}; use super::{file::file_open, colors::print};
use crate::{helpers::{self, colors::print_bold}, ActionType}; 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!(" "); print!(" ");
match atype { match atype {
ActionType::Details => { ActionType::Details => {
helpers::colors::print_cyan_bold(&title, true); helpers::colors::print(&title, true, color);
for _ in 0..(10 - title.len()) { for _ in 0..(10 - title.len()) {
print!(" "); print!(" ");
} }
helpers::colors::print_bold(" : ", true); helpers::colors::print(" : ", true, "white");
print!("{}", &value); print!("{}", &value);
}, },
ActionType::Delimiter => { ActionType::Delimiter => {
print_bold("-----------------------------", true); print("-----------------------------", true, "white");
}, },
ActionType::HostInfo => { ActionType::HostInfo => {
print_bold(&format!( print(&format!(
"{}@{}", "{}@{}",
title, title,
value 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()) file_open(&format!("ponies/{}.txt", pony).to_string())
.lines() .lines()
.skip(line as usize) .skip(line as usize)
.take(1) .take(1)
.for_each(|line| { .for_each(|line| {
print_cyan_bold(line, true); print(line, true, color);
}); });
} }

View file

@ -1,5 +1,9 @@
use clap::Parser;
use args::Args;
mod helpers; mod helpers;
mod system; mod system;
mod args;
#[derive(Clone, Copy)] #[derive(Clone, Copy)]
pub enum ActionType { pub enum ActionType {
@ -68,8 +72,10 @@ const ACTIONS: [Action; 10] = [
]; ];
fn main() { fn main() {
let args: Args = Args::parse();
for i in 0..12 { 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() { if ACTIONS.get(i as usize).is_none() {
println!(); println!();
@ -81,7 +87,8 @@ fn main() {
helpers::print::print_detail( helpers::print::print_detail(
&system::host::get_user(), &system::host::get_user(),
system::host::get_hostname(), system::host::get_hostname(),
ActionType::HostInfo ActionType::HostInfo,
&args.color
); );
}, },
@ -89,7 +96,8 @@ fn main() {
helpers::print::print_detail( helpers::print::print_detail(
"", "",
"".to_string(), "".to_string(),
ActionType::Delimiter ActionType::Delimiter,
args.color.as_str()
); );
}, },
@ -97,7 +105,8 @@ fn main() {
helpers::print::print_detail( helpers::print::print_detail(
ACTIONS[i as usize].name.unwrap(), ACTIONS[i as usize].name.unwrap(),
ACTIONS[i as usize].func.unwrap()(), ACTIONS[i as usize].func.unwrap()(),
ACTIONS[i as usize].action_type ACTIONS[i as usize].action_type,
args.color.as_str()
); );
} }
} }