Skip to content

Commit

Permalink
Improve readability
Browse files Browse the repository at this point in the history
  • Loading branch information
starptr committed May 17, 2021
1 parent d80d79b commit e3b9db7
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 11 deletions.
24 changes: 17 additions & 7 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -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")
Expand Down Expand Up @@ -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)
Expand All @@ -76,20 +77,29 @@ 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)
.value_name("hex"),
)
.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
Expand All @@ -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);

Expand Down
36 changes: 32 additions & 4 deletions src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Color, PechoErr>{
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<Values>) -> String {
match values {
Expand Down Expand Up @@ -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) {
Expand Down

0 comments on commit e3b9db7

Please sign in to comment.