2021-04-02 12:33:56 +00:00
|
|
|
use crate::app::Task;
|
|
|
|
use crate::app::{ExternalMsg, InternalMsg, MsgIn};
|
|
|
|
use crate::input::Key;
|
2021-10-04 11:43:54 +00:00
|
|
|
use anyhow::Result;
|
2021-06-04 16:09:14 +00:00
|
|
|
use crossterm::event::{self, Event, MouseEventKind};
|
2021-04-02 12:33:56 +00:00
|
|
|
use std::sync::mpsc::{Receiver, Sender};
|
|
|
|
use std::thread;
|
|
|
|
|
2021-10-04 11:43:54 +00:00
|
|
|
pub fn pause_reading(tx_event_reader: &Sender<bool>, rx_loop_waiter: &Receiver<()>) -> Result<()> {
|
|
|
|
tx_event_reader.send(true)?;
|
|
|
|
rx_loop_waiter.recv()?;
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn resume_reading(tx_event_reader: &Sender<bool>, rx_loop_waiter: &Receiver<()>) -> Result<()> {
|
|
|
|
tx_event_reader.send(false)?;
|
|
|
|
rx_loop_waiter.recv()?;
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn keep_reading(
|
|
|
|
tx_msg_in: Sender<Task>,
|
|
|
|
rx_event_reader: Receiver<bool>,
|
|
|
|
tx_loop_waiter: Sender<()>,
|
|
|
|
) {
|
2021-04-02 12:33:56 +00:00
|
|
|
thread::spawn(move || {
|
|
|
|
let mut is_paused = false;
|
|
|
|
loop {
|
2021-04-05 05:36:53 +00:00
|
|
|
if let Ok(paused) = rx_event_reader.try_recv() {
|
2021-04-02 12:33:56 +00:00
|
|
|
is_paused = paused;
|
2021-10-04 11:43:54 +00:00
|
|
|
tx_loop_waiter.send(()).unwrap();
|
2021-04-02 12:33:56 +00:00
|
|
|
};
|
|
|
|
|
2021-05-13 07:48:51 +00:00
|
|
|
if is_paused {
|
|
|
|
thread::sleep(std::time::Duration::from_millis(200));
|
2021-06-22 03:32:29 +00:00
|
|
|
} else if event::poll(std::time::Duration::from_millis(150)).unwrap_or_default() {
|
2021-05-13 07:48:51 +00:00
|
|
|
// NOTE: The poll timeout need to stay low, else spawning sub subshell
|
|
|
|
// and start typing immediately will cause panic.
|
2021-06-22 03:32:29 +00:00
|
|
|
// To reproduce, press `:`, then press and hold `!`.
|
2021-04-05 05:36:53 +00:00
|
|
|
match event::read() {
|
|
|
|
Ok(Event::Key(key)) => {
|
|
|
|
let key = Key::from_event(key);
|
|
|
|
let msg = MsgIn::Internal(InternalMsg::HandleKey(key));
|
2021-10-04 11:43:54 +00:00
|
|
|
tx_msg_in.send(Task::new(msg, Some(key))).unwrap();
|
2021-04-05 05:36:53 +00:00
|
|
|
}
|
2021-04-02 12:33:56 +00:00
|
|
|
|
2021-06-04 16:09:14 +00:00
|
|
|
Ok(Event::Mouse(evt)) => match evt.kind {
|
|
|
|
MouseEventKind::ScrollUp => {
|
|
|
|
let msg = MsgIn::External(ExternalMsg::FocusPrevious);
|
2021-10-04 11:43:54 +00:00
|
|
|
tx_msg_in.send(Task::new(msg, None)).unwrap();
|
2021-06-04 16:09:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
MouseEventKind::ScrollDown => {
|
|
|
|
let msg = MsgIn::External(ExternalMsg::FocusNext);
|
2021-10-04 11:43:54 +00:00
|
|
|
tx_msg_in.send(Task::new(msg, None)).unwrap();
|
2021-06-04 16:09:14 +00:00
|
|
|
}
|
|
|
|
_ => {}
|
|
|
|
},
|
|
|
|
|
2021-04-05 05:36:53 +00:00
|
|
|
Ok(Event::Resize(_, _)) => {
|
|
|
|
let msg = MsgIn::External(ExternalMsg::Refresh);
|
2021-10-04 11:43:54 +00:00
|
|
|
tx_msg_in.send(Task::new(msg, None)).unwrap();
|
2021-04-05 05:36:53 +00:00
|
|
|
}
|
2021-05-21 12:56:21 +00:00
|
|
|
|
2021-04-05 05:36:53 +00:00
|
|
|
Err(e) => {
|
|
|
|
tx_msg_in
|
|
|
|
.send(Task::new(
|
|
|
|
MsgIn::External(ExternalMsg::LogError(e.to_string())),
|
|
|
|
None,
|
|
|
|
))
|
2021-10-04 11:43:54 +00:00
|
|
|
.unwrap();
|
2021-04-02 12:33:56 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|