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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
use crate::{
    convert, file_info::FileInfo, output_type::OutputType, progress_line::ProgressLine,
    tty::move_cursor_up,
};
use std::{io::Write, path::PathBuf};

pub struct Output<TWrite: Write> {
    top_line: ProgressLine,
    bottom_line: ProgressLine,
    writer: TWrite,
    duplicates: Option<String>,
    pub is_called: bool,
}

impl<TWrite: Write> Output<TWrite> {
    pub fn new(writer: TWrite, total_files_count: u64) -> Output<TWrite> {
        Output {
            top_line: ProgressLine {
                current: 0,
                message: String::new(),
                show_counters: false,
                show_message: true,
                total: 100,
                width: None,
            },
            bottom_line: ProgressLine {
                current: 0,
                message: String::new(),
                show_counters: true,
                show_message: false,
                total: total_files_count,
                width: None,
            },
            writer,
            duplicates: None,
            is_called: false,
        }
    }

    pub fn update_summary_progress(&mut self) {
        self.bottom_line.current += 1;
    }

    pub fn update_file_progress(&mut self, percent: u64, message: &PathBuf) {
        self.top_line.current = percent;
        self.top_line.message =
            convert::to_string_of_length(message, crate::constants::OUTPUT_PADDING);
    }

    pub fn set_finish(&mut self, duplicates: Vec<Vec<FileInfo>>, output_type: OutputType) {
        let string = convert::to_string_by_output_type(duplicates, output_type);
        self.duplicates = Some(string);
    }

    pub fn redraw(&mut self) {
        let mut out = String::new();

        if self.is_called {
            out += &move_cursor_up(2);
        }
        self.is_called = true;

        if let Some(duplicates) = &self.duplicates {
            out.push_str(&format!("\r{}\n", &self.top_line.to_empty_string()));
            out.push_str(&format!("\r{}\n", &self.bottom_line.to_empty_string()));

            out += &move_cursor_up(2);
            out.push_str(&format!("\r{}\n", duplicates))
        } else {
            out.push_str(&format!("\r{}\n", &self.top_line.to_string()));
            out.push_str(&format!("\r{}\n", &self.bottom_line.to_string()));
        }
        self.writer
            .write(out.as_bytes())
            .ok()
            .expect("write() fail");

        self.writer.flush().ok().expect("flush() fail");
    }
}