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;
|
|
|
|
|
2017-09-03 13:34:05 +00:00
|
|
|
use tui::backend::MouseBackend;
|
2018-08-12 17:44:52 +00:00
|
|
|
use tui::layout::{Constraint, Direction, Layout, Rect};
|
2017-10-30 21:28:37 +00:00
|
|
|
use tui::style::{Color, Modifier, Style};
|
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;
|
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();
|
2018-09-01 12:05:33 +00:00
|
|
|
draw(&mut terminal, term_size).unwrap();
|
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;
|
|
|
|
}
|
2018-09-01 12:05:33 +00:00
|
|
|
draw(&mut terminal, term_size).unwrap();
|
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
|
|
|
}
|
|
|
|
|
2018-09-01 12:05:33 +00:00
|
|
|
fn draw(t: &mut Terminal<MouseBackend>, size: Rect) -> Result<(), io::Error> {
|
|
|
|
t.draw(|mut f| {
|
2018-08-12 17:44:52 +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
|
|
|
|
Block::default().borders(Borders::ALL).render(&mut f, size);
|
|
|
|
let chunks = Layout::default()
|
|
|
|
.direction(Direction::Vertical)
|
|
|
|
.margin(4)
|
|
|
|
.constraints([Constraint::Percentage(50), Constraint::Percentage(50)].as_ref())
|
|
|
|
.split(size);
|
|
|
|
{
|
|
|
|
let chunks = Layout::default()
|
2016-11-02 16:08:52 +00:00
|
|
|
.direction(Direction::Horizontal)
|
2018-08-12 17:44:52 +00:00
|
|
|
.constraints([Constraint::Percentage(50), Constraint::Percentage(50)].as_ref())
|
2018-08-12 22:27:56 +00:00
|
|
|
.split(chunks[0]);
|
2018-08-12 17:44:52 +00:00
|
|
|
Block::default()
|
|
|
|
.title("With background")
|
|
|
|
.title_style(Style::default().fg(Color::Yellow))
|
|
|
|
.style(Style::default().bg(Color::Green))
|
2018-08-12 22:27:56 +00:00
|
|
|
.render(&mut f, chunks[0]);
|
2018-08-12 17:44:52 +00:00
|
|
|
Block::default()
|
|
|
|
.title("Styled title")
|
|
|
|
.title_style(
|
|
|
|
Style::default()
|
|
|
|
.fg(Color::White)
|
|
|
|
.bg(Color::Red)
|
|
|
|
.modifier(Modifier::Bold),
|
|
|
|
)
|
2018-08-12 22:27:56 +00:00
|
|
|
.render(&mut f, chunks[1]);
|
2018-08-12 17:44:52 +00:00
|
|
|
}
|
|
|
|
{
|
|
|
|
let chunks = Layout::default()
|
2016-11-07 23:35:46 +00:00
|
|
|
.direction(Direction::Horizontal)
|
2018-08-12 17:44:52 +00:00
|
|
|
.constraints([Constraint::Percentage(50), Constraint::Percentage(50)].as_ref())
|
2018-08-12 22:27:56 +00:00
|
|
|
.split(chunks[1]);
|
2018-08-12 17:44:52 +00:00
|
|
|
Block::default()
|
|
|
|
.title("With borders")
|
|
|
|
.borders(Borders::ALL)
|
2018-08-12 22:27:56 +00:00
|
|
|
.render(&mut f, chunks[0]);
|
2018-08-12 17:44:52 +00:00
|
|
|
Block::default()
|
|
|
|
.title("With styled borders")
|
|
|
|
.border_style(Style::default().fg(Color::Cyan))
|
|
|
|
.borders(Borders::LEFT | Borders::RIGHT)
|
2018-08-12 22:27:56 +00:00
|
|
|
.render(&mut f, chunks[1]);
|
2018-08-12 17:44:52 +00:00
|
|
|
}
|
2018-09-01 12:05:33 +00:00
|
|
|
})
|
2016-10-15 22:38:20 +00:00
|
|
|
}
|