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 //! UI library
use std::io::Stdout; use std::io::Stdout;
use std::mem;
use std::time::Duration; use std::time::Duration;
use anyhow::Result; use anyhow::Result;
use tokio::sync::mpsc; use crossterm::{
cursor,
event::{self, Event, KeyCode, KeyEvent},
style, terminal,
};
use tokio::{sync::mpsc, time};
use tui::{ use tui::{
text::Spans,
backend::CrosstermBackend, backend::CrosstermBackend,
layout::{Constraint, Direction, Layout}, layout::{Constraint, Direction, Layout},
text::Spans,
widgets::*, widgets::*,
Terminal, Terminal,
}; };
/// Main entrypoint for the UI const FRAME_DURATION: Duration = Duration::from_millis(17);
pub async fn run_ui(stdout: Stdout, exit_tx: mpsc::Sender<()>) -> Result<()> {
let backend = CrosstermBackend::new(stdout);
let mut terminal = Terminal::new(backend)?;
// let events = Events::with_config(Config { /// Main entrypoint for the UI
// tick_rate: Duration::from_millis(17), pub async fn run_ui(mut stdout: Stdout, exit_tx: mpsc::Sender<()>) -> Result<()> {
// ..Config::default() execute!(stdout, cursor::Hide, terminal::EnterAlternateScreen)?;
// }); terminal::enable_raw_mode()?;
let backend = CrosstermBackend::new(&mut stdout);
let mut terminal = Terminal::new(backend)?;
loop { loop {
terminal.draw(|f| { terminal.draw(|f| {
let chunks = Layout::default() let chunks = Layout::default()
.direction(Direction::Vertical) .direction(Direction::Vertical)
.margin(1) .margin(0)
.constraints( .constraints(
[ [
Constraint::Percentage(10), Constraint::Percentage(10),
@ -37,7 +43,7 @@ pub async fn run_ui(stdout: Stdout, exit_tx: mpsc::Sender<()>) -> Result<()> {
.as_ref(), .as_ref(),
) )
.split(f.size()); .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]); f.render_widget(block, chunks[0]);
let block = Block::default().title("Block 2").borders(Borders::ALL); let block = Block::default().title("Block 2").borders(Borders::ALL);
f.render_widget(block, chunks[1]); 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]); 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()? { // if let Event::Input(input) = events.next()? {
// match input { // match input {
// Key::Char('q') => { // 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(()) Ok(())
} }