mirror of
https://github.com/extrawurst/gitui.git
synced 2024-11-23 11:42:56 +03:00
allow input error to be logged and abort program (#693)
This commit is contained in:
parent
43d125b909
commit
423a014b0e
66
src/input.rs
66
src/input.rs
@ -1,7 +1,9 @@
|
|||||||
use crate::notify_mutex::NotifyableMutex;
|
use crate::notify_mutex::NotifyableMutex;
|
||||||
use crossbeam_channel::{unbounded, Receiver};
|
use anyhow::Result;
|
||||||
|
use crossbeam_channel::{unbounded, Receiver, Sender};
|
||||||
use crossterm::event::{self, Event};
|
use crossterm::event::{self, Event};
|
||||||
use std::{
|
use std::{
|
||||||
|
process,
|
||||||
sync::{
|
sync::{
|
||||||
atomic::{AtomicBool, Ordering},
|
atomic::{AtomicBool, Ordering},
|
||||||
Arc,
|
Arc,
|
||||||
@ -44,33 +46,12 @@ impl Input {
|
|||||||
let arc_desired = Arc::clone(&desired_state);
|
let arc_desired = Arc::clone(&desired_state);
|
||||||
let arc_current = Arc::clone(¤t_state);
|
let arc_current = Arc::clone(¤t_state);
|
||||||
|
|
||||||
thread::spawn(move || loop {
|
thread::spawn(move || {
|
||||||
if arc_desired.get() {
|
if let Err(e) =
|
||||||
if !arc_current.load(Ordering::Relaxed) {
|
Self::input_loop(&arc_desired, &arc_current, &tx)
|
||||||
log::info!("input polling resumed");
|
|
||||||
|
|
||||||
tx.send(InputEvent::State(InputState::Polling))
|
|
||||||
.expect("send state failed");
|
|
||||||
}
|
|
||||||
arc_current.store(true, Ordering::Relaxed);
|
|
||||||
|
|
||||||
if let Some(e) = Self::poll(POLL_DURATION)
|
|
||||||
.expect("failed to pull events.")
|
|
||||||
{
|
{
|
||||||
tx.send(InputEvent::Input(e))
|
log::error!("input thread error: {}", e);
|
||||||
.expect("send input failed");
|
process::abort();
|
||||||
}
|
|
||||||
} else {
|
|
||||||
if arc_current.load(Ordering::Relaxed) {
|
|
||||||
log::info!("input polling suspended");
|
|
||||||
|
|
||||||
tx.send(InputEvent::State(InputState::Paused))
|
|
||||||
.expect("send state failed");
|
|
||||||
}
|
|
||||||
|
|
||||||
arc_current.store(false, Ordering::Relaxed);
|
|
||||||
|
|
||||||
arc_desired.wait(true);
|
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
@ -108,4 +89,35 @@ impl Input {
|
|||||||
Ok(None)
|
Ok(None)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn input_loop(
|
||||||
|
arc_desired: &Arc<NotifyableMutex<bool>>,
|
||||||
|
arc_current: &Arc<AtomicBool>,
|
||||||
|
tx: &Sender<InputEvent>,
|
||||||
|
) -> Result<()> {
|
||||||
|
loop {
|
||||||
|
if arc_desired.get() {
|
||||||
|
if !arc_current.load(Ordering::Relaxed) {
|
||||||
|
log::info!("input polling resumed");
|
||||||
|
|
||||||
|
tx.send(InputEvent::State(InputState::Polling))?;
|
||||||
|
}
|
||||||
|
arc_current.store(true, Ordering::Relaxed);
|
||||||
|
|
||||||
|
if let Some(e) = Self::poll(POLL_DURATION)? {
|
||||||
|
tx.send(InputEvent::Input(e))?;
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
if arc_current.load(Ordering::Relaxed) {
|
||||||
|
log::info!("input polling suspended");
|
||||||
|
|
||||||
|
tx.send(InputEvent::State(InputState::Paused))?
|
||||||
|
}
|
||||||
|
|
||||||
|
arc_current.store(false, Ordering::Relaxed);
|
||||||
|
|
||||||
|
arc_desired.wait(true);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user