Bring colors to termion backend

Currently require termion fork for color storage support.
This commit is contained in:
Alexandre Bury 2016-10-21 17:18:26 -07:00
parent d678d767f0
commit 965b32bd9b
3 changed files with 90 additions and 10 deletions

View File

@ -33,7 +33,8 @@ version = "0.6"
[dependencies.termion]
optional = true
version = "1.1.1"
# version = "1.1.1"
git = "https://github.com/gyscos/termion"
[dependencies.bear-lib-terminal]
optional = true

View File

@ -2,13 +2,30 @@ extern crate termion;
use ::backend;
use ::event::{Event, Key};
use std::io::Write;
use self::termion::color;
use self::termion::input::TermRead;
use self::termion::raw::IntoRawMode;
use ::theme::{BaseColor, Color, ColorStyle, Effect};
use std::cell::Cell;
use std::collections::BTreeMap;
use std::io::Write;
use ::theme;
pub struct Concrete {
terminal: termion::raw::RawTerminal<::std::io::Stdout>,
current_style: Cell<theme::ColorStyle>,
colors: BTreeMap<i16, (Box<color::Color>, Box<color::Color>)>,
}
fn apply_colors(fg: &color::Color, bg: &color::Color) {
print!("{}{}", color::Fg(fg), color::Bg(bg));
}
impl Concrete {
fn apply_colorstyle(&self, color_style: theme::ColorStyle) {
let (ref fg, ref bg) = self.colors[&color_style.id()];
apply_colors(&**fg, &**bg);
}
}
impl backend::Backend for Concrete {
@ -17,9 +34,10 @@ impl backend::Backend for Concrete {
let backend = Concrete {
terminal: ::std::io::stdout().into_raw_mode().unwrap(),
current_style: Cell::new(theme::ColorStyle::Background),
colors: BTreeMap::new(),
};
backend.clear();
backend
}
@ -29,19 +47,31 @@ impl backend::Backend for Concrete {
self.clear();
}
fn init_color_style(&mut self, style: ColorStyle, foreground: &Color,
background: &Color) {
// Do we _need_ to save the color?
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.id(),
(colour_to_termion_colour(foreground),
colour_to_termion_colour(background)));
}
fn with_color<F: FnOnce()>(&self, color: ColorStyle, f: F) {
fn with_color<F: FnOnce()>(&self, color: theme::ColorStyle, f: F) {
// TODO: actually use colors
// TODO: careful! need to remember the previous state
// and apply it back
let current_style = self.current_style.get();
self.apply_colorstyle(color);
self.current_style.set(color);
f();
self.current_style.set(current_style);
self.apply_colorstyle(current_style);
}
fn with_effect<F: FnOnce()>(&self, effect: Effect, f: F) {
fn with_effect<F: FnOnce()>(&self, effect: theme::Effect, f: F) {
// TODO: actually use effects
// TODO: careful! need to remember the previous state
// and apply it back
@ -59,11 +89,11 @@ impl backend::Backend for Concrete {
}
fn clear(&self) {
self.apply_colorstyle(theme::ColorStyle::Background);
print!("{}", termion::clear::All);
}
fn refresh(&mut self) {
// Not sure termion needs a refresh phase
self.terminal.flush().unwrap();
}
@ -79,7 +109,55 @@ impl backend::Backend for Concrete {
}
fn poll_event(&self) -> Event {
// TODO: parse input
::std::io::stdin().keys().next();
Event::Key(Key::Enter)
}
}
fn colour_to_termion_colour(clr: &theme::Color) -> Box<color::Color> {
match *clr {
theme::Color::Dark(theme::BaseColor::Black) => Box::new(color::Black),
theme::Color::Dark(theme::BaseColor::Red) => Box::new(color::Red),
theme::Color::Dark(theme::BaseColor::Green) => Box::new(color::Green),
theme::Color::Dark(theme::BaseColor::Yellow) => {
Box::new(color::Yellow)
}
theme::Color::Dark(theme::BaseColor::Blue) => Box::new(color::Blue),
theme::Color::Dark(theme::BaseColor::Magenta) => {
Box::new(color::Magenta)
}
theme::Color::Dark(theme::BaseColor::Cyan) => Box::new(color::Cyan),
theme::Color::Dark(theme::BaseColor::White) => Box::new(color::White),
theme::Color::Light(theme::BaseColor::Black) => {
Box::new(color::LightBlack)
}
theme::Color::Light(theme::BaseColor::Red) => {
Box::new(color::LightRed)
}
theme::Color::Light(theme::BaseColor::Green) => {
Box::new(color::LightGreen)
}
theme::Color::Light(theme::BaseColor::Yellow) => {
Box::new(color::LightYellow)
}
theme::Color::Light(theme::BaseColor::Blue) => {
Box::new(color::LightBlue)
}
theme::Color::Light(theme::BaseColor::Magenta) => {
Box::new(color::LightMagenta)
}
theme::Color::Light(theme::BaseColor::Cyan) => {
Box::new(color::LightCyan)
}
theme::Color::Light(theme::BaseColor::White) => {
Box::new(color::LightWhite)
}
theme::Color::Rgb(r, g, b) => Box::new(color::Rgb(r, g, b)),
theme::Color::RgbLowRes(r, g, b) => {
Box::new(color::AnsiValue::rgb(r, g, b))
}
}
}

View File

@ -257,6 +257,7 @@ impl Theme {
backend.init_color_style(ColorStyle::HighlightInactive,
&self.colors.view,
&self.colors.highlight_inactive);
backend.clear();
}
}