get_color to Box<dyn color::Color> instead of heap-allocating a ref.

This commit is contained in:
Barnabas 2020-12-29 16:12:08 +01:00 committed by Ferran Basora
parent 422eed9ecc
commit b97519170b
2 changed files with 28 additions and 28 deletions

View File

@ -1,16 +1,16 @@
use termion::color;
pub fn get_color(color_name: &str) -> Box<&dyn color::Color> {
pub fn get_color(color_name: &str) -> Box<dyn color::Color> {
match color_name {
"black" => Box::new(&color::Black),
"red" => Box::new(&color::Red),
"green" => Box::new(&color::Green),
"yellow" => Box::new(&color::Yellow),
"blue" => Box::new(&color::Blue),
"magenta" => Box::new(&color::Magenta),
"cyan" => Box::new(&color::Cyan),
"white" => Box::new(&color::White),
"default" => Box::new(&color::Reset),
"black" => Box::new(color::Black),
"red" => Box::new(color::Red),
"green" => Box::new(color::Green),
"yellow" => Box::new(color::Yellow),
"blue" => Box::new(color::Blue),
"magenta" => Box::new(color::Magenta),
"cyan" => Box::new(color::Cyan),
"white" => Box::new(color::White),
"default" => Box::new(color::Reset),
_ => panic!("Unknown color: {}", color_name),
}
}
@ -21,7 +21,7 @@ mod tests {
#[test]
fn match_color() {
let text1 = println!("{}{}", color::Fg(*get_color("green")), "foo");
let text1 = println!("{}{}", color::Fg(&*get_color("green")), "foo");
let text2 = println!("{}{}", color::Fg(color::Green), "foo");
assert_eq!(text1, text2);
@ -30,6 +30,6 @@ mod tests {
#[test]
#[should_panic]
fn no_match_color() {
println!("{}{}", color::Fg(*get_color("wat")), "foo");
println!("{}{}", color::Fg(&*get_color("wat")), "foo");
}
}

View File

@ -17,12 +17,12 @@ pub struct View<'a> {
contrast: bool,
position: &'a str,
matches: Vec<state::Match<'a>>,
select_foreground_color: Box<&'a dyn color::Color>,
select_background_color: Box<&'a dyn color::Color>,
foreground_color: Box<&'a dyn color::Color>,
background_color: Box<&'a dyn color::Color>,
hint_background_color: Box<&'a dyn color::Color>,
hint_foreground_color: Box<&'a dyn color::Color>,
select_foreground_color: Box<dyn color::Color>,
select_background_color: Box<dyn color::Color>,
foreground_color: Box<dyn color::Color>,
background_color: Box<dyn color::Color>,
hint_background_color: Box<dyn color::Color>,
hint_foreground_color: Box<dyn color::Color>,
}
enum CaptureEvent {
@ -38,12 +38,12 @@ impl<'a> View<'a> {
unique: bool,
contrast: bool,
position: &'a str,
select_foreground_color: Box<&'a dyn color::Color>,
select_background_color: Box<&'a dyn color::Color>,
foreground_color: Box<&'a dyn color::Color>,
background_color: Box<&'a dyn color::Color>,
hint_foreground_color: Box<&'a dyn color::Color>,
hint_background_color: Box<&'a dyn color::Color>,
select_foreground_color: Box<dyn color::Color>,
select_background_color: Box<dyn color::Color>,
foreground_color: Box<dyn color::Color>,
background_color: Box<dyn color::Color>,
hint_foreground_color: Box<dyn color::Color>,
hint_background_color: Box<dyn color::Color>,
) -> View<'a> {
let matches = state.matches(reverse, unique);
let skip = if reverse { matches.len() - 1 } else { 0 };
@ -119,8 +119,8 @@ impl<'a> View<'a> {
print!(
"{goto}{background}{foregroud}{text}{resetf}{resetb}",
goto = cursor::Goto(offset + 1, mat.y as u16 + 1),
foregroud = color::Fg(**selected_color),
background = color::Bg(**selected_background_color),
foregroud = color::Fg(&**selected_color),
background = color::Bg(&**selected_background_color),
resetf = color::Fg(color::Reset),
resetb = color::Bg(color::Reset),
text = &text
@ -140,8 +140,8 @@ impl<'a> View<'a> {
print!(
"{goto}{background}{foregroud}{text}{resetf}{resetb}",
goto = cursor::Goto(final_position as u16 + 1, mat.y as u16 + 1),
foregroud = color::Fg(*self.hint_foreground_color),
background = color::Bg(*self.hint_background_color),
foregroud = color::Fg(&*self.hint_foreground_color),
background = color::Bg(&*self.hint_background_color),
resetf = color::Fg(color::Reset),
resetb = color::Bg(color::Reset),
text = &text