refactor: UiColors -> colors module

This commit is contained in:
graelo 2021-03-19 09:39:30 +01:00
parent de1aa3889c
commit d119ce6b0d
4 changed files with 50 additions and 49 deletions

View file

@ -1,4 +1,5 @@
use crate::error;
use clap::Clap;
use termion::color;
pub fn parse_color(src: &str) -> Result<Box<dyn color::Color>, error::ParseError> {
@ -45,3 +46,49 @@ mod tests {
assert!(parse_color("wat").is_err(), "this color should not exist");
}
}
/// 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>,
}

View file

@ -98,7 +98,7 @@ pub struct CliOpt {
unique_hint: bool,
#[clap(flatten)]
colors: ui::UiColors,
colors: colors::UiColors,
/// Align hint with its match.
#[clap(long, arg_enum, default_value = "leading")]

View file

@ -19,4 +19,4 @@
mod vc;
pub use vc::ViewController;
pub use vc::{HintAlignment, HintStyle, UiColors};
pub use vc::{HintAlignment, HintStyle};

View file

@ -8,7 +8,7 @@ use sequence_trie::SequenceTrie;
use termion::{self, color, cursor, event, style};
use crate::error::ParseError;
use crate::{colors, model, output_destination::OutputDestination, selection::Selection};
use crate::{colors::UiColors, model, output_destination::OutputDestination, selection::Selection};
pub struct ViewController<'a> {
model: &'a mut model::Model<'a>,
@ -1115,49 +1115,3 @@ Barcelona https://en.wikipedia.org/wiki/Barcelona - ";
assert_eq!(writer, expected.as_bytes());
}
}
// /// 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 = colors::parse_color))]
pub text_fg: Box<dyn color::Color>,
/// Background color for base text.
#[clap(long, default_value = "bright-white", parse(try_from_str = colors::parse_color))]
pub text_bg: Box<dyn color::Color>,
/// Foreground color for matches.
#[clap(long, default_value = "yellow",
parse(try_from_str = colors::parse_color))]
pub match_fg: Box<dyn color::Color>,
/// Background color for matches.
#[clap(long, default_value = "bright-white",
parse(try_from_str = colors::parse_color))]
pub match_bg: Box<dyn color::Color>,
/// Foreground color for the focused match.
#[clap(long, default_value = "magenta",
parse(try_from_str = colors::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 = colors::parse_color))]
pub focused_bg: Box<dyn color::Color>,
/// Foreground color for hints.
#[clap(long, default_value = "white",
parse(try_from_str = colors::parse_color))]
pub hint_fg: Box<dyn color::Color>,
/// Background color for hints.
#[clap(long, default_value = "magenta",
parse(try_from_str = colors::parse_color))]
pub hint_bg: Box<dyn color::Color>,
}