2020-05-24 21:02:11 +02:00
|
|
|
use crate::error;
|
2021-03-19 09:39:30 +01:00
|
|
|
use clap::Clap;
|
2020-06-02 20:03:16 +02:00
|
|
|
use termion::color;
|
|
|
|
|
|
2020-05-24 21:02:11 +02:00
|
|
|
pub fn parse_color(src: &str) -> Result<Box<dyn color::Color>, error::ParseError> {
|
|
|
|
|
match src {
|
|
|
|
|
"black" => Ok(Box::new(color::Black)),
|
|
|
|
|
"red" => Ok(Box::new(color::Red)),
|
|
|
|
|
"green" => Ok(Box::new(color::Green)),
|
|
|
|
|
"yellow" => Ok(Box::new(color::Yellow)),
|
|
|
|
|
"blue" => Ok(Box::new(color::Blue)),
|
|
|
|
|
"magenta" => Ok(Box::new(color::Magenta)),
|
|
|
|
|
"cyan" => Ok(Box::new(color::Cyan)),
|
|
|
|
|
"white" => Ok(Box::new(color::White)),
|
2020-05-29 20:48:54 +02:00
|
|
|
"bright-black" => Ok(Box::new(color::LightBlack)),
|
|
|
|
|
"bright-red" => Ok(Box::new(color::LightRed)),
|
|
|
|
|
"bright-green" => Ok(Box::new(color::LightGreen)),
|
|
|
|
|
"bright-yellow" => Ok(Box::new(color::LightYellow)),
|
|
|
|
|
"bright-blue" => Ok(Box::new(color::LightBlue)),
|
|
|
|
|
"bright-magenta" => Ok(Box::new(color::LightMagenta)),
|
|
|
|
|
"bright-cyan" => Ok(Box::new(color::LightCyan)),
|
|
|
|
|
"bright-white" => Ok(Box::new(color::LightWhite)),
|
2020-05-24 21:02:11 +02:00
|
|
|
// "default" => Ok(Box::new(color::Reset)),
|
|
|
|
|
_ => Err(error::ParseError::UnknownColor),
|
|
|
|
|
}
|
2020-06-02 20:03:16 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
|
mod tests {
|
2020-05-24 21:02:11 +02:00
|
|
|
use super::*;
|
2020-06-02 20:03:16 +02:00
|
|
|
|
2020-05-24 21:02:11 +02:00
|
|
|
#[test]
|
|
|
|
|
fn match_color() {
|
|
|
|
|
let text1 = format!(
|
|
|
|
|
"{}{}",
|
|
|
|
|
color::Fg(parse_color("green").unwrap().as_ref()),
|
|
|
|
|
"foo"
|
|
|
|
|
);
|
|
|
|
|
let text2 = format!("{}{}", color::Fg(color::Green), "foo");
|
2020-06-02 20:03:16 +02:00
|
|
|
|
2020-05-24 21:02:11 +02:00
|
|
|
assert_eq!(text1, text2);
|
|
|
|
|
}
|
2020-06-02 20:03:16 +02:00
|
|
|
|
2020-05-24 21:02:11 +02:00
|
|
|
#[test]
|
|
|
|
|
fn no_match_color() {
|
|
|
|
|
assert!(parse_color("wat").is_err(), "this color should not exist");
|
|
|
|
|
}
|
2020-06-02 20:03:16 +02:00
|
|
|
}
|
2021-03-19 09:39:30 +01:00
|
|
|
|
|
|
|
|
/// Holds color-related data, for clarity.
|
|
|
|
|
///
|
|
|
|
|
/// - `focus_*` colors are used to render the currently focused matched text.
|
|
|
|
|
/// - `normal_*` colors are used to render other matched text.
|
|
|
|
|
/// - `hint_*` colors are used to render the hints.
|
|
|
|
|
#[derive(Clap, Debug)]
|
|
|
|
|
pub struct UiColors {
|
|
|
|
|
/// Foreground color for base text.
|
|
|
|
|
#[clap(long, default_value = "bright-cyan", parse(try_from_str = parse_color))]
|
|
|
|
|
pub text_fg: Box<dyn color::Color>,
|
|
|
|
|
|
|
|
|
|
/// Background color for base text.
|
|
|
|
|
#[clap(long, default_value = "bright-white", parse(try_from_str = parse_color))]
|
|
|
|
|
pub text_bg: Box<dyn color::Color>,
|
|
|
|
|
|
|
|
|
|
/// Foreground color for matches.
|
|
|
|
|
#[clap(long, default_value = "yellow",
|
|
|
|
|
parse(try_from_str = parse_color))]
|
|
|
|
|
pub match_fg: Box<dyn color::Color>,
|
|
|
|
|
|
|
|
|
|
/// Background color for matches.
|
|
|
|
|
#[clap(long, default_value = "bright-white",
|
|
|
|
|
parse(try_from_str = parse_color))]
|
|
|
|
|
pub match_bg: Box<dyn color::Color>,
|
|
|
|
|
|
|
|
|
|
/// Foreground color for the focused match.
|
|
|
|
|
#[clap(long, default_value = "magenta",
|
|
|
|
|
parse(try_from_str = parse_color))]
|
|
|
|
|
pub focused_fg: Box<dyn color::Color>,
|
|
|
|
|
|
|
|
|
|
/// Background color for the focused match.
|
|
|
|
|
#[clap(long, default_value = "bright-white",
|
|
|
|
|
parse(try_from_str = parse_color))]
|
|
|
|
|
pub focused_bg: Box<dyn color::Color>,
|
|
|
|
|
|
|
|
|
|
/// Foreground color for hints.
|
|
|
|
|
#[clap(long, default_value = "white",
|
|
|
|
|
parse(try_from_str = parse_color))]
|
|
|
|
|
pub hint_fg: Box<dyn color::Color>,
|
|
|
|
|
|
|
|
|
|
/// Background color for hints.
|
|
|
|
|
#[clap(long, default_value = "magenta",
|
|
|
|
|
parse(try_from_str = parse_color))]
|
|
|
|
|
pub hint_bg: Box<dyn color::Color>,
|
|
|
|
|
}
|