Some cleanup

pull/395/head
Arijit Basu 3 years ago committed by Arijit Basu
parent 102832c65c
commit 91675e28af

@ -1,10 +1,12 @@
use crate::app::Task;
use crate::app::{ExternalMsg, InternalMsg, MsgIn};
use crate::input::Key;
use anyhow::Error;
use crossterm::event::{self, Event, MouseEventKind};
use std::sync::mpsc;
use std::sync::mpsc::{Receiver, Sender};
use std::thread;
use std::time::Duration;
pub(crate) struct EventReader {
task_sender: Sender<Task>,
@ -32,8 +34,8 @@ impl EventReader {
pub(crate) fn stop(&self) {
if let Some((stopper, ack)) = &self.stopper {
stopper.send(true).unwrap();
ack.recv().unwrap();
stopper.send(true).unwrap_or_default(); // Let's not panic when xplr stops.
ack.recv().unwrap_or_default();
}
}
}
@ -47,39 +49,44 @@ fn keep_reading(tx_msg_in: Sender<Task>, rx_stopper: Receiver<bool>, tx_ack: Sen
// NOTE: The poll timeout need to stay low, else spawning sub subshell
// and start typing immediately will cause panic.
// To reproduce, press `:`, then press and hold `!`.
match event::read() {
let res = match event::read() {
Ok(Event::Key(key)) => {
let key = Key::from_event(key);
let msg = MsgIn::Internal(InternalMsg::HandleKey(key));
tx_msg_in.send(Task::new(msg, Some(key))).unwrap();
tx_msg_in
.send(Task::new(msg, Some(key)))
.map_err(Error::new)
}
Ok(Event::Mouse(evt)) => match evt.kind {
MouseEventKind::ScrollUp => {
let msg = MsgIn::External(ExternalMsg::FocusPrevious);
tx_msg_in.send(Task::new(msg, None)).unwrap();
tx_msg_in.send(Task::new(msg, None)).map_err(Error::new)
}
MouseEventKind::ScrollDown => {
let msg = MsgIn::External(ExternalMsg::FocusNext);
tx_msg_in.send(Task::new(msg, None)).unwrap();
tx_msg_in.send(Task::new(msg, None)).map_err(Error::new)
}
_ => {}
_ => Ok(()),
},
Ok(Event::Resize(_, _)) => {
let msg = MsgIn::External(ExternalMsg::Refresh);
tx_msg_in.send(Task::new(msg, None)).unwrap();
tx_msg_in.send(Task::new(msg, None)).map_err(Error::new)
}
Err(e) => {
tx_msg_in
.send(Task::new(
MsgIn::External(ExternalMsg::LogError(e.to_string())),
None,
))
.unwrap();
}
Err(e) => Err(Error::new(e)),
};
if let Err(e) = res {
tx_msg_in
.send(Task::new(
MsgIn::External(ExternalMsg::LogError(e.to_string())),
None,
))
.unwrap_or_default(); // Let's not panic when xplr stops
thread::sleep(Duration::from_secs(1));
}
}
}

@ -1,5 +1,5 @@
use crate::app::{DirectoryBuffer, ExplorerConfig, ExternalMsg, InternalMsg, MsgIn, Node, Task};
use anyhow::Result;
use anyhow::{Result, Error};
use std::fs;
use std::path::PathBuf;
use std::sync::mpsc::Sender;
@ -55,13 +55,13 @@ pub(crate) fn explore_async(
) {
thread::spawn(move || {
explore_sync(config, parent.clone(), focused_path, fallback_focus)
.map(|buf| {
.and_then(|buf| {
tx_msg_in
.send(Task::new(
MsgIn::Internal(InternalMsg::SetDirectory(buf)),
None,
))
.unwrap();
.map_err(Error::new)
})
.unwrap_or_else(|e| {
tx_msg_in
@ -69,7 +69,7 @@ pub(crate) fn explore_async(
MsgIn::External(ExternalMsg::LogError(e.to_string())),
None,
))
.unwrap();
.unwrap_or_default(); // Let's not panic if xplr closes.
})
});
}

@ -1,6 +1,6 @@
use crate::app::Task;
use crate::app::{ExternalMsg, MsgIn};
use anyhow::Result;
use anyhow::{Error, Result};
use std::path::PathBuf;
use std::sync::mpsc::{Receiver, Sender};
use std::thread;
@ -18,23 +18,26 @@ pub fn keep_watching(
if let Ok(new_pwd) = rx_pwd_watcher.try_recv() {
pwd = PathBuf::from(new_pwd);
} else {
pwd.metadata()
.and_then(|m| m.modified())
.map(|modified| {
if let Err(e) = pwd
.metadata()
.map_err(Error::new)
.and_then(|m| m.modified().map_err(Error::new))
.and_then(|modified| {
if modified != last_modified {
let msg = MsgIn::External(ExternalMsg::ExplorePwdAsync);
tx_msg_in.send(Task::new(msg, None)).unwrap();
last_modified = modified;
tx_msg_in.send(Task::new(msg, None)).map_err(Error::new)
} else {
thread::sleep(Duration::from_secs(1));
};
Result::Ok(())
}
})
.unwrap_or_else(|e| {
let msg = MsgIn::External(ExternalMsg::LogError(e.to_string()));
tx_msg_in.send(Task::new(msg, None)).unwrap();
thread::sleep(Duration::from_secs(1));
})
}
{
let msg = MsgIn::External(ExternalMsg::LogError(e.to_string()));
tx_msg_in.send(Task::new(msg, None)).unwrap_or_default();
thread::sleep(Duration::from_secs(1));
};
};
});
Ok(())
}
@ -42,8 +45,8 @@ pub fn keep_watching(
#[cfg(test)]
mod tests {
use super::*;
use std::sync::mpsc;
#[test]
fn test_pwd_watcher() {
let (tx_msg_in, rx_msg_in) = mpsc::channel();

@ -8,13 +8,12 @@ use crate::lua;
use crate::pipe_reader;
use crate::pwd_watcher;
use crate::ui;
use anyhow::Result;
use anyhow::{bail, Error, Result};
use crossterm::event;
use crossterm::execute;
use crossterm::terminal as term;
use mlua::LuaSerdeExt;
use std::fs;
use std::io;
use std::io::Write;
use std::path::PathBuf;
use std::process::{Command, ExitStatus, Stdio};
@ -22,11 +21,14 @@ use std::sync::mpsc;
use tui::backend::CrosstermBackend;
use tui::Terminal;
pub fn get_tty() -> io::Result<fs::File> {
fs::OpenOptions::new()
.read(true)
.write(true)
.open("/dev/tty")
pub fn get_tty() -> Result<fs::File> {
let tty = "/dev/tty";
match fs::OpenOptions::new().read(true).write(true).open(&tty) {
Ok(f) => Ok(f),
Err(e) => {
bail!(format!("Failed to open {}. {}", tty, e))
}
}
}
fn call_lua(
@ -47,7 +49,7 @@ fn call_lua(
lua::call(lua, func, arg)
}
fn call(app: &app::App, cmd: app::Command, silent: bool) -> io::Result<ExitStatus> {
fn call(app: &app::App, cmd: app::Command, silent: bool) -> Result<ExitStatus> {
let focus_index = app
.directory_buffer
.as_ref()
@ -90,6 +92,7 @@ fn call(app: &app::App, cmd: app::Command, silent: bool) -> io::Result<ExitStatu
.stderr(stderr)
.args(cmd.args)
.status()
.map_err(Error::new)
}
fn start_fifo(path: &str, focus_path: &str) -> Result<fs::File> {

Loading…
Cancel
Save