diff options
author | Jiayi Zhao <jeff.no.zhao@gmail.com> | 2020-03-14 10:34:14 -0400 |
---|---|---|
committer | Jiayi Zhao <jeff.no.zhao@gmail.com> | 2020-03-14 10:34:14 -0400 |
commit | 032258fcd42761719561a43e52c1a264b92cf067 (patch) | |
tree | ddadf6c31989b08c69ec2fc6b738399bf00eb597 | |
parent | 18ddb386325e9a017df45825ace6d667c90ac3e5 (diff) |
make sure to reload src and dest dirlists after a io operation completes
-rw-r--r-- | src/fs/dirlist.rs | 6 | ||||
-rw-r--r-- | src/history.rs | 6 | ||||
-rw-r--r-- | src/io/io_worker.rs | 38 | ||||
-rw-r--r-- | src/io/mod.rs | 2 | ||||
-rw-r--r-- | src/run.rs | 35 |
5 files changed, 63 insertions, 24 deletions
diff --git a/src/fs/dirlist.rs b/src/fs/dirlist.rs index 0621ee1..7d31814 100644 --- a/src/fs/dirlist.rs +++ b/src/fs/dirlist.rs @@ -66,9 +66,7 @@ impl JoshutoDirList { None } else { match self.index { - Some(i) if i < contents_len => { - Some(i) - } + Some(i) if i < contents_len => Some(i), Some(i) => { let entry = &self.contents[i]; contents @@ -80,8 +78,6 @@ impl JoshutoDirList { } None => Some(0), } - - } }; diff --git a/src/history.rs b/src/history.rs index cf6892f..16fc8a3 100644 --- a/src/history.rs +++ b/src/history.rs @@ -16,6 +16,8 @@ pub trait DirectoryHistory { sort_option: &sort::SortOption, ) -> std::io::Result<()>; fn depreciate_all_entries(&mut self); + + fn depreciate_entry(&mut self, path: &Path); } pub type JoshutoHistory = HashMap<PathBuf, JoshutoDirList>; @@ -75,6 +77,10 @@ impl DirectoryHistory for JoshutoHistory { fn depreciate_all_entries(&mut self) { self.iter_mut().for_each(|(_, v)| v.depreciate()); } + + fn depreciate_entry(&mut self, path: &Path) { + self.get_mut(path).map(|v| v.depreciate()); + } } fn get_index_of_value(arr: &[JoshutoDirEntry], val: &Path) -> Option<usize> { diff --git a/src/io/io_worker.rs b/src/io/io_worker.rs index f497526..382d6aa 100644 --- a/src/io/io_worker.rs +++ b/src/io/io_worker.rs @@ -1,7 +1,9 @@ -use crate::util::event::Event; +use std::path; use std::sync::mpsc; use std::thread; +use crate::util::event::Event; + #[derive(Clone, Debug)] pub struct Options { pub overwrite: bool, @@ -17,7 +19,41 @@ impl std::default::Default for Options { } } +pub struct IOWorkerObserver { + pub src: path::PathBuf, + pub dest: path::PathBuf, + pub handle: std::thread::JoinHandle<()>, +} + +impl IOWorkerObserver { + pub fn new(worker: IOWorkerThread, event_tx: mpsc::Sender<Event>) -> Self { + let src = worker.src.clone(); + let dest = worker.dest.clone(); + + let handle = thread::spawn(move || { + worker.start(); + while let Ok(evt) = worker.recv() { + let _ = event_tx.send(evt); + } + worker.handle.join(); + let _ = event_tx.send(Event::IOWorkerResult); + }); + + Self { + src, + dest, + handle, + } + } + + pub fn join(self) { + self.handle.join(); + } +} + pub struct IOWorkerThread { + pub src: path::PathBuf, + pub dest: path::PathBuf, pub handle: thread::JoinHandle<std::io::Result<u64>>, pub tx_start: mpsc::Sender<()>, pub rx: mpsc::Receiver<Event>, diff --git a/src/io/mod.rs b/src/io/mod.rs index 350bdba..5955b60 100644 --- a/src/io/mod.rs +++ b/src/io/mod.rs @@ -1,3 +1,3 @@ mod io_worker; -pub use self::io_worker::{IOWorkerThread, Options}; +pub use self::io_worker::{IOWorkerObserver, IOWorkerThread, Options}; @@ -1,12 +1,14 @@ use std::thread; -use crate::commands::{CommandKeybind, CursorMoveUp, JoshutoRunnable}; +use crate::commands::{CommandKeybind, CursorMoveStub, JoshutoRunnable}; use crate::config::{JoshutoCommandMapping, JoshutoConfig}; use crate::context::JoshutoContext; use crate::tab::JoshutoTab; use crate::ui; use crate::ui::widgets::{TuiCommandMenu, TuiView}; use crate::util::event::Event; +use crate::history::DirectoryHistory; +use crate::io::IOWorkerObserver; pub fn run(config_t: JoshutoConfig, keymap_t: JoshutoCommandMapping) -> std::io::Result<()> { let mut backend: ui::TuiBackend = ui::TuiBackend::new()?; @@ -20,31 +22,23 @@ pub fn run(config_t: JoshutoConfig, keymap_t: JoshutoCommandMapping) -> std::io: context.push_tab(tab); // move the cursor by 0 just to trigger a preview of child - let tmp = CursorMoveUp::new(0); - tmp.execute(&mut context, &mut backend); + CursorMoveStub::new().execute(&mut context, &mut backend); // render our view let mut view = TuiView::new(&context); backend.render(&mut view); } - let mut io_handle = None; + let mut io_observer = None; while !context.exit { /* checking if there are workers that need to be run */ if !context.worker_queue.is_empty() { - if let None = io_handle.as_ref() { + if let None = io_observer.as_ref() { let worker = context.worker_queue.pop_front().unwrap(); - io_handle = { + io_observer = { let event_tx = context.events.event_tx.clone(); - let thread = thread::spawn(move || { - worker.start(); - while let Ok(evt) = worker.recv() { - let _ = event_tx.send(evt); - } - worker.handle.join(); - let _ = event_tx.send(Event::IOWorkerResult); - }); - Some(thread) + let observer = IOWorkerObserver::new(worker, event_tx); + Some(observer) }; } } @@ -56,16 +50,23 @@ pub fn run(config_t: JoshutoConfig, keymap_t: JoshutoCommandMapping) -> std::io: context.worker_msg = Some(format!("bytes copied {}", p)); } Event::IOWorkerResult => { - match io_handle { + match io_observer { Some(handle) => { + let src = handle.src.clone(); + let dest = handle.dest.clone(); handle.join(); context .message_queue .push_back("io_worker done".to_string()); + let options = &context.config_t.sort_option; + for tab in context.tabs.iter_mut() { + tab.history.create_or_update(src.as_path(), options); + tab.history.create_or_update(dest.as_path(), options); + } } None => {} } - io_handle = None; + io_observer = None; context.worker_msg = None; } Event::Input(key) => { |