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};
|
|
|
|
use std::io::Write;
|
2016-10-10 21:08:07 +00:00
|
|
|
use self::termion::input::TermRead;
|
|
|
|
use self::termion::raw::IntoRawMode;
|
2016-10-08 23:47:15 +00:00
|
|
|
use ::theme::{BaseColor, Color, ColorStyle, Effect};
|
|
|
|
|
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-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-09 23:03:49 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
backend.clear();
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
fn init_color_style(&mut self, style: ColorStyle, foreground: &Color,
|
|
|
|
background: &Color) {
|
|
|
|
// Do we _need_ to save the color?
|
|
|
|
}
|
|
|
|
|
|
|
|
fn with_color<F: FnOnce()>(&self, color: ColorStyle, f: F) {
|
|
|
|
// TODO: actually use colors
|
|
|
|
// TODO: careful! need to remember the previous state
|
|
|
|
// and apply it back
|
|
|
|
f();
|
|
|
|
}
|
|
|
|
|
|
|
|
fn with_effect<F: FnOnce()>(&self, effect: Effect, f: F) {
|
|
|
|
// TODO: actually use effects
|
|
|
|
// TODO: careful! need to remember the previous state
|
|
|
|
// and apply it back
|
|
|
|
f();
|
|
|
|
}
|
|
|
|
|
|
|
|
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-08 23:47:15 +00:00
|
|
|
print!("{}", termion::clear::All);
|
|
|
|
}
|
|
|
|
|
|
|
|
fn refresh(&mut self) {
|
|
|
|
// Not sure termion needs a refresh phase
|
|
|
|
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 {
|
|
|
|
::std::io::stdin().keys().next();
|
|
|
|
Event::Key(Key::Enter)
|
|
|
|
}
|
|
|
|
}
|