panorama/src/ui/mod.rs

63 lines
1.7 KiB
Rust
Raw Normal View History

2021-03-01 08:20:21 +00:00
//! UI library
2021-02-14 12:27:13 +00:00
2021-03-01 08:20:21 +00:00
mod events;
2021-02-12 13:21:00 +00:00
2021-03-01 08:20:21 +00:00
use std::io::Stdout;
2021-02-12 08:54:19 +00:00
use std::time::Duration;
2021-02-12 08:12:43 +00:00
use anyhow::Result;
2021-03-01 08:20:21 +00:00
use termion::{event::Key, screen::AlternateScreen};
use tokio::sync::mpsc;
use tui::{
backend::TermionBackend,
layout::{Constraint, Direction, Layout},
widgets::{Block, Borders, Widget},
Terminal,
2021-02-12 08:12:43 +00:00
};
2021-03-01 08:20:21 +00:00
use self::events::{Config, Event, Events};
2021-02-12 08:12:43 +00:00
2021-03-01 08:20:21 +00:00
/// Main entrypoint for the UI
pub async fn run_ui(stdout: Stdout, exit_tx: mpsc::Sender<()>) -> Result<()> {
let stdout = AlternateScreen::from(stdout);
let backend = TermionBackend::new(stdout);
let mut terminal = Terminal::new(backend)?;
2021-02-12 13:21:00 +00:00
2021-03-01 08:20:21 +00:00
let events = Events::with_config(Config {
tick_rate: Duration::from_millis(17),
..Config::default()
});
2021-02-27 06:27:53 +00:00
2021-02-12 08:54:19 +00:00
loop {
2021-03-01 08:20:21 +00:00
terminal.draw(|f| {
let chunks = Layout::default()
.direction(Direction::Vertical)
.margin(1)
.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[1]);
})?;
if let Event::Input(input) = events.next()? {
match input {
Key::Char('q') => {
break;
}
_ => {}
2021-02-12 08:12:43 +00:00
}
2021-03-01 08:20:21 +00:00
}
2021-02-12 08:12:43 +00:00
}
2021-02-12 08:54:19 +00:00
Ok(())
2021-02-12 08:12:43 +00:00
}