ratatui/examples/layout.rs

64 lines
1.7 KiB
Rust
Raw Normal View History

#[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};
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
2019-01-06 11:57:06 +00:00
use crate::util::event::{Event, Events};
fn main() -> Result<(), failure::Error> {
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
loop {
terminal.draw(|mut f| {
let chunks = Layout::default()
.direction(Direction::Vertical)
.constraints(
[
Constraint::Percentage(10),
Constraint::Percentage(80),
Constraint::Percentage(10),
]
.as_ref(),
)
.split(f.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 {
break;
}
}
_ => {}
2018-04-01 15:57:26 +00:00
}
}
Ok(())
2018-04-01 15:57:26 +00:00
}