1use crate::{
2 duplicate_finder, hashing::CalculateHash, output::Output, output_msg::OutputMsg,
3 output_type::OutputType, path_files,
4};
5use rayon::prelude::*;
6use std::{io::Stdout, iter::*, path::PathBuf, sync::mpsc, sync::mpsc::Sender};
7
8pub fn run(path: &str, is_progress_bar_enabled: bool, output_type: OutputType) {
9 let file_paths = path_files::get_all_file_paths(path);
10 let total_files_count = file_paths.len();
11 let (sender, receiver) = mpsc::channel();
12
13 let join_handle = std::thread::spawn(move || {
14 let mut output = Output::new(::std::io::stdout(), total_files_count as u64);
15 for item in receiver {
16 handle_output_msg(output_type, &mut output, item, is_progress_bar_enabled);
17 }
18 });
19
20 let join_handle2 = std::thread::spawn(move || {
21 let file_infos = file_paths
22 .into_iter()
23 .map(|path_buf| (path_buf, sender.clone()))
24 .collect::<Vec<(PathBuf, Sender<OutputMsg>)>>()
25 .into_par_iter()
26 .map(|pair| CalculateHash::new(pair.0, pair.1).calculate())
27 .collect::<Vec<_>>();
28
29 let duplicates = duplicate_finder::find_duplicates(file_infos);
30 sender.send(OutputMsg::Finish(duplicates)).unwrap();
31 });
32
33 join_handle2.join().unwrap();
34 join_handle.join().unwrap();
35}
36
37fn handle_output_msg(
38 output_type: OutputType,
39 output: &mut Output<Stdout>,
40 progress_msg: OutputMsg,
41 is_progress_bar_enabled: bool,
42) {
43 if is_progress_bar_enabled {
44 match progress_msg {
45 OutputMsg::Summary => output.update_summary_progress(),
46 OutputMsg::FileProgress(percent, path_buf) => {
47 output.update_file_progress(percent, &path_buf)
48 }
49 OutputMsg::Finish(duplicates) => output.set_finish(duplicates, output_type),
50 };
51 output.redraw();
52 } else {
53 match progress_msg {
54 OutputMsg::Finish(duplicates) => {
55 output.set_finish(duplicates, output_type);
56 output.redraw();
57 }
58 _ => {}
59 };
60 }
61}