From 07846f996fd54e2bd6c7576f942e90361c234fc4 Mon Sep 17 00:00:00 2001 From: Michael Zhang Date: Mon, 1 Mar 2021 03:23:05 -0600 Subject: [PATCH] got my quit back --- src/ui/mod.rs | 62 +++++++++++++++++++++++++++++++++++++++++---------- 1 file changed, 50 insertions(+), 12 deletions(-) diff --git a/src/ui/mod.rs b/src/ui/mod.rs index 6516759..f9701d2 100644 --- a/src/ui/mod.rs +++ b/src/ui/mod.rs @@ -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(()) }