2016-10-10 00:16:35 +00:00
|
|
|
extern crate termion;
|
|
|
|
|
2016-10-08 23:47:15 +00:00
|
|
|
use ::backend;
|
|
|
|
use ::event::{Event, Key};
|
2016-10-22 00:18:26 +00:00
|
|
|
use self::termion::color;
|
2016-10-24 18:16:56 +00:00
|
|
|
use self::termion::event::Key as TKey;
|
2016-10-10 21:08:07 +00:00
|
|
|
use self::termion::input::TermRead;
|
2016-10-24 18:16:56 +00:00
|
|
|
use self::termion::style;
|
2016-10-10 21:08:07 +00:00
|
|
|
use self::termion::raw::IntoRawMode;
|
2016-10-22 00:18:26 +00:00
|
|
|
use std::cell::Cell;
|
|
|
|
use std::collections::BTreeMap;
|
|
|
|
use std::io::Write;
|
|
|
|
|
|
|
|
use ::theme;
|
2016-10-08 23:47:15 +00:00
|
|
|
|
2016-10-10 00:16:35 +00:00
|
|
|
pub struct Concrete {
|
2016-10-08 23:47:15 +00:00
|
|
|
terminal: termion::raw::RawTerminal<::std::io::Stdout>,
|
2016-10-22 00:18:26 +00:00
|
|
|
current_style: Cell<theme::ColorStyle>,
|
|
|
|
colors: BTreeMap<i16, (Box<color::Color>, Box<color::Color>)>,
|
|
|
|
}
|
|
|
|
|
2016-10-24 18:16:56 +00:00
|
|
|
trait Effectable {
|
|
|
|
fn on(&self);
|
|
|
|
fn off(&self);
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Effectable for theme::Effect {
|
|
|
|
fn on(&self) {
|
|
|
|
match *self {
|
|
|
|
theme::Effect::Simple => (),
|
|
|
|
theme::Effect::Reverse => print!("{}", style::Invert),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn off(&self) {
|
|
|
|
match *self {
|
|
|
|
theme::Effect::Simple => (),
|
|
|
|
theme::Effect::Reverse => print!("{}", style::NoInvert),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-10-22 00:18:26 +00:00
|
|
|
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);
|
|
|
|
}
|
2016-10-08 23:47:15 +00:00
|
|
|
}
|
|
|
|
|
2016-10-10 00:16:35 +00:00
|
|
|
impl backend::Backend for Concrete {
|
2016-10-08 23:47:15 +00:00
|
|
|
fn init() -> Self {
|
|
|
|
print!("{}", termion::cursor::Hide);
|
|
|
|
|
2016-10-10 00:16:35 +00:00
|
|
|
let backend = Concrete {
|
2016-10-08 23:47:15 +00:00
|
|
|
terminal: ::std::io::stdout().into_raw_mode().unwrap(),
|
2016-10-22 00:18:26 +00:00
|
|
|
current_style: Cell::new(theme::ColorStyle::Background),
|
|
|
|
colors: BTreeMap::new(),
|
2016-10-09 23:03:49 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
backend
|
2016-10-08 23:47:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fn finish(&mut self) {
|
|
|
|
// Maybe we should clear everything?
|
|
|
|
print!("{}{}", termion::cursor::Show, termion::cursor::Goto(1, 1));
|
2016-10-09 23:03:49 +00:00
|
|
|
self.clear();
|
2016-10-08 23:47:15 +00:00
|
|
|
}
|
|
|
|
|
2016-10-22 00:18:26 +00:00
|
|
|
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)));
|
2016-10-08 23:47:15 +00:00
|
|
|
}
|
|
|
|
|
2016-10-22 00:18:26 +00:00
|
|
|
fn with_color<F: FnOnce()>(&self, color: theme::ColorStyle, f: F) {
|
2016-10-08 23:47:15 +00:00
|
|
|
// TODO: actually use colors
|
|
|
|
// TODO: careful! need to remember the previous state
|
|
|
|
// and apply it back
|
2016-10-22 00:18:26 +00:00
|
|
|
|
|
|
|
let current_style = self.current_style.get();
|
|
|
|
|
|
|
|
self.apply_colorstyle(color);
|
|
|
|
|
|
|
|
self.current_style.set(color);
|
2016-10-08 23:47:15 +00:00
|
|
|
f();
|
2016-10-22 00:18:26 +00:00
|
|
|
self.current_style.set(current_style);
|
|
|
|
|
|
|
|
self.apply_colorstyle(current_style);
|
2016-10-08 23:47:15 +00:00
|
|
|
}
|
|
|
|
|
2016-10-22 00:18:26 +00:00
|
|
|
fn with_effect<F: FnOnce()>(&self, effect: theme::Effect, f: F) {
|
2016-10-08 23:47:15 +00:00
|
|
|
// TODO: actually use effects
|
|
|
|
// TODO: careful! need to remember the previous state
|
|
|
|
// and apply it back
|
2016-10-24 18:16:56 +00:00
|
|
|
effect.on();
|
2016-10-08 23:47:15 +00:00
|
|
|
f();
|
2016-10-24 18:16:56 +00:00
|
|
|
effect.off();
|
2016-10-08 23:47:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fn has_colors(&self) -> bool {
|
|
|
|
// TODO: color support detection?
|
|
|
|
true
|
|
|
|
}
|
|
|
|
|
|
|
|
fn screen_size(&self) -> (usize, usize) {
|
|
|
|
let (x, y) = termion::terminal_size().unwrap_or((1, 1));
|
|
|
|
(x as usize, y as usize)
|
|
|
|
}
|
|
|
|
|
2016-10-09 23:03:49 +00:00
|
|
|
fn clear(&self) {
|
2016-10-22 00:18:26 +00:00
|
|
|
self.apply_colorstyle(theme::ColorStyle::Background);
|
2016-10-08 23:47:15 +00:00
|
|
|
print!("{}", termion::clear::All);
|
|
|
|
}
|
|
|
|
|
|
|
|
fn refresh(&mut self) {
|
|
|
|
self.terminal.flush().unwrap();
|
|
|
|
}
|
|
|
|
|
|
|
|
fn print_at(&self, (x, y): (usize, usize), text: &str) {
|
|
|
|
// TODO: terminals are 1-based. Should we add 1 here?
|
|
|
|
print!("{}{}", termion::cursor::Goto(x as u16, y as u16), text);
|
|
|
|
}
|
|
|
|
|
|
|
|
fn set_refresh_rate(&mut self, fps: u32) {
|
|
|
|
// TODO: handle async refresh, when no input is entered.
|
|
|
|
// Could be done with a timeout on the event polling,
|
|
|
|
// if it was supportedd.
|
|
|
|
}
|
|
|
|
|
|
|
|
fn poll_event(&self) -> Event {
|
2016-10-22 00:18:26 +00:00
|
|
|
// TODO: parse input
|
2016-10-24 18:16:56 +00:00
|
|
|
if let Some(key) = ::std::io::stdin().keys().next() {
|
|
|
|
match key.unwrap() {
|
|
|
|
TKey::Backspace => Event::Key(Key::Backspace),
|
|
|
|
TKey::Left => Event::Key(Key::Left),
|
|
|
|
TKey::Right => Event::Key(Key::Right),
|
|
|
|
TKey::Up => Event::Key(Key::Up),
|
|
|
|
TKey::Down => Event::Key(Key::Down),
|
|
|
|
TKey::Home => Event::Key(Key::Home),
|
|
|
|
TKey::End => Event::Key(Key::End),
|
|
|
|
TKey::PageUp => Event::Key(Key::PageUp),
|
|
|
|
TKey::PageDown => Event::Key(Key::PageDown),
|
|
|
|
TKey::Delete => Event::Key(Key::Del),
|
|
|
|
TKey::Insert => Event::Key(Key::Ins),
|
|
|
|
TKey::F(i) if i < 12 => Event::Key(Key::from_f(i)),
|
|
|
|
TKey::F(j) => Event::Unknown(-(j as i32)),
|
|
|
|
TKey::Char('\n') => Event::Key(Key::Enter),
|
|
|
|
TKey::Char('\t') => Event::Key(Key::Tab),
|
|
|
|
TKey::Char(c) => Event::Char(c),
|
|
|
|
TKey::Ctrl('c') => panic!("Ctrl-C pressed"),
|
|
|
|
TKey::Ctrl(c) => Event::CtrlChar(c),
|
|
|
|
TKey::Alt(c) => Event::AltChar(c),
|
|
|
|
_ => Event::Unknown(-1),
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
Event::Refresh
|
|
|
|
}
|
2016-10-08 23:47:15 +00:00
|
|
|
}
|
|
|
|
}
|
2016-10-22 00:18:26 +00:00
|
|
|
|
|
|
|
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))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|