From e3b9db70b3291c04d02301de4e2b81b8b1dc98c5 Mon Sep 17 00:00:00 2001 From: Yuto Nishida Date: Sun, 16 May 2021 18:00:22 -0700 Subject: [PATCH] Improve readability --- src/main.rs | 24 +++++++++++++++++------- src/utils.rs | 36 ++++++++++++++++++++++++++++++++---- 2 files changed, 49 insertions(+), 11 deletions(-) diff --git a/src/main.rs b/src/main.rs index 6a294c2..9ef6d98 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,6 +1,6 @@ mod utils; use utils::*; -use clap::{App, Arg}; +use clap::{App, Arg, ArgGroup}; fn main() { let arg_matches = App::new("pecho") @@ -53,14 +53,15 @@ fn main() { ) .arg( Arg::with_name("color") - .help("Specify color using an argument. Overrides single color options") + .help("Specify color using an argument") .short("c") .long("color") .takes_value(true) + .possible_values(&COLORS) ) .arg( Arg::with_name("colorBg") - .help("Specify background color using an argument. Overrides single color options") + .help("Specify background color using an argument") .short("C") .long("color-bg") .takes_value(true) @@ -76,7 +77,7 @@ fn main() { ) .arg( Arg::with_name("truecolor") - .help("Hex color in xxxxxx format. Overrides other color options") + .help("Hex color in xxxxxx format") .short("t") .long("truecolor") .takes_value(true) @@ -84,12 +85,21 @@ fn main() { ) .arg( Arg::with_name("truecolorBg") - .help("Background in hex in xxxxxx format. Overrides other color options") + .help("Background in hex in xxxxxx format") .short("T") .long("truecolor-bg") .takes_value(true) .value_name("hex"), ) + .group(ArgGroup::with_name("specific") + .args(&["black", "red", "green", "yellow", "blue", "purple", "cyan", "white", + "blackBg", "redBg", "greenBg", "yellowBg", "blueBg", "purpleBg", "cyanBg", "whiteBg",])) + .group(ArgGroup::with_name("generic") + .args(&["color", "colorBg"]) + .conflicts_with("specific")) + .group(ArgGroup::with_name("trueGeneric") + .args(&["truecolor", "truecolorBg"]) + .conflicts_with_all(&["specific", "generic"])) .get_matches(); // Concatenate input into space-separated words @@ -98,9 +108,9 @@ fn main() { // Replace escaped special characters and add trailing newline if necessary let std_print_string = special_chars_and_newlines(input, arg_matches.is_present("noEscapes"), arg_matches.is_present("noNewline")); - let std_print_string = add_color(std_print_string, &arg_matches, false); + let std_print_string = add_color_fg(std_print_string, &arg_matches); - let std_print_string = add_color(std_print_string.to_string(), &arg_matches, true); + let std_print_string = add_color_bg(std_print_string.to_string(), &arg_matches); let std_print_string = add_style(std_print_string, &arg_matches); diff --git a/src/utils.rs b/src/utils.rs index 4df833d..11405a7 100644 --- a/src/utils.rs +++ b/src/utils.rs @@ -4,10 +4,30 @@ use lazy_static::*; use regex::Regex; use std::char; -const COLORS: [&str; 8] = [ +enum PechoErr { + NoMatch, + InvalidHex, +} + +pub const COLORS: [&str; 8] = [ "black", "red", "green", "yellow", "blue", "purple", "cyan", "white", ]; +// convert a color string to a Color enum +fn parse_color(s: &str) -> Result{ + match s { + "black" => Ok(Color::Black), + "red" => Ok(Color::Red), + "green" => Ok(Color::Green), + "yellow" => Ok(Color::Yellow), + "blue" => Ok(Color::Blue), + "purple" => Ok(Color::Magenta), + "cyan" => Ok(Color::Cyan), + "white" => Ok(Color::White), + _ => Err(PechoErr::NoMatch), + } +} + // turn iterable of input into a string of space-separated words pub fn args_to_input(values: Option) -> String { match values { @@ -115,18 +135,26 @@ fn is_valid_hex(s: &str) -> bool { RE.is_match(s) } -pub fn hex_to_dectuple(hex: &str) -> Result<(u8, u8, u8), &str> { +fn hex_to_dectuple(hex: &str) -> Result<(u8, u8, u8), PechoErr> { if is_valid_hex(&hex) { let first = u8::from_str_radix(&hex[0..2], 16).unwrap(); let second = u8::from_str_radix(&hex[2..4], 16).unwrap(); let third = u8::from_str_radix(&hex[4..6], 16).unwrap(); Ok((first, second, third)) } else { - Err("Not a valid hex") + Err(PechoErr::InvalidHex) } } -pub fn add_color(input: String, arg_matches: &ArgMatches, is_bg: bool) -> ColoredString { +pub fn add_color_fg(input: String, arg_matches: &ArgMatches) -> ColoredString { + add_color(input, arg_matches, false) +} + +pub fn add_color_bg(input: String, arg_matches: &ArgMatches) -> ColoredString { + add_color(input, arg_matches, true) +} + +fn add_color(input: String, arg_matches: &ArgMatches, is_bg: bool) -> ColoredString { let suffix = if is_bg { "Bg" } else { "" }; if let Some(hex) = arg_matches.value_of("truecolor".to_owned() + suffix) { if let Ok(truecolor_args) = hex_to_dectuple(&hex) {