From 39d7b2ba9652ac9030e80cdafbc30627ebebaec1 Mon Sep 17 00:00:00 2001 From: Dheepak Krishnamurthy Date: Sun, 14 Feb 2021 21:49:26 -0700 Subject: [PATCH] Fix terminal issues with alacritty --- src/app.rs | 4 +- src/main.rs | 2 +- src/util.rs | 106 ++++++++++++++++++++++------------------------------ 3 files changed, 48 insertions(+), 64 deletions(-) diff --git a/src/app.rs b/src/app.rs index 15061b6..519a29c 100644 --- a/src/app.rs +++ b/src/app.rs @@ -1255,6 +1255,8 @@ impl TTApp { String::from_utf8_lossy(&output.stderr), )) } else { + String::from_utf8_lossy(&output.stdout); + String::from_utf8_lossy(&output.stderr); Ok(()) } } @@ -1404,7 +1406,6 @@ impl TTApp { terminal: &mut Terminal>, events: &Events, ) -> Result<(), Box> { - events.pause_ticker(); match self.mode { AppMode::TaskReport => match input { Key::Ctrl('c') | Key::Char('q') => self.should_quit = true, @@ -1640,7 +1641,6 @@ impl TTApp { _ => {} }, } - events.resume_ticker(); Ok(()) } } diff --git a/src/main.rs b/src/main.rs index 43c38bd..d60e04c 100644 --- a/src/main.rs +++ b/src/main.rs @@ -71,7 +71,7 @@ fn tui_main(_config: &str) -> Result<(), Box> { // Setup event handlers let events = Events::with_config(EventConfig { - tick_rate: Duration::from_millis(1000), + tick_rate: Duration::from_millis(250), }); let maybeapp = TTApp::new(); diff --git a/src/util.rs b/src/util.rs index e879526..8ccafbc 100644 --- a/src/util.rs +++ b/src/util.rs @@ -9,7 +9,7 @@ use tui::{backend::CrosstermBackend, Terminal}; use std::io::{self, Write}; use std::sync::atomic::{AtomicBool, Ordering}; use std::sync::Arc; -use std::{sync::mpsc, thread, time::Duration}; +use std::{sync::mpsc, thread, time::Duration, time::Instant}; #[derive(Debug, Clone, Copy)] pub enum Key { @@ -63,7 +63,6 @@ pub struct Events { pub rx: mpsc::Receiver>, pub tx: mpsc::Sender>, pub pause_stdin: Arc, - pub pause_ticker: Arc, } impl Events { @@ -72,70 +71,61 @@ impl Events { use crossterm::event::{KeyCode::*, KeyModifiers}; let (tx, rx) = mpsc::channel(); let pause_stdin = Arc::new(AtomicBool::new(false)); - let pause_ticker = Arc::new(AtomicBool::new(false)); + let tick_rate = config.tick_rate; let _input_handle = { let tx = tx.clone(); let pause_stdin = pause_stdin.clone(); thread::spawn(move || { + let mut last_tick = Instant::now(); loop { + let timeout = tick_rate + .checked_sub(last_tick.elapsed()) + .unwrap_or_else(|| Duration::from_secs(0)); + if pause_stdin.load(Ordering::SeqCst) { thread::sleep(Duration::from_millis(250)); continue; } + // poll for tick rate duration, if no event, sent tick event. - if let event::Event::Key(key) = event::read().unwrap() { - let key = match key.code { - Backspace => Key::Backspace, - Enter => Key::Char('\n'), - Left => Key::Left, - Right => Key::Right, - Up => Key::Up, - Down => Key::Down, - Home => Key::Home, - End => Key::End, - PageUp => Key::PageUp, - PageDown => Key::PageDown, - Tab => Key::Char('\t'), - BackTab => Key::BackTab, - Delete => Key::Delete, - Insert => Key::Insert, - F(k) => Key::F(k), - Null => Key::Null, - Esc => Key::Esc, - Char(c) => match key.modifiers { - KeyModifiers::NONE | KeyModifiers::SHIFT => Key::Char(c), - KeyModifiers::CONTROL => Key::Ctrl(c), - KeyModifiers::ALT => Key::Alt(c), - _ => Key::Null, - }, - }; - tx.send(Event::Input(key)).unwrap(); + if event::poll(timeout).unwrap() { + if let event::Event::Key(key) = event::read().unwrap() { + let key = match key.code { + Backspace => Key::Backspace, + Enter => Key::Char('\n'), + Left => Key::Left, + Right => Key::Right, + Up => Key::Up, + Down => Key::Down, + Home => Key::Home, + End => Key::End, + PageUp => Key::PageUp, + PageDown => Key::PageDown, + Tab => Key::Char('\t'), + BackTab => Key::BackTab, + Delete => Key::Delete, + Insert => Key::Insert, + F(k) => Key::F(k), + Null => Key::Null, + Esc => Key::Esc, + Char(c) => match key.modifiers { + KeyModifiers::NONE | KeyModifiers::SHIFT => Key::Char(c), + KeyModifiers::CONTROL => Key::Ctrl(c), + KeyModifiers::ALT => Key::Alt(c), + _ => Key::Null, + }, + }; + tx.send(Event::Input(key)).unwrap(); + } + } + + if last_tick.elapsed() >= tick_rate && tx.send(Event::Tick).is_ok() { + last_tick = Instant::now(); } } }) }; - let _tick_handle = { - let tx = tx.clone(); - let pause_ticker = pause_ticker.clone(); - thread::spawn(move || loop { - // print!("\r\n"); - // dbg!(*pause_ticker.lock().unwrap()); - while pause_ticker.load(Ordering::SeqCst) { - thread::sleep(Duration::from_millis(250)); - } - if tx.send(Event::Tick).is_err() { - break; - } - thread::sleep(config.tick_rate); - }) - }; - - Events { - rx, - tx, - pause_stdin, - pause_ticker, - } + Events { rx, tx, pause_stdin } } /// Attempts to read an event. @@ -144,14 +134,6 @@ impl Events { self.rx.recv() } - pub fn pause_ticker(&self) { - self.pause_ticker.swap(true, Ordering::SeqCst); - } - - pub fn resume_ticker(&self) { - self.pause_ticker.swap(false, Ordering::SeqCst); - } - pub fn pause_event_loop(&self) { self.pause_stdin.swap(true, Ordering::SeqCst); } @@ -162,14 +144,16 @@ impl Events { pub fn pause_key_capture(&self, terminal: &mut Terminal>) { self.pause_event_loop(); + thread::sleep(Duration::from_millis(250)); disable_raw_mode().unwrap(); execute!(io::stdout(), LeaveAlternateScreen, DisableMouseCapture).unwrap(); terminal.show_cursor().unwrap(); } pub fn resume_key_capture(&self, terminal: &mut Terminal>) { - enable_raw_mode().unwrap(); execute!(io::stdout(), EnterAlternateScreen, EnableMouseCapture).unwrap(); + enable_raw_mode().unwrap(); + thread::sleep(Duration::from_millis(250)); self.resume_event_loop(); terminal.resize(terminal.size().unwrap()).unwrap(); }