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 {
terminal: AlternateScreen<termion::raw::RawTerminal<::std::io::Stdout>>,
current_style: Cell<theme::ColorStyle>,
colors:
HashMap<theme::ColorStyle, (Box<tcolor::Color>, Box<tcolor::Color>)>,
current_style: Cell<theme::ColorPair>,
input: chan::Receiver<Event>,
resize: chan::Receiver<chan_signal::Signal>,
timeout: Option<u32>,
@ -57,16 +54,9 @@ fn apply_colors(fg: &tcolor::Color, bg: &tcolor::Color) {
}
impl Concrete {
fn apply_colorstyle(&self, color_style: theme::ColorStyle) {
if let theme::ColorStyle::Custom { front, back } = color_style {
let fg = colour_to_termion_colour(&front);
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);
}
fn apply_colorstyle(&self, colors: theme::ColorPair) {
with_color(&colors.front, |c| print!("{}", tcolor::Fg(c)));
with_color(&colors.back, |c| print!("{}", tcolor::Bg(c)));
}
}
@ -89,8 +79,7 @@ impl backend::Backend for Concrete {
let backend = Concrete {
terminal: terminal,
current_style: Cell::new(theme::ColorStyle::Background),
colors: HashMap::new(),
current_style: Cell::new(theme::ColorPair::from_256colors(0, 0)),
input: receiver,
resize: resize,
timeout: None,
@ -107,16 +96,7 @@ impl backend::Backend for Concrete {
termion::clear::All);
}
fn init_color_style(&mut self, style: theme::ColorStyle,
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) {
fn with_color<F: FnOnce()>(&self, color: theme::ColorPair, f: F) {
let current_style = self.current_style.get();
self.apply_colorstyle(color);
@ -145,7 +125,7 @@ impl backend::Backend for Concrete {
}
fn clear(&self) {
self.apply_colorstyle(theme::ColorStyle::Background);
// self.apply_colorstyle(theme::ColorStyle::Background);
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 {
theme::Color::Dark(theme::BaseColor::Black) => Box::new(tcolor::Black),
theme::Color::Dark(theme::BaseColor::Red) => Box::new(tcolor::Red),
theme::Color::Dark(theme::BaseColor::Green) => Box::new(tcolor::Green),
theme::Color::Dark(theme::BaseColor::Yellow) => {
Box::new(tcolor::Yellow)
}
theme::Color::Dark(theme::BaseColor::Blue) => Box::new(tcolor::Blue),
theme::Color::Dark(theme::BaseColor::Magenta) => {
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::Dark(theme::BaseColor::Black) => f(&tcolor::Black),
theme::Color::Dark(theme::BaseColor::Red) => f(&tcolor::Red),
theme::Color::Dark(theme::BaseColor::Green) => f(&tcolor::Green),
theme::Color::Dark(theme::BaseColor::Yellow) => f(&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::Cyan) => f(&tcolor::Cyan),
theme::Color::Dark(theme::BaseColor::White) => f(&tcolor::White),
theme::Color::Light(theme::BaseColor::Black) => {
Box::new(tcolor::LightBlack)
}
theme::Color::Light(theme::BaseColor::Red) => {
Box::new(tcolor::LightRed)
f(&tcolor::LightBlack)
}
theme::Color::Light(theme::BaseColor::Red) => f(&tcolor::LightRed),
theme::Color::Light(theme::BaseColor::Green) => {
Box::new(tcolor::LightGreen)
f(&tcolor::LightGreen)
}
theme::Color::Light(theme::BaseColor::Yellow) => {
Box::new(tcolor::LightYellow)
}
theme::Color::Light(theme::BaseColor::Blue) => {
Box::new(tcolor::LightBlue)
f(&tcolor::LightYellow)
}
theme::Color::Light(theme::BaseColor::Blue) => f(&tcolor::LightBlue),
theme::Color::Light(theme::BaseColor::Magenta) => {
Box::new(tcolor::LightMagenta)
}
theme::Color::Light(theme::BaseColor::Cyan) => {
Box::new(tcolor::LightCyan)
f(&tcolor::LightMagenta)
}
theme::Color::Light(theme::BaseColor::Cyan) => f(&tcolor::LightCyan),
theme::Color::Light(theme::BaseColor::White) => {
Box::new(tcolor::LightWhite)
f(&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) => {
Box::new(tcolor::AnsiValue::rgb(r, g, b))
}
f(&tcolor::AnsiValue::rgb(r, g, b))
}
}
}