2016-10-15 22:38:20 +00:00
|
|
|
extern crate termion;
|
2017-10-30 21:28:37 +00:00
|
|
|
extern crate tui;
|
2016-10-15 22:38:20 +00:00
|
|
|
|
|
|
|
use std::io;
|
|
|
|
use termion::event;
|
|
|
|
use termion::input::TermRead;
|
|
|
|
|
2016-11-28 08:52:51 +00:00
|
|
|
use tui::Terminal;
|
2017-09-03 13:34:05 +00:00
|
|
|
use tui::backend::MouseBackend;
|
2017-12-26 16:09:04 +00:00
|
|
|
use tui::widgets::{Block, Borders, Widget};
|
2017-10-30 21:28:37 +00:00
|
|
|
use tui::layout::{Direction, Group, Rect, Size};
|
|
|
|
use tui::style::{Color, Modifier, Style};
|
2016-10-15 22:38:20 +00:00
|
|
|
|
|
|
|
fn main() {
|
2017-09-03 13:34:05 +00:00
|
|
|
let mut terminal = Terminal::new(MouseBackend::new().unwrap()).unwrap();
|
2016-10-15 22:38:20 +00:00
|
|
|
let stdin = io::stdin();
|
2016-11-03 22:59:04 +00:00
|
|
|
terminal.clear().unwrap();
|
|
|
|
terminal.hide_cursor().unwrap();
|
2017-05-08 19:34:41 +00:00
|
|
|
|
|
|
|
let mut term_size = terminal.size().unwrap();
|
|
|
|
draw(&mut terminal, &term_size);
|
2016-10-15 22:38:20 +00:00
|
|
|
for c in stdin.keys() {
|
2017-05-08 19:34:41 +00:00
|
|
|
let size = terminal.size().unwrap();
|
|
|
|
if term_size != size {
|
|
|
|
terminal.resize(size).unwrap();
|
|
|
|
term_size = size;
|
|
|
|
}
|
|
|
|
draw(&mut terminal, &term_size);
|
2016-10-15 22:38:20 +00:00
|
|
|
let evt = c.unwrap();
|
|
|
|
if evt == event::Key::Char('q') {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2016-11-03 22:59:04 +00:00
|
|
|
terminal.show_cursor().unwrap();
|
2016-10-15 22:38:20 +00:00
|
|
|
}
|
|
|
|
|
2017-09-03 13:34:05 +00:00
|
|
|
fn draw(t: &mut Terminal<MouseBackend>, size: &Rect) {
|
2016-11-07 23:35:46 +00:00
|
|
|
// Wrapping block for a group
|
|
|
|
// Just draw the block and the group on the same area and build the group
|
|
|
|
// with at least a margin of 1
|
2017-12-26 15:55:13 +00:00
|
|
|
Block::default().borders(Borders::ALL).render(t, size);
|
2016-10-15 22:38:20 +00:00
|
|
|
Group::default()
|
|
|
|
.direction(Direction::Vertical)
|
2016-11-07 23:35:46 +00:00
|
|
|
.margin(4)
|
|
|
|
.sizes(&[Size::Percent(50), Size::Percent(50)])
|
2017-05-21 09:13:24 +00:00
|
|
|
.render(t, size, |t, chunks| {
|
2016-10-15 22:38:20 +00:00
|
|
|
Group::default()
|
2016-11-02 16:08:52 +00:00
|
|
|
.direction(Direction::Horizontal)
|
2016-11-07 23:35:46 +00:00
|
|
|
.sizes(&[Size::Percent(50), Size::Percent(50)])
|
|
|
|
.render(t, &chunks[0], |t, chunks| {
|
2016-11-06 20:41:32 +00:00
|
|
|
Block::default()
|
2016-11-07 23:35:46 +00:00
|
|
|
.title("With background")
|
2016-11-06 20:41:32 +00:00
|
|
|
.title_style(Style::default().fg(Color::Yellow))
|
2016-11-07 23:35:46 +00:00
|
|
|
.style(Style::default().bg(Color::Green))
|
|
|
|
.render(t, &chunks[0]);
|
|
|
|
Block::default()
|
|
|
|
.title("Styled title")
|
2017-10-30 21:28:37 +00:00
|
|
|
.title_style(
|
|
|
|
Style::default()
|
|
|
|
.fg(Color::White)
|
|
|
|
.bg(Color::Red)
|
|
|
|
.modifier(Modifier::Bold),
|
|
|
|
)
|
2016-11-07 23:35:46 +00:00
|
|
|
.render(t, &chunks[1]);
|
|
|
|
});
|
|
|
|
Group::default()
|
|
|
|
.direction(Direction::Horizontal)
|
|
|
|
.sizes(&[Size::Percent(50), Size::Percent(50)])
|
|
|
|
.render(t, &chunks[1], |t, chunks| {
|
|
|
|
Block::default()
|
|
|
|
.title("With borders")
|
2017-12-26 15:55:13 +00:00
|
|
|
.borders(Borders::ALL)
|
2016-11-06 20:41:32 +00:00
|
|
|
.render(t, &chunks[0]);
|
2016-11-02 16:08:52 +00:00
|
|
|
Block::default()
|
2016-11-07 23:35:46 +00:00
|
|
|
.title("With styled borders")
|
2016-11-06 20:41:32 +00:00
|
|
|
.border_style(Style::default().fg(Color::Cyan))
|
2017-12-26 15:55:13 +00:00
|
|
|
.borders(Borders::LEFT | Borders::RIGHT)
|
2016-11-06 17:49:57 +00:00
|
|
|
.render(t, &chunks[1]);
|
2016-10-15 22:38:20 +00:00
|
|
|
});
|
|
|
|
});
|
2016-11-02 16:08:52 +00:00
|
|
|
|
2016-11-03 22:59:04 +00:00
|
|
|
t.draw().unwrap();
|
2016-10-15 22:38:20 +00:00
|
|
|
}
|