diff options
author | Jiayi Zhao <jeff.no.zhao@gmail.com> | 2020-08-30 21:29:21 -0400 |
---|---|---|
committer | Jiayi Zhao <jeff.no.zhao@gmail.com> | 2020-08-30 21:29:21 -0400 |
commit | e6e35948545bfa14d672cafae92c2c8c153ba708 (patch) | |
tree | 68daaf0da49f0507fc3e3d4a4cfd4f812ff00a23 /src/io | |
parent | e7218c81d90ae07d7f56dce0c3032db15b11d118 (diff) |
make io worker msgs more friendly and informative
Diffstat (limited to 'src/io')
-rw-r--r-- | src/io/io_worker.rs | 50 | ||||
-rw-r--r-- | src/io/mod.rs | 2 |
2 files changed, 42 insertions, 10 deletions
diff --git a/src/io/io_worker.rs b/src/io/io_worker.rs index cd96929..ee599a9 100644 --- a/src/io/io_worker.rs +++ b/src/io/io_worker.rs @@ -28,6 +28,14 @@ impl std::default::Default for IOWorkerOptions { } } +#[derive(Clone, Debug)] +pub struct IOWorkerProgress { + pub kind: FileOp, + pub index: usize, + pub len: usize, + pub processed: u64, +} + #[derive(Debug)] pub struct IOWorkerThread { pub options: IOWorkerOptions, @@ -44,20 +52,32 @@ impl IOWorkerThread { } } - pub fn start(&self, tx: mpsc::Sender<u64>) -> std::io::Result<u64> { + pub fn start(&self, tx: mpsc::Sender<IOWorkerProgress>) -> std::io::Result<IOWorkerProgress> { match self.options.kind { FileOp::Cut => self.paste_cut(tx), FileOp::Copy => self.paste_copy(tx), } } - fn paste_copy(&self, tx: mpsc::Sender<u64>) -> std::io::Result<u64> { + fn paste_copy(&self, tx: mpsc::Sender<IOWorkerProgress>) -> std::io::Result<IOWorkerProgress> { let mut total = 0; - for path in self.paths.iter() { + let len = self.paths.len(); + for (i, path) in self.paths.iter().enumerate() { total += self.recursive_copy(path.as_path(), self.dest.as_path())?; - tx.send(total); + let progress = IOWorkerProgress { + kind: FileOp::Copy, + index: i, + processed: total, + len, + }; + tx.send(progress); } - Ok(total) + Ok(IOWorkerProgress { + kind: FileOp::Copy, + index: len, + processed: total, + len, + }) } fn recursive_copy(&self, src: &path::Path, dest: &path::Path) -> std::io::Result<u64> { @@ -87,13 +107,25 @@ impl IOWorkerThread { } } - fn paste_cut(&self, tx: mpsc::Sender<u64>) -> std::io::Result<u64> { + fn paste_cut(&self, tx: mpsc::Sender<IOWorkerProgress>) -> std::io::Result<IOWorkerProgress> { let mut total = 0; - for path in self.paths.iter() { + let len = self.paths.len(); + for (i, path) in self.paths.iter().enumerate() { total += self.recursive_cut(path.as_path(), self.dest.as_path())?; - tx.send(total); + let progress = IOWorkerProgress { + kind: FileOp::Copy, + index: i, + processed: total, + len, + }; + tx.send(progress); } - Ok(total) + Ok(IOWorkerProgress { + kind: FileOp::Copy, + index: len, + processed: total, + len, + }) } pub fn recursive_cut(&self, src: &path::Path, dest: &path::Path) -> std::io::Result<u64> { diff --git a/src/io/mod.rs b/src/io/mod.rs index 43c70d7..f360d19 100644 --- a/src/io/mod.rs +++ b/src/io/mod.rs @@ -3,5 +3,5 @@ mod io_worker; mod name_resolution; pub use io_observer::IOWorkerObserver; -pub use io_worker::{FileOp, IOWorkerOptions, IOWorkerThread}; +pub use io_worker::{FileOp, IOWorkerOptions, IOWorkerProgress, IOWorkerThread}; pub use name_resolution::rename_filename_conflict; |