tui-rs/examples/rustbox.rs

47 lines
1.4 KiB
Rust
Raw Normal View History

2016-11-06 17:49:57 +00:00
use rustbox::Key;
2018-05-06 10:59:24 +00:00
use std::error::Error;
2016-11-06 17:49:57 +00:00
use tui::backend::RustboxBackend;
2017-10-30 21:28:37 +00:00
use tui::style::{Color, Modifier, Style};
2018-11-24 18:42:28 +00:00
use tui::widgets::{Block, Borders, Paragraph, Text, Widget};
2018-06-09 09:02:37 +00:00
use tui::Terminal;
2016-11-06 17:49:57 +00:00
2018-11-24 18:42:28 +00:00
fn main() -> Result<(), failure::Error> {
2016-11-06 17:49:57 +00:00
let mut terminal = Terminal::new(RustboxBackend::new().unwrap()).unwrap();
terminal.clear().unwrap();
terminal.hide_cursor().unwrap();
loop {
2018-11-24 18:42:28 +00:00
draw(&mut terminal)?;
2016-11-06 17:49:57 +00:00
match terminal.backend().rustbox().poll_event(false) {
Ok(rustbox::Event::KeyEvent(key)) => {
if key == Key::Char('q') {
break;
}
}
2016-11-06 17:49:57 +00:00
Err(e) => panic!("{}", e.description()),
_ => {}
};
}
2018-11-24 18:42:28 +00:00
terminal.show_cursor()?;
Ok(())
2016-11-06 17:49:57 +00:00
}
2018-11-24 18:42:28 +00:00
fn draw(t: &mut Terminal<RustboxBackend>) -> Result<(), std::io::Error> {
let text = [
Text::raw("It "),
Text::styled("works", Style::default().fg(Color::Yellow)),
];
t.draw(|mut f| {
let size = f.size();
2018-11-24 18:42:28 +00:00
Paragraph::new(text.iter())
.block(
Block::default()
2018-08-12 22:27:56 +00:00
.title("Rustbox backend")
.title_style(Style::default().fg(Color::Yellow).modifier(Modifier::Bold))
.borders(Borders::ALL)
.border_style(Style::default().fg(Color::Magenta)),
)
.render(&mut f, size)
2018-11-24 18:42:28 +00:00
})
2016-11-06 17:49:57 +00:00
}