summaryrefslogtreecommitdiffstats
path: root/src/io/io_observer.rs
blob: 532b6f5c18589799e662da810a2d3752f3ba2bff (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
63
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,
}

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: 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());
                let op_str = match progress.kind() {
                    FileOp::Cut => "Moving",
                    FileOp::Copy => "Copying",
                };

                let msg = format!(
                    "{} ({}/{}) {} completed",
                    op_str,
                    progress.index() + 1,
                    progress.len(),
                    size_str
                );
                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()
    }
}