diff --git a/src/app.rs b/src/app.rs index 3d0f7ab..a5e87b0 100644 --- a/src/app.rs +++ b/src/app.rs @@ -1,6 +1,5 @@ use crate::config::Config; use crate::config::Mode; -use crate::explorer; use crate::input::Key; use crate::ui::Layout; use anyhow::{bail, Result}; @@ -1298,6 +1297,7 @@ impl Command { #[derive(Debug, Clone, Eq, PartialEq, Serialize, Deserialize)] pub enum MsgOut { + ExplorePwd, ExplorePwdAsync, ExploreParentsAsync, Refresh, @@ -1700,13 +1700,9 @@ impl App { Ok(self) } - fn explore_pwd(self) -> Result { - let dirbuf = explorer::explore_sync( - self.explorer_config().clone(), - self.pwd().clone(), - self.focused_node().map(|n| n.absolute_path().clone()), - )?; - self.add_directory(dirbuf.parent().clone(), dirbuf) + fn explore_pwd(mut self) -> Result { + self.msg_out.push_back(MsgOut::ExplorePwd); + Ok(self) } fn explore_pwd_async(mut self) -> Result { @@ -1805,18 +1801,13 @@ impl App { } fn change_directory(mut self, dir: &str) -> Result { - if PathBuf::from(dir).is_dir() { - match env::set_current_dir(dir) { - Ok(()) => { - self.pwd = dir.to_owned(); - self.history = self.history.push(self.pwd.clone()); - self.msg_out.push_back(MsgOut::Refresh); - Ok(self) - } - Err(e) => self.log_error(e.to_string()), + match env::set_current_dir(dir) { + Ok(()) => { + self.pwd = dir.to_owned(); + self.history = self.history.push(self.pwd.clone()); + self.explore_pwd_async() } - } else { - Ok(self) + Err(e) => self.log_error(e.to_string()), } } @@ -1845,7 +1836,7 @@ impl App { .map(|p| p.to_owned()) .unwrap_or(self.pwd); env::set_current_dir(&self.pwd)?; - self.refresh() + self.explore_pwd_async() } fn next_visited_path(mut self) -> Result { @@ -1856,7 +1847,7 @@ impl App { .map(|p| p.to_owned()) .unwrap_or(self.pwd); env::set_current_dir(&self.pwd)?; - self.refresh() + self.explore_pwd_async() } fn buffer_input(mut self, input: &str) -> Result { diff --git a/src/explorer.rs b/src/explorer.rs index a64378c..ba1c84e 100644 --- a/src/explorer.rs +++ b/src/explorer.rs @@ -46,23 +46,29 @@ pub fn explore_async( config: ExplorerConfig, parent: String, focused_path: Option, - tx: Sender, + tx_msg_in: Sender, + tx_pwd_watcher: Sender, ) { thread::spawn(move || { explore_sync(config, parent.clone(), focused_path) - .map(|dirbuf| { - tx.send(Task::new( - MsgIn::Internal(InternalMsg::AddDirectory(parent, dirbuf)), - None, - )) - .unwrap_or_default(); + .map(|buf| { + tx_pwd_watcher + .send(buf.parent().clone()) + .unwrap_or_default(); + tx_msg_in + .send(Task::new( + MsgIn::Internal(InternalMsg::AddDirectory(parent.clone(), buf)), + None, + )) + .unwrap_or_default(); }) .unwrap_or_else(|e| { - tx.send(Task::new( - MsgIn::External(ExternalMsg::LogError(e.to_string())), - None, - )) - .unwrap_or_default(); + tx_msg_in + .send(Task::new( + MsgIn::External(ExternalMsg::LogError(e.to_string())), + None, + )) + .unwrap_or_default(); }) }); } @@ -71,16 +77,24 @@ pub fn explore_recursive_async( config: ExplorerConfig, parent: String, focused_path: Option, - tx: Sender, + tx_msg_in: Sender, + tx_pwd_watcher: Sender, ) { let path = PathBuf::from(&parent); - explore_async(config.clone(), parent, focused_path, tx.clone()); + explore_async( + config.clone(), + parent, + focused_path, + tx_msg_in.clone(), + tx_pwd_watcher.clone(), + ); if let Some(grand_parent) = path.parent() { explore_recursive_async( config, grand_parent.to_string_lossy().to_string(), path.file_name().map(|f| f.to_string_lossy().to_string()), - tx, + tx_msg_in, + tx_pwd_watcher, ); } } diff --git a/src/pwd_watcher.rs b/src/pwd_watcher.rs index 1d6c8a5..f99d11f 100644 --- a/src/pwd_watcher.rs +++ b/src/pwd_watcher.rs @@ -17,6 +17,10 @@ pub fn keep_watching( thread::spawn(move || loop { if let Ok(new_pwd) = rx_pwd_watcher.try_recv() { pwd = PathBuf::from(new_pwd); + last_modified = pwd + .metadata() + .and_then(|m| m.modified()) + .unwrap_or(last_modified); } else { pwd.metadata() .and_then(|m| m.modified()) @@ -26,13 +30,13 @@ pub fn keep_watching( tx_msg_in.send(Task::new(msg, None)).unwrap_or_default(); last_modified = modified; } else { - thread::sleep(Duration::from_millis(1000)); + thread::sleep(Duration::from_secs(1)); }; }) .unwrap_or_else(|e| { let msg = MsgIn::External(ExternalMsg::LogError(e.to_string())); tx_msg_in.send(Task::new(msg, None)).unwrap_or_default(); - thread::sleep(Duration::from_millis(1000)); + thread::sleep(Duration::from_secs(1)); }) } }); diff --git a/src/runner.rs b/src/runner.rs index a49f373..c73f044 100644 --- a/src/runner.rs +++ b/src/runner.rs @@ -75,6 +75,7 @@ pub fn run(mut app: app::App, focused_path: Option) -> Result) -> Result { + match explorer::explore_sync( + app.explorer_config().clone(), + app.pwd().clone(), + app.focused_node().map(|n| n.relative_path().clone()), + ) { + Ok(buf) => { + tx_pwd_watcher.send(buf.parent().clone())?; + let msg = app::MsgIn::Internal(app::InternalMsg::AddDirectory( + buf.parent().clone(), + buf, + )); + tx_msg_in.send(app::Task::new(msg, None))?; + } + Err(e) => { + let msg = + app::MsgIn::External(app::ExternalMsg::LogError(e.to_string())); + tx_msg_in.send(app::Task::new(msg, None))?; + } + } + } + app::MsgOut::ExplorePwdAsync => { explorer::explore_async( app.explorer_config().clone(), app.pwd().clone(), app.focused_node().map(|n| n.relative_path().clone()), tx_msg_in.clone(), + tx_pwd_watcher.clone(), ); } @@ -171,20 +195,12 @@ pub fn run(mut app: app::App, focused_path: Option) -> Result { - if app.pwd() != last_app.pwd() { - tx_pwd_watcher.send(app.pwd().clone())?; - explorer::explore_recursive_async( - app.explorer_config().clone(), - app.pwd().clone(), - app.focused_node().map(|n| n.relative_path().clone()), - tx_msg_in.clone(), - ); - }; - // UI terminal.draw(|f| ui::draw(f, &app, &hb))?; }