tmux-copyrat/src/ui/colors.rs

97 lines
3.3 KiB
Rust
Raw Normal View History

2021-10-24 09:33:55 +02:00
use clap::Parser;
2020-06-02 20:03:16 +02:00
use termion::color;
2021-10-24 18:28:58 +02:00
use crate::{error::Error, Result};
2021-10-24 11:57:25 +02:00
pub fn parse_color(src: &str) -> Result<Box<dyn color::Color>> {
2020-05-24 21:02:11 +02:00
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)),
2021-03-23 22:20:21 +01:00
"bright-black" | "brightblack" => Ok(Box::new(color::LightBlack)),
"bright-red" | "brightred" => Ok(Box::new(color::LightRed)),
"bright-green" | "brightgreen" => Ok(Box::new(color::LightGreen)),
"bright-yellow" | "brightyellow" => Ok(Box::new(color::LightYellow)),
"bright-blue" | "brightblue" => Ok(Box::new(color::LightBlue)),
"bright-magenta" | "brightmagenta" => Ok(Box::new(color::LightMagenta)),
"bright-cyan" | "brightcyan" => Ok(Box::new(color::LightCyan)),
"bright-white" | "brightwhite" => Ok(Box::new(color::LightWhite)),
"none" => Ok(Box::new(color::Reset)),
2021-10-24 18:28:58 +02:00
_ => Err(Error::UnknownColor),
2020-05-24 21:02:11 +02:00
}
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]
2021-03-22 23:56:35 +01:00
fn span_color() {
2020-05-24 21:02:11 +02:00
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]
2021-03-22 23:56:35 +01:00
fn no_span_color() {
2020-05-24 21:02:11 +02:00
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
2021-03-21 11:32:57 +01:00
/// Holds color-related data.
2021-03-19 09:39:30 +01:00
///
2021-03-22 23:56:35 +01:00
/// - `focus_*` colors are used to render the currently focused text span.
/// - `normal_*` colors are used to render other text spans.
2021-03-19 09:39:30 +01:00
/// - `hint_*` colors are used to render the hints.
2021-10-24 09:33:55 +02:00
#[derive(Parser, Debug)]
2021-03-21 11:32:57 +01:00
#[clap(about)] // Needed to avoid this doc comment to be used as overall `about`.
2021-03-19 09:39:30 +01:00
pub struct UiColors {
/// Foreground color for base text.
2021-03-23 22:20:21 +01:00
#[clap(long, default_value = "bright-cyan", parse(try_from_str = parse_color))]
2021-03-19 09:39:30 +01:00
pub text_fg: Box<dyn color::Color>,
/// Background color for base text.
2021-03-23 22:20:21 +01:00
#[clap(long, default_value = "none", parse(try_from_str = parse_color))]
2021-03-19 09:39:30 +01:00
pub text_bg: Box<dyn color::Color>,
2021-03-22 23:56:35 +01:00
/// Foreground color for spans.
2021-03-23 22:20:21 +01:00
#[clap(long, default_value = "blue",
2021-03-19 09:39:30 +01:00
parse(try_from_str = parse_color))]
2021-03-22 23:56:35 +01:00
pub span_fg: Box<dyn color::Color>,
2021-03-19 09:39:30 +01:00
2021-03-22 23:56:35 +01:00
/// Background color for spans.
2021-03-23 22:20:21 +01:00
#[clap(long, default_value = "none",
2021-03-19 09:39:30 +01:00
parse(try_from_str = parse_color))]
2021-03-22 23:56:35 +01:00
pub span_bg: Box<dyn color::Color>,
2021-03-19 09:39:30 +01:00
2021-03-22 23:56:35 +01:00
/// Foreground color for the focused span.
2021-03-19 09:39:30 +01:00
#[clap(long, default_value = "magenta",
parse(try_from_str = parse_color))]
pub focused_fg: Box<dyn color::Color>,
2021-03-22 23:56:35 +01:00
/// Background color for the focused span.
2021-03-23 22:20:21 +01:00
#[clap(long, default_value = "none",
2021-03-19 09:39:30 +01:00
parse(try_from_str = parse_color))]
pub focused_bg: Box<dyn color::Color>,
/// Foreground color for hints.
2021-03-23 22:20:21 +01:00
#[clap(long, default_value = "yellow",
2021-03-19 09:39:30 +01:00
parse(try_from_str = parse_color))]
pub hint_fg: Box<dyn color::Color>,
/// Background color for hints.
2021-03-23 22:20:21 +01:00
#[clap(long, default_value = "none",
2021-03-19 09:39:30 +01:00
parse(try_from_str = parse_color))]
pub hint_bg: Box<dyn color::Color>,
}