2021-11-01 21:27:46 +00:00
|
|
|
use crossterm::{
|
|
|
|
event::{self, DisableMouseCapture, EnableMouseCapture, Event, KeyCode},
|
|
|
|
execute,
|
|
|
|
terminal::{disable_raw_mode, enable_raw_mode, EnterAlternateScreen, LeaveAlternateScreen},
|
|
|
|
};
|
2020-03-13 01:02:51 +00:00
|
|
|
use std::{error::Error, io};
|
|
|
|
use tui::{
|
2021-11-01 21:27:46 +00:00
|
|
|
backend::{Backend, CrosstermBackend},
|
2021-05-22 14:55:24 +00:00
|
|
|
layout::{Alignment, 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},
|
2021-11-01 21:27:46 +00:00
|
|
|
Frame, Terminal,
|
2020-03-13 01:02:51 +00:00
|
|
|
};
|
2017-05-08 19:34:41 +00:00
|
|
|
|
2020-03-13 01:02:51 +00:00
|
|
|
fn main() -> Result<(), Box<dyn Error>> {
|
2021-11-01 21:27:46 +00:00
|
|
|
// setup terminal
|
|
|
|
enable_raw_mode()?;
|
|
|
|
let mut stdout = io::stdout();
|
|
|
|
execute!(stdout, EnterAlternateScreen, EnableMouseCapture)?;
|
|
|
|
let backend = CrosstermBackend::new(stdout);
|
2018-09-23 18:59:51 +00:00
|
|
|
let mut terminal = Terminal::new(backend)?;
|
|
|
|
|
2021-11-01 21:27:46 +00:00
|
|
|
// create app and run it
|
|
|
|
let res = run_app(&mut terminal);
|
|
|
|
|
|
|
|
// restore terminal
|
|
|
|
disable_raw_mode()?;
|
|
|
|
execute!(
|
|
|
|
terminal.backend_mut(),
|
|
|
|
LeaveAlternateScreen,
|
|
|
|
DisableMouseCapture
|
|
|
|
)?;
|
|
|
|
terminal.show_cursor()?;
|
|
|
|
|
|
|
|
if let Err(err) = res {
|
|
|
|
println!("{:?}", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
2018-09-23 18:59:51 +00:00
|
|
|
|
2021-11-01 21:27:46 +00:00
|
|
|
fn run_app<B: Backend>(terminal: &mut Terminal<B>) -> io::Result<()> {
|
2018-09-23 18:59:51 +00:00
|
|
|
loop {
|
2021-11-01 21:27:46 +00:00
|
|
|
terminal.draw(ui)?;
|
|
|
|
|
|
|
|
if let Event::Key(key) = event::read()? {
|
|
|
|
match key.code {
|
|
|
|
KeyCode::Char('q') => return Ok(()),
|
|
|
|
_ => {}
|
2018-12-07 15:17:33 +00:00
|
|
|
}
|
2018-08-12 17:44:52 +00:00
|
|
|
}
|
2018-09-23 18:59:51 +00:00
|
|
|
}
|
2021-11-01 21:27:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fn ui<B: Backend>(f: &mut Frame<B>) {
|
|
|
|
// 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
|
|
|
|
let size = f.size();
|
|
|
|
|
|
|
|
// Surrounding block
|
|
|
|
let block = Block::default()
|
|
|
|
.borders(Borders::ALL)
|
|
|
|
.title("Main block with round corners")
|
|
|
|
.title_alignment(Alignment::Center)
|
|
|
|
.border_type(BorderType::Rounded);
|
|
|
|
f.render_widget(block, size);
|
|
|
|
|
|
|
|
let chunks = Layout::default()
|
|
|
|
.direction(Direction::Vertical)
|
|
|
|
.margin(4)
|
|
|
|
.constraints([Constraint::Percentage(50), Constraint::Percentage(50)].as_ref())
|
|
|
|
.split(f.size());
|
|
|
|
|
|
|
|
// Top two inner blocks
|
|
|
|
let top_chunks = Layout::default()
|
|
|
|
.direction(Direction::Horizontal)
|
|
|
|
.constraints([Constraint::Percentage(50), Constraint::Percentage(50)].as_ref())
|
|
|
|
.split(chunks[0]);
|
|
|
|
|
|
|
|
// Top left inner block with green background
|
|
|
|
let block = Block::default()
|
|
|
|
.title(vec![
|
|
|
|
Span::styled("With", Style::default().fg(Color::Yellow)),
|
|
|
|
Span::from(" background"),
|
|
|
|
])
|
|
|
|
.style(Style::default().bg(Color::Green));
|
|
|
|
f.render_widget(block, top_chunks[0]);
|
|
|
|
|
|
|
|
// Top right inner block with styled title aligned to the right
|
|
|
|
let block = Block::default()
|
|
|
|
.title(Span::styled(
|
|
|
|
"Styled title",
|
|
|
|
Style::default()
|
|
|
|
.fg(Color::White)
|
|
|
|
.bg(Color::Red)
|
|
|
|
.add_modifier(Modifier::BOLD),
|
|
|
|
))
|
|
|
|
.title_alignment(Alignment::Right);
|
|
|
|
f.render_widget(block, top_chunks[1]);
|
|
|
|
|
|
|
|
// Bottom two inner blocks
|
|
|
|
let bottom_chunks = Layout::default()
|
|
|
|
.direction(Direction::Horizontal)
|
|
|
|
.constraints([Constraint::Percentage(50), Constraint::Percentage(50)].as_ref())
|
|
|
|
.split(chunks[1]);
|
|
|
|
|
|
|
|
// Bottom left block with all default borders
|
|
|
|
let block = Block::default().title("With borders").borders(Borders::ALL);
|
|
|
|
f.render_widget(block, bottom_chunks[0]);
|
|
|
|
|
|
|
|
// Bottom right block with styled left and right border
|
|
|
|
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]);
|
2016-10-15 22:38:20 +00:00
|
|
|
}
|