got my quit back

This commit is contained in:
Michael Zhang 2021-03-01 03:23:05 -06:00
parent e5515f6c7c
commit 07846f996f
Signed by: michael
GPG key ID: BDA47A31A3C8EE6B

View file

@ -1,33 +1,39 @@
//! UI library
use std::io::Stdout;
use std::mem;
use std::time::Duration;
use anyhow::Result;
use tokio::sync::mpsc;
use crossterm::{
cursor,
event::{self, Event, KeyCode, KeyEvent},
style, terminal,
};
use tokio::{sync::mpsc, time};
use tui::{
text::Spans,
backend::CrosstermBackend,
layout::{Constraint, Direction, Layout},
text::Spans,
widgets::*,
Terminal,
};
/// Main entrypoint for the UI
pub async fn run_ui(stdout: Stdout, exit_tx: mpsc::Sender<()>) -> Result<()> {
let backend = CrosstermBackend::new(stdout);
let mut terminal = Terminal::new(backend)?;
const FRAME_DURATION: Duration = Duration::from_millis(17);
// let events = Events::with_config(Config {
// tick_rate: Duration::from_millis(17),
// ..Config::default()
// });
/// Main entrypoint for the UI
pub async fn run_ui(mut stdout: Stdout, exit_tx: mpsc::Sender<()>) -> Result<()> {
execute!(stdout, cursor::Hide, terminal::EnterAlternateScreen)?;
terminal::enable_raw_mode()?;
let backend = CrosstermBackend::new(&mut stdout);
let mut terminal = Terminal::new(backend)?;
loop {
terminal.draw(|f| {
let chunks = Layout::default()
.direction(Direction::Vertical)
.margin(1)
.margin(0)
.constraints(
[
Constraint::Percentage(10),
@ -37,7 +43,7 @@ pub async fn run_ui(stdout: Stdout, exit_tx: mpsc::Sender<()>) -> Result<()> {
.as_ref(),
)
.split(f.size());
let block = Block::default().title("Block").borders(Borders::ALL);
let block = Block::default().title("Block").borders(Borders::NONE);
f.render_widget(block, chunks[0]);
let block = Block::default().title("Block 2").borders(Borders::ALL);
f.render_widget(block, chunks[1]);
@ -47,6 +53,26 @@ pub async fn run_ui(stdout: Stdout, exit_tx: mpsc::Sender<()>) -> Result<()> {
f.render_widget(tabs, chunks[2]);
})?;
let event = if event::poll(FRAME_DURATION)? {
let event = event::read()?;
// table.update(&event);
if let Event::Key(KeyEvent {
code: KeyCode::Char('q'),
..
}) = event
{
break;
}
Some(event)
} else {
None
};
// approx 60fps
time::sleep(FRAME_DURATION).await;
// if let Event::Input(input) = events.next()? {
// match input {
// Key::Char('q') => {
@ -57,5 +83,17 @@ pub async fn run_ui(stdout: Stdout, exit_tx: mpsc::Sender<()>) -> Result<()> {
// }
}
mem::drop(terminal);
execute!(
stdout,
style::ResetColor,
cursor::Show,
terminal::LeaveAlternateScreen
)?;
terminal::disable_raw_mode()?;
exit_tx.send(()).await?;
debug!("sent exit");
Ok(())
}