ratatui/examples/layout.rs

90 lines
2.1 KiB
Rust
Raw Normal View History

extern crate failure;
2018-04-01 15:57:26 +00:00
extern crate log;
extern crate stderrlog;
extern crate termion;
extern crate tui;
#[allow(dead_code)]
mod util;
2018-04-01 15:57:26 +00:00
use std::io;
2018-04-01 15:57:26 +00:00
use termion::event::Key;
use termion::input::MouseTerminal;
use termion::raw::IntoRawMode;
use termion::screen::AlternateScreen;
use tui::backend::TermionBackend;
use tui::layout::{Constraint, Direction, Layout, Rect};
2018-05-06 10:59:24 +00:00
use tui::widgets::{Block, Borders, Widget};
2018-06-09 09:02:37 +00:00
use tui::Terminal;
2018-04-01 15:57:26 +00:00
use util::event::{Event, Events};
2018-04-01 15:57:26 +00:00
struct App {
size: Rect,
}
impl App {
fn new() -> App {
App {
size: Rect::default(),
}
}
}
fn main() -> Result<(), failure::Error> {
stderrlog::new().verbosity(4).init()?;
2018-04-01 15:57:26 +00:00
// 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)?;
terminal.hide_cursor()?;
2018-04-01 15:57:26 +00:00
let events = Events::new();
2018-04-01 15:57:26 +00:00
// App
let mut app = App::new();
loop {
let size = terminal.size()?;
2018-04-01 15:57:26 +00:00
if size != app.size {
terminal.resize(size)?;
2018-04-01 15:57:26 +00:00
app.size = size;
}
terminal.draw(|mut f| {
let chunks = Layout::default()
.direction(Direction::Vertical)
.constraints(
[
Constraint::Percentage(10),
Constraint::Percentage(80),
Constraint::Percentage(10),
]
.as_ref(),
).split(app.size);
Block::default()
.title("Block")
.borders(Borders::ALL)
.render(&mut f, chunks[0]);
Block::default()
.title("Block 2")
.borders(Borders::ALL)
.render(&mut f, chunks[2]);
})?;
match events.next()? {
Event::Input(input) => if let Key::Char('q') = input {
2018-08-12 22:27:56 +00:00
break;
2018-04-01 15:57:26 +00:00
},
_ => {}
2018-04-01 15:57:26 +00:00
}
}
Ok(())
2018-04-01 15:57:26 +00:00
}