diff options
author | Jiayi Zhao <jeff.no.zhao@gmail.com> | 2020-03-14 18:22:18 -0400 |
---|---|---|
committer | Jiayi Zhao <jeff.no.zhao@gmail.com> | 2020-03-14 18:23:14 -0400 |
commit | d5466b503e0ba0f88682654dc7ce28cf2f4393c8 (patch) | |
tree | 9ce09bb4100b3f12480f6db2eb951ec48e9795b4 /src/util/event.rs | |
parent | 032258fcd42761719561a43e52c1a264b92cf067 (diff) |
rework input event thread handling
- this fixes issues where joshuto steals the inputs of
terminal applications after file operations have been done
Diffstat (limited to 'src/util/event.rs')
-rw-r--r-- | src/util/event.rs | 44 |
1 files changed, 26 insertions, 18 deletions
diff --git a/src/util/event.rs b/src/util/event.rs index 5a76702..9ec41f7 100644 --- a/src/util/event.rs +++ b/src/util/event.rs @@ -27,7 +27,7 @@ pub struct Events { prefix: &'static str, pub event_tx: mpsc::Sender<Event>, event_rx: mpsc::Receiver<Event>, - pub sync_tx: mpsc::SyncSender<()>, + pub input_tx: mpsc::SyncSender<()>, // fileio_handle: thread::JoinHandle<()>, } @@ -41,7 +41,7 @@ impl Events { } pub fn with_config(prefix: &'static str) -> Self { - let (sync_tx, sync_rx) = mpsc::sync_channel(1); + let (input_tx, input_rx) = mpsc::sync_channel(1); let (event_tx, event_rx) = mpsc::channel(); { @@ -49,16 +49,26 @@ impl Events { thread::spawn(move || { let stdin = io::stdin(); let mut keys = stdin.keys(); - while let Ok(_) = sync_rx.recv() { - if let Some(evt) = keys.next() { - match evt { - Ok(key) => { - if let Err(e) = event_tx.send(Event::Input(key)) { - eprintln!("[{}] Input thread send err: {:#?}", prefix, e); - return; - } + match keys.next() { + Some(key) => match key { + Ok(key) => { + if let Err(e) = event_tx.send(Event::Input(key)) { + eprintln!("[{}] Input thread send err: {:#?}", prefix, e); + return; + } + } + _ => return, + } + _ => return, + } + + while let Ok(_) = input_rx.recv() { + if let Some(key) = keys.next() { + if let Ok(key) = key { + if let Err(e) = event_tx.send(Event::Input(key)) { + eprintln!("[{}] Input thread send err: {:#?}", prefix, e); + return; } - _ => {} } } } @@ -68,19 +78,17 @@ impl Events { Events { event_tx, event_rx, - sync_tx, + input_tx, prefix, } } pub fn next(&self) -> Result<Event, mpsc::RecvError> { - self.sync_tx.try_send(()); let event = self.event_rx.recv()?; Ok(event) } - /* - pub fn flush(&self) { - self.sync_rx.try_recv(); - } - */ + + pub fn flush(&self) { + self.input_tx.send(()); + } } |