Update termion backend

This commit is contained in:
Alexandre Bury 2017-06-12 18:31:08 -07:00
parent 9f5ce65e34
commit 059812f427

View File

@ -22,10 +22,7 @@ use theme;
pub struct Concrete { pub struct Concrete {
terminal: AlternateScreen<termion::raw::RawTerminal<::std::io::Stdout>>, terminal: AlternateScreen<termion::raw::RawTerminal<::std::io::Stdout>>,
current_style: Cell<theme::ColorStyle>, current_style: Cell<theme::ColorPair>,
colors:
HashMap<theme::ColorStyle, (Box<tcolor::Color>, Box<tcolor::Color>)>,
input: chan::Receiver<Event>, input: chan::Receiver<Event>,
resize: chan::Receiver<chan_signal::Signal>, resize: chan::Receiver<chan_signal::Signal>,
timeout: Option<u32>, timeout: Option<u32>,
@ -57,16 +54,9 @@ fn apply_colors(fg: &tcolor::Color, bg: &tcolor::Color) {
} }
impl Concrete { impl Concrete {
fn apply_colorstyle(&self, color_style: theme::ColorStyle) { fn apply_colorstyle(&self, colors: theme::ColorPair) {
if let theme::ColorStyle::Custom { front, back } = color_style { with_color(&colors.front, |c| print!("{}", tcolor::Fg(c)));
let fg = colour_to_termion_colour(&front); with_color(&colors.back, |c| print!("{}", tcolor::Bg(c)));
let bg = colour_to_termion_colour(&back);
apply_colors(&*fg, &*bg);
} else {
let (ref fg, ref bg) = self.colors[&color_style];
apply_colors(&**fg, &**bg);
}
} }
} }
@ -89,8 +79,7 @@ impl backend::Backend for Concrete {
let backend = Concrete { let backend = Concrete {
terminal: terminal, terminal: terminal,
current_style: Cell::new(theme::ColorStyle::Background), current_style: Cell::new(theme::ColorPair::from_256colors(0, 0)),
colors: HashMap::new(),
input: receiver, input: receiver,
resize: resize, resize: resize,
timeout: None, timeout: None,
@ -107,16 +96,7 @@ impl backend::Backend for Concrete {
termion::clear::All); termion::clear::All);
} }
fn init_color_style(&mut self, style: theme::ColorStyle, fn with_color<F: FnOnce()>(&self, color: theme::ColorPair, f: F) {
foreground: &theme::Color, background: &theme::Color) {
// Step 1: convert foreground and background into proper termion Color
self.colors
.insert(style,
(colour_to_termion_colour(foreground),
colour_to_termion_colour(background)));
}
fn with_color<F: FnOnce()>(&self, color: theme::ColorStyle, f: F) {
let current_style = self.current_style.get(); let current_style = self.current_style.get();
self.apply_colorstyle(color); self.apply_colorstyle(color);
@ -145,7 +125,7 @@ impl backend::Backend for Concrete {
} }
fn clear(&self) { fn clear(&self) {
self.apply_colorstyle(theme::ColorStyle::Background); // self.apply_colorstyle(theme::ColorStyle::Background);
print!("{}", termion::clear::All); print!("{}", termion::clear::All);
} }
@ -211,49 +191,43 @@ fn map_key(event: TEvent) -> Event {
} }
fn colour_to_termion_colour(clr: &theme::Color) -> Box<tcolor::Color> { fn with_color<F, R>(clr: &theme::Color, f: F) -> R
where F: FnOnce(&tcolor::Color) -> R
{
match *clr { match *clr {
theme::Color::Dark(theme::BaseColor::Black) => Box::new(tcolor::Black), theme::Color::Dark(theme::BaseColor::Black) => f(&tcolor::Black),
theme::Color::Dark(theme::BaseColor::Red) => Box::new(tcolor::Red), theme::Color::Dark(theme::BaseColor::Red) => f(&tcolor::Red),
theme::Color::Dark(theme::BaseColor::Green) => Box::new(tcolor::Green), theme::Color::Dark(theme::BaseColor::Green) => f(&tcolor::Green),
theme::Color::Dark(theme::BaseColor::Yellow) => { theme::Color::Dark(theme::BaseColor::Yellow) => f(&tcolor::Yellow),
Box::new(tcolor::Yellow) theme::Color::Dark(theme::BaseColor::Blue) => f(&tcolor::Blue),
} theme::Color::Dark(theme::BaseColor::Magenta) => f(&tcolor::Magenta),
theme::Color::Dark(theme::BaseColor::Blue) => Box::new(tcolor::Blue), theme::Color::Dark(theme::BaseColor::Cyan) => f(&tcolor::Cyan),
theme::Color::Dark(theme::BaseColor::Magenta) => { theme::Color::Dark(theme::BaseColor::White) => f(&tcolor::White),
Box::new(tcolor::Magenta)
}
theme::Color::Dark(theme::BaseColor::Cyan) => Box::new(tcolor::Cyan),
theme::Color::Dark(theme::BaseColor::White) => Box::new(tcolor::White),
theme::Color::Light(theme::BaseColor::Black) => { theme::Color::Light(theme::BaseColor::Black) => {
Box::new(tcolor::LightBlack) f(&tcolor::LightBlack)
} }
theme::Color::Light(theme::BaseColor::Red) => { theme::Color::Light(theme::BaseColor::Red) => f(&tcolor::LightRed),
Box::new(tcolor::LightRed) theme::Color::Light(theme::BaseColor::Green) => {
} f(&tcolor::LightGreen)
theme::Color::Light(theme::BaseColor::Green) => { }
Box::new(tcolor::LightGreen) theme::Color::Light(theme::BaseColor::Yellow) => {
} f(&tcolor::LightYellow)
theme::Color::Light(theme::BaseColor::Yellow) => { }
Box::new(tcolor::LightYellow) theme::Color::Light(theme::BaseColor::Blue) => f(&tcolor::LightBlue),
} theme::Color::Light(theme::BaseColor::Magenta) => {
theme::Color::Light(theme::BaseColor::Blue) => { f(&tcolor::LightMagenta)
Box::new(tcolor::LightBlue) }
} theme::Color::Light(theme::BaseColor::Cyan) => f(&tcolor::LightCyan),
theme::Color::Light(theme::BaseColor::Magenta) => { theme::Color::Light(theme::BaseColor::White) => {
Box::new(tcolor::LightMagenta) f(&tcolor::LightWhite)
} }
theme::Color::Light(theme::BaseColor::Cyan) => {
Box::new(tcolor::LightCyan)
}
theme::Color::Light(theme::BaseColor::White) => {
Box::new(tcolor::LightWhite)
}
theme::Color::Rgb(r, g, b) => Box::new(tcolor::Rgb(r, g, b)), theme::Color::Rgb(r, g, b) => f(&tcolor::Rgb(r, g, b)),
theme::Color::RgbLowRes(r, g, b) => { theme::Color::RgbLowRes(r, g, b) => {
Box::new(tcolor::AnsiValue::rgb(r, g, b)) f(&tcolor::AnsiValue::rgb(r, g, b))
} }
}
}
} }