summaryrefslogtreecommitdiffstats
path: root/src/io
diff options
context:
space:
mode:
authorJiayi Zhao <jeff.no.zhao@gmail.com>2020-12-11 21:07:13 -0500
committerJiayi Zhao <jeff.no.zhao@gmail.com>2020-12-11 21:07:13 -0500
commit916d72c42cfc7bcb8fc560e17cb3bd75c00757da (patch)
treea8024fc9bd9a48065cd6b65375109482ac9b7983 /src/io
parentbf9c102a4cfb85a9fd910195e6372dcd1d062c16 (diff)
add a view for showing worker progress
Diffstat (limited to 'src/io')
-rw-r--r--src/io/io_observer.rs48
-rw-r--r--src/io/io_worker.rs111
2 files changed, 112 insertions, 47 deletions
diff --git a/src/io/io_observer.rs b/src/io/io_observer.rs
index c882911..0648fbd 100644
--- a/src/io/io_observer.rs
+++ b/src/io/io_observer.rs
@@ -1,9 +1,13 @@
use std::path;
use std::thread;
+use crate::io::{FileOp, IOWorkerProgress};
+use crate::util::format;
+
#[derive(Debug)]
pub struct IOWorkerObserver {
pub handle: thread::JoinHandle<()>,
+ pub progress: Option<IOWorkerProgress>,
msg: String,
src: path::PathBuf,
dest: path::PathBuf,
@@ -13,25 +17,57 @@ impl IOWorkerObserver {
pub fn new(handle: thread::JoinHandle<()>, src: path::PathBuf, dest: path::PathBuf) -> Self {
Self {
handle,
+ progress: None,
src,
dest,
msg: String::new(),
}
}
- pub fn join(self) {
- self.handle.join();
+ pub fn join(self) -> bool {
+ match self.handle.join() {
+ Ok(_) => true,
+ _ => false,
+ }
}
- pub fn set_msg(&mut self, msg: String) {
- self.msg = msg
+ pub fn set_progress(&mut self, progress: IOWorkerProgress) {
+ self.progress = Some(progress);
+ }
+ pub fn update_msg(&mut self) {
+ match self.progress.as_ref() {
+ None => {}
+ Some(progress) => {
+ let size_str = format::file_size_to_string(progress.processed());
+ match progress.kind() {
+ FileOp::Cut => {
+ let msg = format!(
+ "moving ({}/{}) {} completed",
+ progress.index() + 1,
+ progress.len(),
+ size_str
+ );
+ self.msg = msg;
+ }
+ FileOp::Copy => {
+ let msg = format!(
+ "copying ({}/{}) {} completed",
+ progress.index() + 1,
+ progress.len(),
+ size_str
+ );
+ self.msg = msg;
+ }
+ }
+ }
+ }
}
pub fn get_msg(&self) -> &str {
self.msg.as_str()
}
- pub fn get_src_path(&self) -> &path::Path {
+ pub fn src_path(&self) -> &path::Path {
self.src.as_path()
}
- pub fn get_dest_path(&self) -> &path::Path {
+ pub fn dest_path(&self) -> &path::Path {
self.dest.as_path()
}
}
diff --git a/src/io/io_worker.rs b/src/io/io_worker.rs
index dbccc70..f44a2e6 100644
--- a/src/io/io_worker.rs
+++ b/src/io/io_worker.rs
@@ -37,15 +37,50 @@ impl std::fmt::Display for IOWorkerOptions {
#[derive(Clone, Debug)]
pub struct IOWorkerProgress {
- pub kind: FileOp,
- pub index: usize,
- pub len: usize,
- pub processed: u64,
+ _kind: FileOp,
+ _index: usize,
+ _len: usize,
+ _processed: u64,
+}
+
+impl IOWorkerProgress {
+ pub fn new(_kind: FileOp, _index: usize, _len: usize, _processed: u64) -> Self {
+ Self {
+ _kind,
+ _index,
+ _len,
+ _processed,
+ }
+ }
+
+ pub fn kind(&self) -> FileOp {
+ self._kind
+ }
+
+ pub fn index(&self) -> usize {
+ self._index
+ }
+
+ pub fn set_index(&mut self, _index: usize) {
+ self._index = _index;
+ }
+
+ pub fn len(&self) -> usize {
+ self._len
+ }
+
+ pub fn processed(&self) -> u64 {
+ self._processed
+ }
+
+ pub fn set_processed(&mut self, _processed: u64) {
+ self._processed = _processed;
+ }
}
#[derive(Debug)]
pub struct IOWorkerThread {
- pub kind: FileOp,
+ _kind: FileOp,
pub options: IOWorkerOptions,
pub paths: Vec<path::PathBuf>,
pub dest: path::PathBuf,
@@ -53,36 +88,35 @@ pub struct IOWorkerThread {
impl IOWorkerThread {
pub fn new(
- kind: FileOp,
+ _kind: FileOp,
paths: Vec<path::PathBuf>,
dest: path::PathBuf,
options: IOWorkerOptions,
) -> Self {
Self {
- kind,
+ _kind,
options,
paths,
dest,
}
}
+ pub fn kind(&self) -> FileOp {
+ self._kind
+ }
+
pub fn start(&self, tx: mpsc::Sender<IOWorkerProgress>) -> std::io::Result<IOWorkerProgress> {
- match self.kind {
+ match self.kind() {
FileOp::Cut => self.paste_cut(tx),
FileOp::Copy => self.paste_copy(tx),
}
}
fn paste_copy(&self, tx: mpsc::Sender<IOWorkerProgress>) -> std::io::Result<IOWorkerProgress> {
- let mut progress = IOWorkerProgress {
- kind: self.kind,
- index: 0,
- processed: 0,
- len: self.paths.len(),
- };
+ let mut progress = IOWorkerProgress::new(self.kind(), 0, self.paths.len(), 0);
for (i, path) in self.paths.iter().enumerate() {
- progress.index = i;
- tx.send(progress.clone());
+ progress.set_index(i);
+ let _ = tx.send(progress.clone());
self.recursive_copy(
path.as_path(),
self.dest.as_path(),
@@ -90,12 +124,12 @@ impl IOWorkerThread {
&mut progress,
)?;
}
- Ok(IOWorkerProgress {
- kind: self.kind,
- index: self.paths.len(),
- processed: progress.processed,
- len: self.paths.len(),
- })
+ Ok(IOWorkerProgress::new(
+ self.kind(),
+ self.paths.len(),
+ self.paths.len(),
+ progress.processed(),
+ ))
}
fn recursive_copy(
@@ -122,11 +156,11 @@ impl IOWorkerThread {
tx.clone(),
progress,
)?;
- tx.send(progress.clone());
+ let _ = tx.send(progress.clone());
}
Ok(())
} else if file_type.is_file() {
- progress.processed += fs::copy(src, dest_buf)?;
+ progress._processed += fs::copy(src, dest_buf)?;
Ok(())
} else if file_type.is_symlink() {
let link_path = fs::read_link(src)?;
@@ -138,15 +172,10 @@ impl IOWorkerThread {
}
fn paste_cut(&self, tx: mpsc::Sender<IOWorkerProgress>) -> std::io::Result<IOWorkerProgress> {
- let mut progress = IOWorkerProgress {
- kind: self.kind,
- index: 0,
- processed: 0,
- len: self.paths.len(),
- };
+ let mut progress = IOWorkerProgress::new(self.kind(), 0, self.paths.len(), 0);
for (i, path) in self.paths.iter().enumerate() {
- progress.index = i;
- tx.send(progress.clone());
+ progress.set_index(i);
+ let _ = tx.send(progress.clone());
self.recursive_cut(
path.as_path(),
self.dest.as_path(),
@@ -154,12 +183,12 @@ impl IOWorkerThread {
&mut progress,
)?;
}
- Ok(IOWorkerProgress {
- kind: self.kind,
- index: self.paths.len(),
- processed: progress.processed,
- len: self.paths.len(),
- })
+ Ok(IOWorkerProgress::new(
+ self.kind(),
+ self.paths.len(),
+ self.paths.len(),
+ progress.processed(),
+ ))
}
pub fn recursive_cut(
@@ -179,7 +208,7 @@ impl IOWorkerThread {
if file_type.is_dir() {
match fs::rename(src, dest_buf.as_path()) {
Ok(_) => {
- progress.processed += metadata.len();
+ progress._processed += metadata.len();
}
Err(_) => {
fs::create_dir(dest_buf.as_path())?;
@@ -199,13 +228,13 @@ impl IOWorkerThread {
if fs::rename(src, dest_buf.as_path()).is_err() {
fs::copy(src, dest_buf.as_path())?;
fs::remove_file(src)?;
- progress.processed += metadata.len();
+ progress._processed += metadata.len();
}
} else if file_type.is_symlink() {
let link_path = fs::read_link(src)?;
std::os::unix::fs::symlink(link_path, dest_buf)?;
fs::remove_file(src)?;
- progress.processed += metadata.len();
+ progress._processed += metadata.len();
}
Ok(())
}