2018-09-23 18:59:51 +00:00
|
|
|
extern crate failure;
|
|
|
|
extern crate termion;
|
2016-11-02 18:17:18 +00:00
|
|
|
extern crate tui;
|
|
|
|
|
2018-09-23 18:59:51 +00:00
|
|
|
#[allow(dead_code)]
|
|
|
|
mod util;
|
|
|
|
|
|
|
|
use std::io;
|
|
|
|
|
|
|
|
use termion::event::Key;
|
|
|
|
use termion::input::MouseTerminal;
|
|
|
|
use termion::raw::IntoRawMode;
|
|
|
|
use termion::screen::AlternateScreen;
|
|
|
|
use tui::backend::TermionBackend;
|
2016-11-02 18:17:18 +00:00
|
|
|
use tui::buffer::Buffer;
|
|
|
|
use tui::layout::Rect;
|
2016-11-06 20:41:32 +00:00
|
|
|
use tui::style::Style;
|
2018-05-06 10:59:24 +00:00
|
|
|
use tui::widgets::Widget;
|
2018-06-09 09:02:37 +00:00
|
|
|
use tui::Terminal;
|
2016-11-02 18:17:18 +00:00
|
|
|
|
2018-09-23 18:59:51 +00:00
|
|
|
use util::event::{Event, Events};
|
|
|
|
|
|
|
|
struct App {
|
|
|
|
size: Rect,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Default for App {
|
|
|
|
fn default() -> App {
|
|
|
|
App {
|
|
|
|
size: Rect::default(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-11-02 18:17:18 +00:00
|
|
|
struct Label<'a> {
|
|
|
|
text: &'a str,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'a> Default for Label<'a> {
|
|
|
|
fn default() -> Label<'a> {
|
|
|
|
Label { text: "" }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'a> Widget for Label<'a> {
|
2018-08-12 22:27:56 +00:00
|
|
|
fn draw(&mut self, area: Rect, buf: &mut Buffer) {
|
2018-09-07 20:24:52 +00:00
|
|
|
buf.set_string(area.left(), area.top(), self.text, Style::default());
|
2016-11-02 18:17:18 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'a> Label<'a> {
|
|
|
|
fn text(&mut self, text: &'a str) -> &mut Label<'a> {
|
|
|
|
self.text = text;
|
|
|
|
self
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-09-23 18:59:51 +00:00
|
|
|
fn main() -> Result<(), failure::Error> {
|
|
|
|
// Terminal initialization
|
|
|
|
let stdout = io::stdout().into_raw_mode()?;
|
|
|
|
let stdout = MouseTerminal::from(stdout);
|
|
|
|
let stdout = AlternateScreen::from(stdout);
|
|
|
|
let backend = TermionBackend::new(stdout);
|
|
|
|
let mut terminal = Terminal::new(backend)?;
|
|
|
|
|
|
|
|
let events = Events::new();
|
|
|
|
|
|
|
|
let mut app = App::default();
|
|
|
|
|
|
|
|
loop {
|
|
|
|
let size = terminal.size()?;
|
|
|
|
if app.size != size {
|
|
|
|
terminal.resize(size)?;
|
|
|
|
app.size = size;
|
|
|
|
}
|
|
|
|
|
|
|
|
terminal.draw(|mut f| {
|
|
|
|
Label::default().text("Test").render(&mut f, app.size);
|
|
|
|
})?;
|
|
|
|
|
|
|
|
match events.next()? {
|
|
|
|
Event::Input(key) => if key == Key::Char('q') {
|
|
|
|
break;
|
|
|
|
},
|
|
|
|
_ => {}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Ok(())
|
2016-11-02 18:17:18 +00:00
|
|
|
}
|