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},
|
2020-03-13 01:02:51 +00:00
|
|
|
layout::{Constraint, Direction, Layout},
|
|
|
|
widgets::{Block, Borders},
|
2021-11-01 21:27:46 +00:00
|
|
|
Frame, Terminal,
|
2020-03-13 01:02:51 +00:00
|
|
|
};
|
2018-09-23 18:59:51 +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)?;
|
2018-04-01 15:57:26 +00:00
|
|
|
|
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-04-01 15:57:26 +00:00
|
|
|
|
2021-11-01 21:27:46 +00:00
|
|
|
fn run_app<B: Backend>(terminal: &mut Terminal<B>) -> io::Result<()> {
|
2018-04-01 15:57:26 +00:00
|
|
|
loop {
|
2021-11-01 21:27:46 +00:00
|
|
|
terminal.draw(|f| ui(f))?;
|
|
|
|
|
|
|
|
if let Event::Key(key) = event::read()? {
|
|
|
|
match key.code {
|
|
|
|
KeyCode::Char('q') => return Ok(()),
|
|
|
|
_ => {}
|
2018-12-07 15:17:33 +00:00
|
|
|
}
|
2018-04-01 15:57:26 +00:00
|
|
|
}
|
|
|
|
}
|
2021-11-01 21:27:46 +00:00
|
|
|
}
|
2018-04-01 15:57:26 +00:00
|
|
|
|
2021-11-01 21:27:46 +00:00
|
|
|
fn ui<B: Backend>(f: &mut Frame<B>) {
|
|
|
|
let chunks = Layout::default()
|
|
|
|
.direction(Direction::Vertical)
|
|
|
|
.constraints(
|
|
|
|
[
|
|
|
|
Constraint::Percentage(10),
|
|
|
|
Constraint::Percentage(80),
|
|
|
|
Constraint::Percentage(10),
|
|
|
|
]
|
|
|
|
.as_ref(),
|
|
|
|
)
|
|
|
|
.split(f.size());
|
|
|
|
|
|
|
|
let block = Block::default().title("Block").borders(Borders::ALL);
|
|
|
|
f.render_widget(block, chunks[0]);
|
|
|
|
let block = Block::default().title("Block 2").borders(Borders::ALL);
|
|
|
|
f.render_widget(block, chunks[2]);
|
2018-04-01 15:57:26 +00:00
|
|
|
}
|