2018-09-23 18:59:51 +00:00
|
|
|
#[allow(dead_code)]
|
|
|
|
mod util;
|
2016-10-15 22:38:20 +00:00
|
|
|
|
2019-01-06 11:57:06 +00:00
|
|
|
use crate::util::event::{Event, Events};
|
2020-03-13 01:02:51 +00:00
|
|
|
use std::{error::Error, io};
|
|
|
|
use termion::{event::Key, input::MouseTerminal, raw::IntoRawMode, screen::AlternateScreen};
|
|
|
|
use tui::{
|
|
|
|
backend::TermionBackend,
|
|
|
|
layout::{Constraint, Direction, Layout},
|
2020-07-11 17:08:28 +00:00
|
|
|
style::{Color, Modifier, Style},
|
2020-05-10 13:44:30 +00:00
|
|
|
text::Span,
|
2020-03-13 01:02:51 +00:00
|
|
|
widgets::{Block, BorderType, Borders},
|
|
|
|
Terminal,
|
|
|
|
};
|
2017-05-08 19:34:41 +00:00
|
|
|
|
2020-03-13 01:02:51 +00:00
|
|
|
fn main() -> Result<(), Box<dyn Error>> {
|
2018-09-23 18:59:51 +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)?;
|
|
|
|
|
|
|
|
// Setup event handlers
|
|
|
|
let events = Events::new();
|
|
|
|
|
|
|
|
loop {
|
2020-06-15 20:57:23 +00:00
|
|
|
terminal.draw(|f| {
|
2018-09-23 18:59:51 +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
|
2018-12-07 14:43:05 +00:00
|
|
|
let size = f.size();
|
2019-12-15 20:38:18 +00:00
|
|
|
let block = Block::default()
|
2019-12-15 21:50:18 +00:00
|
|
|
.borders(Borders::ALL)
|
|
|
|
.title("Main block with round corners")
|
2019-12-15 20:38:18 +00:00
|
|
|
.border_type(BorderType::Rounded);
|
|
|
|
f.render_widget(block, size);
|
2018-08-12 17:44:52 +00:00
|
|
|
let chunks = Layout::default()
|
2018-09-23 18:59:51 +00:00
|
|
|
.direction(Direction::Vertical)
|
|
|
|
.margin(4)
|
2018-08-12 17:44:52 +00:00
|
|
|
.constraints([Constraint::Percentage(50), Constraint::Percentage(50)].as_ref())
|
2018-12-07 14:43:05 +00:00
|
|
|
.split(f.size());
|
2020-05-10 13:44:30 +00:00
|
|
|
|
|
|
|
let top_chunks = Layout::default()
|
|
|
|
.direction(Direction::Horizontal)
|
|
|
|
.constraints([Constraint::Percentage(50), Constraint::Percentage(50)].as_ref())
|
|
|
|
.split(chunks[0]);
|
|
|
|
let block = Block::default()
|
|
|
|
.title(vec![
|
2020-07-11 17:08:28 +00:00
|
|
|
Span::styled("With", Style::default().fg(Color::Yellow)),
|
2020-05-10 13:44:30 +00:00
|
|
|
Span::from(" background"),
|
|
|
|
])
|
|
|
|
.style(Style::default().bg(Color::Green));
|
|
|
|
f.render_widget(block, top_chunks[0]);
|
|
|
|
|
|
|
|
let block = Block::default().title(Span::styled(
|
|
|
|
"Styled title",
|
2020-07-11 17:08:28 +00:00
|
|
|
Style::default()
|
2019-12-15 20:38:18 +00:00
|
|
|
.fg(Color::White)
|
|
|
|
.bg(Color::Red)
|
2020-05-10 13:44:30 +00:00
|
|
|
.add_modifier(Modifier::BOLD),
|
|
|
|
));
|
|
|
|
f.render_widget(block, top_chunks[1]);
|
|
|
|
|
|
|
|
let bottom_chunks = Layout::default()
|
|
|
|
.direction(Direction::Horizontal)
|
|
|
|
.constraints([Constraint::Percentage(50), Constraint::Percentage(50)].as_ref())
|
|
|
|
.split(chunks[1]);
|
|
|
|
let block = Block::default().title("With borders").borders(Borders::ALL);
|
|
|
|
f.render_widget(block, bottom_chunks[0]);
|
|
|
|
let block = Block::default()
|
|
|
|
.title("With styled borders and doubled borders")
|
|
|
|
.border_style(Style::default().fg(Color::Cyan))
|
|
|
|
.borders(Borders::LEFT | Borders::RIGHT)
|
|
|
|
.border_type(BorderType::Double);
|
|
|
|
f.render_widget(block, bottom_chunks[1]);
|
2018-09-23 18:59:51 +00:00
|
|
|
})?;
|
|
|
|
|
2020-05-16 22:51:57 +00:00
|
|
|
if let Event::Input(key) = events.next()? {
|
|
|
|
if key == Key::Char('q') {
|
|
|
|
break;
|
2018-12-07 15:17:33 +00:00
|
|
|
}
|
2018-08-12 17:44:52 +00:00
|
|
|
}
|
2018-09-23 18:59:51 +00:00
|
|
|
}
|
|
|
|
Ok(())
|
2016-10-15 22:38:20 +00:00
|
|
|
}
|