ratatui/examples/layout.rs

111 lines
2.4 KiB
Rust
Raw Normal View History

2018-04-01 15:57:26 +00:00
extern crate log;
extern crate stderrlog;
extern crate termion;
extern crate tui;
use std::io;
use std::sync::mpsc;
2018-05-06 10:59:24 +00:00
use std::thread;
2018-04-01 15:57:26 +00:00
use termion::event;
use termion::input::TermRead;
use tui::backend::MouseBackend;
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
struct App {
size: Rect,
}
impl App {
fn new() -> App {
App {
size: Rect::default(),
}
}
}
enum Event {
Input(event::Key),
}
fn main() {
stderrlog::new().verbosity(4).init().unwrap();
// Terminal initialization
let backend = MouseBackend::new().unwrap();
let mut terminal = Terminal::new(backend).unwrap();
// Channels
let (tx, rx) = mpsc::channel();
let input_tx = tx.clone();
// Input
thread::spawn(move || {
let stdin = io::stdin();
for c in stdin.keys() {
let evt = c.unwrap();
input_tx.send(Event::Input(evt)).unwrap();
if evt == event::Key::Char('q') {
break;
}
}
});
// App
let mut app = App::new();
// First draw call
terminal.clear().unwrap();
terminal.hide_cursor().unwrap();
app.size = terminal.size().unwrap();
draw(&mut terminal, &app);
loop {
let size = terminal.size().unwrap();
if size != app.size {
terminal.resize(size).unwrap();
app.size = size;
}
let evt = rx.recv().unwrap();
match evt {
2018-08-12 22:27:56 +00:00
Event::Input(input) => if let event::Key::Char('q') = input {
break;
2018-04-01 15:57:26 +00:00
},
}
draw(&mut terminal, &app);
}
terminal.show_cursor().unwrap();
}
fn draw(t: &mut Terminal<MouseBackend>, app: &App) {
{
let mut f = t.get_frame();
let chunks = Layout::default()
.direction(Direction::Vertical)
.constraints(
[
Constraint::Percentage(10),
Constraint::Percentage(80),
Constraint::Percentage(10),
].as_ref(),
)
2018-08-12 22:27:56 +00:00
.split(app.size);
Block::default()
.title("Block")
.borders(Borders::ALL)
2018-08-12 22:27:56 +00:00
.render(&mut f, chunks[0]);
Block::default()
.title("Block 2")
.borders(Borders::ALL)
2018-08-12 22:27:56 +00:00
.render(&mut f, chunks[2]);
}
2018-04-01 15:57:26 +00:00
t.draw().unwrap();
}