summaryrefslogtreecommitdiffstats
path: root/src/io/io_observer.rs
blob: 93a1b15f9569825d3f26b7631ec94eafb539ad94 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
use std::path;
use std::thread;

use crate::io::FileOperationProgress;
use crate::util::format;

#[derive(Debug)]
pub struct IoWorkerObserver {
    pub handle: thread::JoinHandle<()>,
    pub progress: Option<FileOperationProgress>,
    msg: String,
    src: path::PathBuf,
    dest: path::PathBuf,
}

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) -> bool {
        matches!(self.handle.join(), Ok(_))
    }
    pub fn set_progress(&mut self, progress: FileOperationProgress) {
        self.progress = Some(progress);
    }
    pub fn update_msg(&mut self) {
        match self.progress.as_ref() {
            None => {}
            Some(progress) => {
                let op_str = progress.kind().actioning_str();
                let processed_size = format::file_size_to_string(progress.bytes_processed());
                let total_size = format::file_size_to_string(progress.total_bytes());

                let msg = format!(
                    "{} ({}/{}) ({}/{}) completed",
                    op_str,
                    progress.files_processed() + 1,
                    progress.total_files(),
                    processed_size,
                    total_size,
                );
                self.msg = msg;
            }
        }
    }
    pub fn get_msg(&self) -> &str {
        self.msg.as_str()
    }
    pub fn src_path(&self) -> &path::Path {
        self.src.as_path()
    }
    pub fn dest_path(&self) -> &path::Path {
        self.dest.as_path()
    }
}