1
0
Fork 0
mirror of synced 2024-05-21 12:52:37 +12:00

Smaller Printing

This commit is contained in:
Rafał Mikrut 2023-10-10 19:42:30 +02:00
parent edcd7d5b5c
commit cb5e455ced
14 changed files with 192 additions and 681 deletions

View file

@ -1,8 +1,7 @@
#![allow(clippy::needless_late_init)]
use std::process;
use clap::Parser;
use log::error;
use commands::Commands;
use czkawka_core::bad_extensions::BadExtensions;
@ -10,7 +9,7 @@ use czkawka_core::big_file::{BigFile, SearchMode};
use czkawka_core::broken_files::BrokenFiles;
use czkawka_core::common::{set_number_of_threads, setup_logger};
use czkawka_core::common_tool::{CommonData, DeleteMethod};
#[allow(unused_imports)] // It is used in release for print_results().
#[allow(unused_imports)] // It is used in release for print_results_to_output().
use czkawka_core::common_traits::*;
use czkawka_core::duplicate::DuplicateFinder;
use czkawka_core::empty_files::EmptyFiles;
@ -101,16 +100,14 @@ fn duplicates(duplicates: DuplicatesArgs) {
df.find_duplicates(None, None);
if let Some(file_name) = file_to_save.file_name() {
if !df.save_results_to_file(file_name) {
df.get_text_messages().print_messages();
process::exit(1);
if let Err(e) = df.print_results_to_file(file_name) {
error!("Failed to save results to file {e}");
}
}
if !cfg!(debug_assertions) {
df.print_results();
df.print_results_to_output();
}
df.print_results();
df.get_text_messages().print_messages();
}
@ -140,14 +137,13 @@ fn empty_folders(empty_folders: EmptyFoldersArgs) {
ef.find_empty_folders(None, None);
if let Some(file_name) = file_to_save.file_name() {
if !ef.save_results_to_file(file_name) {
ef.get_text_messages().print_messages();
process::exit(1);
if let Err(e) = ef.print_results_to_file(file_name) {
error!("Failed to save results to file {e}");
}
}
if !cfg!(debug_assertions) {
ef.print_results();
ef.print_results_to_output();
}
ef.get_text_messages().print_messages();
}
@ -190,14 +186,13 @@ fn biggest_files(biggest_files: BiggestFilesArgs) {
bf.find_big_files(None, None);
if let Some(file_name) = file_to_save.file_name() {
if !bf.save_results_to_file(file_name) {
bf.get_text_messages().print_messages();
process::exit(1);
if let Err(e) = bf.print_results_to_file(file_name) {
error!("Failed to save results to file {e}");
}
}
if !cfg!(debug_assertions) {
bf.print_results();
bf.print_results_to_output();
}
bf.get_text_messages().print_messages();
}
@ -235,14 +230,13 @@ fn empty_files(empty_files: EmptyFilesArgs) {
ef.find_empty_files(None, None);
if let Some(file_name) = file_to_save.file_name() {
if !ef.save_results_to_file(file_name) {
ef.get_text_messages().print_messages();
process::exit(1);
if let Err(e) = ef.print_results_to_file(file_name) {
error!("Failed to save results to file {e}");
}
}
if !cfg!(debug_assertions) {
ef.print_results();
ef.print_results_to_output();
}
ef.get_text_messages().print_messages();
}
@ -278,14 +272,13 @@ fn temporary(temporary: TemporaryArgs) {
tf.find_temporary_files(None, None);
if let Some(file_name) = file_to_save.file_name() {
if !tf.save_results_to_file(file_name) {
tf.get_text_messages().print_messages();
process::exit(1);
if let Err(e) = tf.print_results_to_file(file_name) {
error!("Failed to save results to file {e}");
}
}
if !cfg!(debug_assertions) {
tf.print_results();
tf.print_results_to_output();
}
tf.get_text_messages().print_messages();
}
@ -329,14 +322,13 @@ fn similar_images(similar_images: SimilarImagesArgs) {
sf.find_similar_images(None, None);
if let Some(file_name) = file_to_save.file_name() {
if !sf.save_results_to_file(file_name) {
sf.get_text_messages().print_messages();
process::exit(1);
if let Err(e) = sf.print_results_to_file(file_name) {
error!("Failed to save results to file {e}");
}
}
if !cfg!(debug_assertions) {
sf.print_results();
sf.print_results_to_output();
}
sf.get_text_messages().print_messages();
}
@ -378,14 +370,13 @@ fn same_music(same_music: SameMusicArgs) {
mf.find_same_music(None, None);
if let Some(file_name) = file_to_save.file_name() {
if !mf.save_results_to_file(file_name) {
mf.get_text_messages().print_messages();
process::exit(1);
if let Err(e) = mf.print_results_to_file(file_name) {
error!("Failed to save results to file {e}");
}
}
if !cfg!(debug_assertions) {
mf.print_results();
mf.print_results_to_output();
}
mf.get_text_messages().print_messages();
}
@ -422,14 +413,13 @@ fn invalid_symlinks(invalid_symlinks: InvalidSymlinksArgs) {
ifs.find_invalid_links(None, None);
if let Some(file_name) = file_to_save.file_name() {
if !ifs.save_results_to_file(file_name) {
ifs.get_text_messages().print_messages();
process::exit(1);
if let Err(e) = ifs.print_results_to_file(file_name) {
error!("Failed to save results to file {e}");
}
}
if !cfg!(debug_assertions) {
ifs.print_results();
ifs.print_results_to_output();
}
ifs.get_text_messages().print_messages();
}
@ -467,14 +457,13 @@ fn broken_files(broken_files: BrokenFilesArgs) {
br.find_broken_files(None, None);
if let Some(file_name) = file_to_save.file_name() {
if !br.save_results_to_file(file_name) {
br.get_text_messages().print_messages();
process::exit(1);
if let Err(e) = br.print_results_to_file(file_name) {
error!("Failed to save results to file {e}");
}
}
if !cfg!(debug_assertions) {
br.print_results();
br.print_results_to_output();
}
br.get_text_messages().print_messages();
}
@ -513,14 +502,13 @@ fn similar_videos(similar_videos: SimilarVideosArgs) {
vr.find_similar_videos(None, None);
if let Some(file_name) = file_to_save.file_name() {
if !vr.save_results_to_file(file_name) {
vr.get_text_messages().print_messages();
process::exit(1);
if let Err(e) = vr.print_results_to_file(file_name) {
error!("Failed to save results to file {e}");
}
}
if !cfg!(debug_assertions) {
vr.print_results();
vr.print_results_to_output();
}
vr.get_text_messages().print_messages();
}
@ -550,17 +538,16 @@ fn bad_extensions(bad_extensions: BadExtensionsArgs) {
#[cfg(target_family = "unix")]
be.set_exclude_other_filesystems(exclude_other_filesystems.exclude_other_filesystems);
be.find_bad_extensions_files(None, None);
if let Some(file_name) = file_to_save.file_name() {
if !be.save_results_to_file(file_name) {
be.get_text_messages().print_messages();
process::exit(1);
if let Err(e) = be.print_results_to_file(file_name) {
error!("Failed to save results to file {e}");
}
}
be.find_bad_extensions_files(None, None);
if !cfg!(debug_assertions) {
be.print_results();
be.print_results_to_output();
}
be.get_text_messages().print_messages();
}

View file

@ -1,7 +1,5 @@
use std::collections::{BTreeSet, HashMap};
use std::fs::File;
use std::io::prelude::*;
use std::io::BufWriter;
use std::mem;
use std::path::PathBuf;
use std::sync::atomic::{AtomicBool, AtomicUsize, Ordering};
@ -413,55 +411,20 @@ impl DebugPrint for BadExtensions {
}
}
impl SaveResults for BadExtensions {
#[fun_time(message = "save_results_to_file")]
fn save_results_to_file(&mut self, file_name: &str) -> bool {
let file_name: String = match file_name {
"" => "results.txt".to_string(),
k => k.to_string(),
};
let file_handler = match File::create(&file_name) {
Ok(t) => t,
Err(e) => {
self.common_data.text_messages.errors.push(format!("Failed to create file {file_name}, reason {e}"));
return false;
}
};
let mut writer = BufWriter::new(file_handler);
if let Err(e) = writeln!(
impl PrintResults for BadExtensions {
fn write_results<T: Write>(&self, writer: &mut T) -> std::io::Result<()> {
writeln!(
writer,
"Results of searching {:?} with excluded directories {:?} and excluded items {:?}",
self.common_data.directories.included_directories, self.common_data.directories.excluded_directories, self.common_data.excluded_items.items
) {
self.common_data
.text_messages
.errors
.push(format!("Failed to save results to file {file_name}, reason {e}"));
return false;
}
)?;
writeln!(writer, "Found {} files with invalid extension.\n", self.information.number_of_files_with_bad_extension)?;
if !self.bad_extensions_files.is_empty() {
writeln!(writer, "Found {} files with invalid extension.", self.information.number_of_files_with_bad_extension).unwrap();
for file_entry in &self.bad_extensions_files {
writeln!(writer, "{} ----- {}", file_entry.path.display(), file_entry.proper_extensions).unwrap();
}
} else {
write!(writer, "Not found any files with invalid extension.").unwrap();
}
true
}
}
impl PrintResults for BadExtensions {
#[fun_time(message = "print_results")]
fn print_results(&self) {
println!("Found {} files with invalid extension.\n", self.information.number_of_files_with_bad_extension);
for file_entry in &self.bad_extensions_files {
println!("{} ----- {}", file_entry.path.display(), file_entry.proper_extensions);
writeln!(writer, "{} ----- {}", file_entry.path.display(), file_entry.proper_extensions)?;
}
Ok(())
}
}

View file

@ -16,7 +16,7 @@ use rayon::prelude::*;
use crate::common::{check_folder_children, prepare_thread_handler_common, send_info_and_wait_for_ending_all_threads, split_path};
use crate::common_dir_traversal::{common_get_entry_data_metadata, common_read_dir, get_lowercase_name, get_modified_time, CheckingMethod, ProgressData, ToolType};
use crate::common_tool::{CommonData, CommonToolData, DeleteMethod};
use crate::common_traits::{DebugPrint, PrintResults, SaveResults};
use crate::common_traits::{DebugPrint, PrintResults};
#[derive(Clone, Debug)]
pub struct FileEntry {
@ -248,61 +248,28 @@ impl DebugPrint for BigFile {
}
}
impl SaveResults for BigFile {
fn save_results_to_file(&mut self, file_name: &str) -> bool {
let file_name: String = match file_name {
"" => "results.txt".to_string(),
k => k.to_string(),
};
let file_handler = match File::create(&file_name) {
Ok(t) => t,
Err(e) => {
self.common_data.text_messages.errors.push(format!("Failed to create file {file_name}, reason {e}"));
return false;
}
};
let mut writer = BufWriter::new(file_handler);
if let Err(e) = writeln!(
impl PrintResults for BigFile {
fn write_results<T: Write>(&self, writer: &mut T) -> std::io::Result<()> {
writeln!(
writer,
"Results of searching {:?} with excluded directories {:?} and excluded items {:?}",
self.common_data.directories.included_directories, self.common_data.directories.excluded_directories, self.common_data.excluded_items.items
) {
self.common_data
.text_messages
.errors
.push(format!("Failed to save results to file {file_name}, reason {e}"));
return false;
}
)?;
if self.information.number_of_real_files != 0 {
if self.search_mode == SearchMode::BiggestFiles {
write!(writer, "{} the biggest files.\n\n", self.information.number_of_real_files).unwrap();
writeln!(writer, "{} the biggest files.\n\n", self.information.number_of_real_files)?;
} else {
write!(writer, "{} the smallest files.\n\n", self.information.number_of_real_files).unwrap();
writeln!(writer, "{} the smallest files.\n\n", self.information.number_of_real_files)?;
}
for (size, file_entry) in &self.big_files {
writeln!(writer, "{} ({}) - {}", format_size(*size, BINARY), size, file_entry.path.display()).unwrap();
writeln!(writer, "{} ({}) - {}", format_size(*size, BINARY), size, file_entry.path.display())?;
}
} else {
write!(writer, "Not found any files.").unwrap();
}
true
}
}
impl PrintResults for BigFile {
fn print_results(&self) {
if self.search_mode == SearchMode::BiggestFiles {
println!("{} the biggest files.\n\n", self.information.number_of_real_files);
} else {
println!("{} the smallest files.\n\n", self.information.number_of_real_files);
}
for (size, file_entry) in &self.big_files {
println!("{} ({}) - {}", format_size(*size, BINARY), size, file_entry.path.display());
}
Ok(())
}
}

View file

@ -462,55 +462,24 @@ impl DebugPrint for BrokenFiles {
}
}
impl SaveResults for BrokenFiles {
#[fun_time(message = "save_results_to_file")]
fn save_results_to_file(&mut self, file_name: &str) -> bool {
let file_name: String = match file_name {
"" => "results.txt".to_string(),
k => k.to_string(),
};
let file_handler = match File::create(&file_name) {
Ok(t) => t,
Err(e) => {
self.common_data.text_messages.errors.push(format!("Failed to create file {file_name}, reason {e}"));
return false;
}
};
let mut writer = BufWriter::new(file_handler);
if let Err(e) = writeln!(
impl PrintResults for BrokenFiles {
fn write_results<T: Write>(&self, writer: &mut T) -> std::io::Result<()> {
writeln!(
writer,
"Results of searching {:?} with excluded directories {:?} and excluded items {:?}",
self.common_data.directories.included_directories, self.common_data.directories.excluded_directories, self.common_data.excluded_items.items
) {
self.common_data
.text_messages
.errors
.push(format!("Failed to save results to file {file_name}, reason {e}"));
return false;
}
)?;
if !self.broken_files.is_empty() {
writeln!(writer, "Found {} broken files.", self.information.number_of_broken_files).unwrap();
writeln!(writer, "Found {} broken files.", self.information.number_of_broken_files)?;
for file_entry in &self.broken_files {
writeln!(writer, "{} - {}", file_entry.path.display(), file_entry.error_string).unwrap();
writeln!(writer, "{} - {}", file_entry.path.display(), file_entry.error_string)?;
}
} else {
write!(writer, "Not found any broken files.").unwrap();
write!(writer, "Not found any broken files.")?;
}
true
}
}
impl PrintResults for BrokenFiles {
#[fun_time(message = "print_results")]
fn print_results(&self) {
println!("Found {} broken files.\n", self.information.number_of_broken_files);
for file_entry in &self.broken_files {
println!("{} - {}", file_entry.path.display(), file_entry.error_string);
}
Ok(())
}
}

View file

@ -1,15 +1,36 @@
use fun_time::fun_time;
use std::fs::File;
use std::io::{BufWriter, Write};
use std::path::Path;
pub trait DebugPrint {
fn debug_print(&self);
}
pub trait SaveResults {
fn save_results_to_file(&mut self, file_name: &str) -> bool;
}
pub trait PrintResults {
fn print_results(&self);
fn write_results<T: Write>(&self, writer: &mut T) -> std::io::Result<()>;
#[fun_time(message = "print_results_to_output")]
fn print_results_to_output(&self) {
let stdout = std::io::stdout();
let mut handle = stdout.lock();
self.write_results(&mut handle).unwrap();
handle.flush().unwrap();
}
#[fun_time(message = "print_results_to_file")]
fn print_results_to_file(&self, file_name: &str) -> std::io::Result<()> {
let file_name: String = match file_name {
"" => "results.txt".to_string(),
k => k.to_string(),
};
let file_handler = File::create(file_name)?;
let mut writer = BufWriter::new(file_handler);
self.write_results(&mut writer)?;
writer.flush()?;
Ok(())
}
}
pub trait ResultEntry {

View file

@ -962,56 +962,35 @@ impl DebugPrint for DuplicateFinder {
}
}
impl SaveResults for DuplicateFinder {
fn save_results_to_file(&mut self, file_name: &str) -> bool {
let file_name: String = match file_name {
"" => "results.txt".to_string(),
k => k.to_string(),
};
let file_handler = match File::create(&file_name) {
Ok(t) => t,
Err(e) => {
self.common_data.text_messages.errors.push(format!("Failed to create file {file_name}, reason {e}"));
return false;
}
};
let mut writer = BufWriter::new(file_handler);
if let Err(e) = writeln!(
impl PrintResults for DuplicateFinder {
fn write_results<T: Write>(&self, writer: &mut T) -> io::Result<()> {
writeln!(
writer,
"Results of searching {:?} with excluded directories {:?} and excluded items {:?}",
self.common_data.directories.included_directories, self.common_data.directories.excluded_directories, self.common_data.excluded_items.items
) {
self.common_data
.text_messages
.errors
.push(format!("Failed to save results to file {file_name}, reason {e}"));
return false;
}
)?;
match self.check_method {
CheckingMethod::Name => {
if !self.files_with_identical_names.is_empty() {
writeln!(
writer,
"-------------------------------------------------Files with same names-------------------------------------------------"
)
.unwrap();
)?;
writeln!(
writer,
"Found {} files in {} groups with same name(may have different content)",
self.information.number_of_duplicated_files_by_name, self.information.number_of_groups_by_name,
)
.unwrap();
)?;
for (name, vector) in self.files_with_identical_names.iter().rev() {
writeln!(writer, "Name - {} - {} files ", name, vector.len()).unwrap();
writeln!(writer, "Name - {} - {} files ", name, vector.len())?;
for j in vector {
writeln!(writer, "{}", j.path.display()).unwrap();
writeln!(writer, "{}", j.path.display())?;
}
writeln!(writer).unwrap();
writeln!(writer)?;
}
} else {
write!(writer, "Not found any files with same names.").unwrap();
write!(writer, "Not found any files with same names.")?;
}
}
CheckingMethod::SizeName => {
@ -1019,23 +998,21 @@ impl SaveResults for DuplicateFinder {
writeln!(
writer,
"-------------------------------------------------Files with same size and names-------------------------------------------------"
)
.unwrap();
)?;
writeln!(
writer,
"Found {} files in {} groups with same size and name(may have different content)",
self.information.number_of_duplicated_files_by_size_name, self.information.number_of_groups_by_size_name,
)
.unwrap();
)?;
for ((size, name), vector) in self.files_with_identical_size_names.iter().rev() {
writeln!(writer, "Name - {}, {} - {} files ", name, format_size(*size, BINARY), vector.len()).unwrap();
writeln!(writer, "Name - {}, {} - {} files ", name, format_size(*size, BINARY), vector.len())?;
for j in vector {
writeln!(writer, "{}", j.path.display()).unwrap();
writeln!(writer, "{}", j.path.display())?;
}
writeln!(writer).unwrap();
writeln!(writer)?;
}
} else {
write!(writer, "Not found any files with same size and names.").unwrap();
write!(writer, "Not found any files with same size and names.")?;
}
}
CheckingMethod::Size => {
@ -1043,24 +1020,22 @@ impl SaveResults for DuplicateFinder {
writeln!(
writer,
"-------------------------------------------------Files with same size-------------------------------------------------"
)
.unwrap();
)?;
writeln!(
writer,
"Found {} duplicated files which in {} groups which takes {}.",
self.information.number_of_duplicated_files_by_size,
self.information.number_of_groups_by_size,
format_size(self.information.lost_space_by_size, BINARY)
)
.unwrap();
)?;
for (size, vector) in self.files_with_identical_size.iter().rev() {
write!(writer, "\n---- Size {} ({}) - {} files \n", format_size(*size, BINARY), size, vector.len()).unwrap();
write!(writer, "\n---- Size {} ({}) - {} files \n", format_size(*size, BINARY), size, vector.len())?;
for file_entry in vector {
writeln!(writer, "{}", file_entry.path.display()).unwrap();
writeln!(writer, "{}", file_entry.path.display())?;
}
}
} else {
write!(writer, "Not found any duplicates.").unwrap();
write!(writer, "Not found any duplicates.")?;
}
}
CheckingMethod::Hash => {
@ -1068,114 +1043,30 @@ impl SaveResults for DuplicateFinder {
writeln!(
writer,
"-------------------------------------------------Files with same hashes-------------------------------------------------"
)
.unwrap();
)?;
writeln!(
writer,
"Found {} duplicated files which in {} groups which takes {}.",
self.information.number_of_duplicated_files_by_hash,
self.information.number_of_groups_by_hash,
format_size(self.information.lost_space_by_hash, BINARY)
)
.unwrap();
)?;
for (size, vectors_vector) in self.files_with_identical_hashes.iter().rev() {
for vector in vectors_vector {
writeln!(writer, "\n---- Size {} ({}) - {} files", format_size(*size, BINARY), size, vector.len()).unwrap();
writeln!(writer, "\n---- Size {} ({}) - {} files", format_size(*size, BINARY), size, vector.len())?;
for file_entry in vector {
writeln!(writer, "{}", file_entry.path.display()).unwrap();
writeln!(writer, "{}", file_entry.path.display())?;
}
}
}
} else {
write!(writer, "Not found any duplicates.").unwrap();
write!(writer, "Not found any duplicates.")?;
}
}
_ => panic!(),
}
true
}
}
impl PrintResults for DuplicateFinder {
fn print_results(&self) {
let mut number_of_files: u64 = 0;
let mut number_of_groups: u64 = 0;
match self.check_method {
CheckingMethod::Name => {
for i in &self.files_with_identical_names {
number_of_files += i.1.len() as u64;
number_of_groups += 1;
}
println!("Found {number_of_files} files in {number_of_groups} groups with same name(may have different content)",);
for (name, vector) in &self.files_with_identical_names {
println!("Name - {} - {} files ", name, vector.len());
for j in vector {
println!("{}", j.path.display());
}
println!();
}
}
CheckingMethod::SizeName => {
for i in &self.files_with_identical_size_names {
number_of_files += i.1.len() as u64;
number_of_groups += 1;
}
println!("Found {number_of_files} files in {number_of_groups} groups with same size and name(may have different content)",);
for ((size, name), vector) in &self.files_with_identical_size_names {
println!("Name - {}, {} - {} files ", name, format_size(*size, BINARY), vector.len());
for j in vector {
println!("{}", j.path.display());
}
println!();
}
}
CheckingMethod::Hash => {
for vector in self.files_with_identical_hashes.values() {
for j in vector {
number_of_files += j.len() as u64;
number_of_groups += 1;
}
}
println!(
"Found {} duplicated files in {} groups with same content which took {}:",
number_of_files,
number_of_groups,
format_size(self.information.lost_space_by_size, BINARY)
);
for (size, vector) in self.files_with_identical_hashes.iter().rev() {
for j in vector {
println!("Size - {} ({}) - {} files ", format_size(*size, BINARY), size, j.len());
for k in j {
println!("{}", k.path.display());
}
println!("----");
}
println!();
}
}
CheckingMethod::Size => {
for i in &self.files_with_identical_size {
number_of_files += i.1.len() as u64;
number_of_groups += 1;
}
println!(
"Found {} files in {} groups with same size(may have different content) which took {}:",
number_of_files,
number_of_groups,
format_size(self.information.lost_space_by_size, BINARY)
);
for (size, vector) in &self.files_with_identical_size {
println!("Size - {} ({}) - {} files ", format_size(*size, BINARY), size, vector.len());
for j in vector {
println!("{}", j.path.display());
}
println!();
}
}
_ => panic!(),
}
Ok(())
}
}

View file

@ -123,55 +123,24 @@ impl DebugPrint for EmptyFiles {
}
}
impl SaveResults for EmptyFiles {
#[fun_time(message = "save_results_to_file")]
fn save_results_to_file(&mut self, file_name: &str) -> bool {
let file_name: String = match file_name {
"" => "results.txt".to_string(),
k => k.to_string(),
};
let file_handler = match File::create(&file_name) {
Ok(t) => t,
Err(e) => {
self.common_data.text_messages.errors.push(format!("Failed to create file {file_name}, reason {e}"));
return false;
}
};
let mut writer = BufWriter::new(file_handler);
if let Err(e) = writeln!(
impl PrintResults for EmptyFiles {
fn write_results<T: Write>(&self, writer: &mut T) -> std::io::Result<()> {
writeln!(
writer,
"Results of searching {:?} with excluded directories {:?} and excluded items {:?}",
self.common_data.directories.included_directories, self.common_data.directories.excluded_directories, self.common_data.excluded_items.items
) {
self.common_data
.text_messages
.errors
.push(format!("Failed to save results to file {file_name}, reason {e}"));
return false;
}
)?;
if !self.empty_files.is_empty() {
writeln!(writer, "Found {} empty files.", self.information.number_of_empty_files).unwrap();
writeln!(writer, "Found {} empty files.", self.information.number_of_empty_files)?;
for file_entry in &self.empty_files {
writeln!(writer, "{}", file_entry.path.display()).unwrap();
writeln!(writer, "{}", file_entry.path.display())?;
}
} else {
write!(writer, "Not found any empty files.").unwrap();
write!(writer, "Not found any empty files.")?;
}
true
}
}
impl PrintResults for EmptyFiles {
#[fun_time(message = "print_results")]
fn print_results(&self) {
println!("Found {} empty files.\n", self.information.number_of_empty_files);
for file_entry in &self.empty_files {
println!("{}", file_entry.path.display());
}
Ok(())
}
}

View file

@ -11,7 +11,7 @@ use log::debug;
use crate::common_dir_traversal::{Collect, DirTraversalBuilder, DirTraversalResult, FolderEmptiness, FolderEntry, ProgressData, ToolType};
use crate::common_tool::{CommonData, CommonToolData};
use crate::common_traits::{DebugPrint, PrintResults, SaveResults};
use crate::common_traits::{DebugPrint, PrintResults};
pub struct EmptyFolder {
common_data: CommonToolData,
@ -144,62 +144,19 @@ impl DebugPrint for EmptyFolder {
}
}
impl SaveResults for EmptyFolder {
#[fun_time(message = "save_results_to_file")]
fn save_results_to_file(&mut self, file_name: &str) -> bool {
let file_name: String = match file_name {
"" => "results.txt".to_string(),
k => k.to_string(),
};
let file_handler = match File::create(&file_name) {
Ok(t) => t,
Err(e) => {
self.common_data.text_messages.errors.push(format!("Failed to create file {file_name}, reason {e}"));
return false;
}
};
let mut writer = BufWriter::new(file_handler);
if let Err(e) = writeln!(
writer,
"Results of searching {:?} with excluded directories {:?}",
self.common_data.directories.included_directories, self.common_data.directories.excluded_directories
) {
self.common_data
.text_messages
.errors
.push(format!("Failed to save results to file {file_name}, reason {e}"));
return false;
}
impl PrintResults for EmptyFolder {
fn write_results<T: Write>(&self, writer: &mut T) -> std::io::Result<()> {
if !self.empty_folder_list.is_empty() {
writeln!(
writer,
"-------------------------------------------------Empty folder list-------------------------------------------------"
)
.unwrap();
writeln!(writer, "Found {} empty folders", self.information.number_of_empty_folders).unwrap();
writeln!(writer, "--------------------------Empty folder list--------------------------")?;
writeln!(writer, "Found {} empty folders", self.information.number_of_empty_folders)?;
for name in self.empty_folder_list.keys() {
writeln!(writer, "{}", name.display()).unwrap();
writeln!(writer, "{}", name.display())?;
}
} else {
write!(writer, "Not found any empty folders.").unwrap();
write!(writer, "Not found any empty folders.")?;
}
true
}
}
impl PrintResults for EmptyFolder {
#[fun_time(message = "print_results")]
fn print_results(&self) {
if !self.empty_folder_list.is_empty() {
println!("Found {} empty folders", self.empty_folder_list.len());
}
for name in self.empty_folder_list.keys() {
println!("{}", name.display());
}
Ok(())
}
}

View file

@ -106,37 +106,10 @@ impl DebugPrint for InvalidSymlinks {
}
}
impl SaveResults for InvalidSymlinks {
#[fun_time(message = "save_results_to_file")]
fn save_results_to_file(&mut self, file_name: &str) -> bool {
let file_name: String = match file_name {
"" => "results.txt".to_string(),
k => k.to_string(),
};
let file_handler = match File::create(&file_name) {
Ok(t) => t,
Err(e) => {
self.common_data.text_messages.errors.push(format!("Failed to create file {file_name}, reason {e}"));
return false;
}
};
let mut writer = BufWriter::new(file_handler);
if let Err(e) = writeln!(
writer,
"Results of searching {:?} with excluded directories {:?} and excluded items {:?}",
self.common_data.directories.included_directories, self.common_data.directories.excluded_directories, self.common_data.excluded_items.items
) {
self.common_data
.text_messages
.errors
.push(format!("Failed to save results to file {file_name}, reason {e}"));
return false;
}
impl PrintResults for InvalidSymlinks {
fn write_results<T: Write>(&self, writer: &mut T) -> std::io::Result<()> {
if !self.invalid_symlinks.is_empty() {
writeln!(writer, "Found {} invalid symlinks.", self.information.number_of_invalid_symlinks).unwrap();
writeln!(writer, "Found {} invalid symlinks.", self.information.number_of_invalid_symlinks)?;
for file_entry in &self.invalid_symlinks {
writeln!(
writer,
@ -147,30 +120,13 @@ impl SaveResults for InvalidSymlinks {
ErrorType::InfiniteRecursion => "Infinite Recursion",
ErrorType::NonExistentFile => "Non Existent File",
}
)
.unwrap();
)?;
}
} else {
write!(writer, "Not found any invalid symlinks.").unwrap();
write!(writer, "Not found any invalid symlinks.")?;
}
true
}
}
impl PrintResults for InvalidSymlinks {
fn print_results(&self) {
println!("Found {} invalid symlinks.\n", self.information.number_of_invalid_symlinks);
for file_entry in &self.invalid_symlinks {
println!(
"{}\t\t{}\t\t{}",
file_entry.path.display(),
file_entry.symlink_info.clone().expect("invalid traversal result").destination_path.display(),
match file_entry.symlink_info.clone().expect("invalid traversal result").type_of_error {
ErrorType::InfiniteRecursion => "Infinite Recursion",
ErrorType::NonExistentFile => "Non Existent File",
}
);
}
Ok(())
}
}

View file

@ -907,40 +907,13 @@ impl DebugPrint for SameMusic {
}
}
impl SaveResults for SameMusic {
#[fun_time(message = "save_results_to_file")]
fn save_results_to_file(&mut self, file_name: &str) -> bool {
let file_name: String = match file_name {
"" => "results.txt".to_string(),
k => k.to_string(),
};
let file_handler = match File::create(&file_name) {
Ok(t) => t,
Err(e) => {
self.common_data.text_messages.errors.push(format!("Failed to create file {file_name}, reason {e}"));
return false;
}
};
let mut writer = BufWriter::new(file_handler);
if let Err(e) = writeln!(
writer,
"Results of searching {:?} with excluded directories {:?} and excluded items {:?}",
self.common_data.directories.included_directories, self.common_data.directories.excluded_directories, self.common_data.excluded_items.items
) {
self.common_data
.text_messages
.errors
.push(format!("Failed to save results to file {file_name}, reason {e}"));
return false;
}
impl PrintResults for SameMusic {
fn write_results<T: Write>(&self, writer: &mut T) -> std::io::Result<()> {
if !self.duplicated_music_entries.is_empty() {
writeln!(writer, "{} music files which have similar friends\n\n.", self.duplicated_music_entries.len()).unwrap();
writeln!(writer, "{} music files which have similar friends\n\n.", self.duplicated_music_entries.len())?;
for vec_file_entry in &self.duplicated_music_entries {
writeln!(writer, "Found {} music files which have similar friends", vec_file_entry.len()).unwrap();
writeln!(writer, "Found {} music files which have similar friends", vec_file_entry.len())?;
for file_entry in vec_file_entry {
writeln!(
writer,
@ -958,32 +931,10 @@ impl SaveResults for SameMusic {
writeln!(writer).unwrap();
}
} else {
write!(writer, "Not found any similar music files.").unwrap();
write!(writer, "Not found any similar music files.")?;
}
true
}
}
impl PrintResults for SameMusic {
#[fun_time(message = "print_results")]
fn print_results(&self) {
println!("Found {} similar music files.\n", self.duplicated_music_entries.len());
for vec_file_entry in &self.duplicated_music_entries {
for file_entry in vec_file_entry {
println!(
"TT: {} - TA: {} - Y: {} - L: {} - G: {} - B: {} - P: {}",
file_entry.track_title,
file_entry.track_artist,
file_entry.year,
file_entry.length,
file_entry.genre,
file_entry.bitrate,
file_entry.path.display()
);
}
println!();
}
Ok(())
}
}

View file

@ -26,7 +26,7 @@ use crate::common::{
use crate::common_cache::{get_similar_images_cache_file, load_cache_from_file_generalized_by_path, save_cache_to_file_generalized};
use crate::common_dir_traversal::{common_get_entry_data_metadata, common_read_dir, get_lowercase_name, get_modified_time, CheckingMethod, ProgressData, ToolType};
use crate::common_tool::{CommonData, CommonToolData};
use crate::common_traits::{DebugPrint, PrintResults, ResultEntry, SaveResults};
use crate::common_traits::{DebugPrint, PrintResults, ResultEntry};
use crate::flc;
type ImHash = Vec<u8>;
@ -831,40 +831,13 @@ impl DebugPrint for SimilarImages {
}
}
impl SaveResults for SimilarImages {
#[fun_time(message = "save_results_to_file")]
fn save_results_to_file(&mut self, file_name: &str) -> bool {
let file_name: String = match file_name {
"" => "results.txt".to_string(),
k => k.to_string(),
};
let file_handler = match File::create(&file_name) {
Ok(t) => t,
Err(e) => {
self.common_data.text_messages.errors.push(format!("Failed to create file {file_name}, reason {e}"));
return false;
}
};
let mut writer = BufWriter::new(file_handler);
if let Err(e) = writeln!(
writer,
"Results of searching {:?} with excluded directories {:?} and excluded items {:?}",
self.common_data.directories.included_directories, self.common_data.directories.excluded_directories, self.common_data.excluded_items.items
) {
self.common_data
.text_messages
.errors
.push(format!("Failed to save results to file {file_name}, reason {e}"));
return false;
}
impl PrintResults for SimilarImages {
fn write_results<T: Write>(&self, writer: &mut T) -> std::io::Result<()> {
if !self.similar_vectors.is_empty() {
write!(writer, "{} images which have similar friends\n\n", self.similar_vectors.len()).unwrap();
write!(writer, "{} images which have similar friends\n\n", self.similar_vectors.len())?;
for struct_similar in &self.similar_vectors {
writeln!(writer, "Found {} images which have similar friends", struct_similar.len()).unwrap();
writeln!(writer, "Found {} images which have similar friends", struct_similar.len())?;
for file_entry in struct_similar {
writeln!(
writer,
@ -873,38 +846,15 @@ impl SaveResults for SimilarImages {
file_entry.dimensions,
format_size(file_entry.size, BINARY),
get_string_from_similarity(&file_entry.similarity, self.hash_size)
)
.unwrap();
)?;
}
writeln!(writer).unwrap();
writeln!(writer)?;
}
} else {
write!(writer, "Not found any similar images.").unwrap();
write!(writer, "Not found any similar images.")?;
}
true
}
}
impl PrintResults for SimilarImages {
#[fun_time(message = "print_results")]
fn print_results(&self) {
if !self.similar_vectors.is_empty() {
println!("Found {} images which have similar friends", self.similar_vectors.len());
for vec_file_entry in &self.similar_vectors {
for file_entry in vec_file_entry {
println!(
"{} - {} - {} - {}",
file_entry.path.display(),
file_entry.dimensions,
format_size(file_entry.size, BINARY),
get_string_from_similarity(&file_entry.similarity, self.hash_size)
);
}
println!();
}
}
Ok(())
}
}

View file

@ -19,7 +19,7 @@ use crate::common::{check_folder_children, prepare_thread_handler_common, send_i
use crate::common_cache::{get_similar_videos_cache_file, load_cache_from_file_generalized_by_path, save_cache_to_file_generalized};
use crate::common_dir_traversal::{common_get_entry_data_metadata, common_read_dir, get_lowercase_name, get_modified_time, CheckingMethod, ProgressData, ToolType};
use crate::common_tool::{CommonData, CommonToolData};
use crate::common_traits::{DebugPrint, PrintResults, ResultEntry, SaveResults};
use crate::common_traits::{DebugPrint, PrintResults, ResultEntry};
use crate::flc;
use crate::localizer_core::generate_translation_hashmap;
@ -423,66 +423,23 @@ impl DebugPrint for SimilarVideos {
}
}
impl SaveResults for SimilarVideos {
#[fun_time(message = "save_results_to_file")]
fn save_results_to_file(&mut self, file_name: &str) -> bool {
let file_name: String = match file_name {
"" => "results.txt".to_string(),
k => k.to_string(),
};
let file_handler = match File::create(&file_name) {
Ok(t) => t,
Err(e) => {
self.common_data.text_messages.errors.push(format!("Failed to create file {file_name}, reason {e}"));
return false;
}
};
let mut writer = BufWriter::new(file_handler);
if let Err(e) = writeln!(
writer,
"Results of searching {:?} with excluded directories {:?} and excluded items {:?}",
self.common_data.directories.included_directories, self.common_data.directories.excluded_directories, self.common_data.excluded_items.items
) {
self.common_data
.text_messages
.errors
.push(format!("Failed to save results to file {file_name}, reason {e}"));
return false;
}
impl PrintResults for SimilarVideos {
fn write_results<T: Write>(&self, writer: &mut T) -> std::io::Result<()> {
if !self.similar_vectors.is_empty() {
write!(writer, "{} videos which have similar friends\n\n", self.similar_vectors.len()).unwrap();
write!(writer, "{} videos which have similar friends\n\n", self.similar_vectors.len())?;
for struct_similar in &self.similar_vectors {
writeln!(writer, "Found {} videos which have similar friends", struct_similar.len()).unwrap();
writeln!(writer, "Found {} videos which have similar friends", struct_similar.len())?;
for file_entry in struct_similar {
writeln!(writer, "{} - {}", file_entry.path.display(), format_size(file_entry.size, BINARY)).unwrap();
writeln!(writer, "{} - {}", file_entry.path.display(), format_size(file_entry.size, BINARY))?;
}
writeln!(writer).unwrap();
writeln!(writer)?;
}
} else {
write!(writer, "Not found any similar videos.").unwrap();
write!(writer, "Not found any similar videos.")?;
}
true
}
}
impl PrintResults for SimilarVideos {
#[fun_time(message = "print_results")]
fn print_results(&self) {
if !self.similar_vectors.is_empty() {
println!("Found {} videos which have similar friends", self.similar_vectors.len());
for vec_file_entry in &self.similar_vectors {
for file_entry in vec_file_entry {
println!("{} - {}", file_entry.path.display(), format_size(file_entry.size, BINARY));
}
println!();
}
}
Ok(())
}
}

View file

@ -191,55 +191,20 @@ impl Temporary {
}
}
impl SaveResults for Temporary {
#[fun_time(message = "save_results_to_file")]
fn save_results_to_file(&mut self, file_name: &str) -> bool {
let file_name: String = match file_name {
"" => "results.txt".to_string(),
k => k.to_string(),
};
let file_handler = match File::create(&file_name) {
Ok(t) => t,
Err(e) => {
self.common_data.text_messages.errors.push(format!("Failed to create file {file_name}, reason {e}"));
return false;
}
};
let mut writer = BufWriter::new(file_handler);
if let Err(e) = writeln!(
impl PrintResults for Temporary {
fn write_results<T: Write>(&self, writer: &mut T) -> std::io::Result<()> {
writeln!(
writer,
"Results of searching {:?} with excluded directories {:?} and excluded items {:?}",
self.common_data.directories.included_directories, self.common_data.directories.excluded_directories, self.common_data.excluded_items.items
) {
self.common_data
.text_messages
.errors
.push(format!("Failed to save results to file {file_name}, reason {e}"));
return false;
}
)?;
writeln!(writer, "Found {} temporary files.\n", self.information.number_of_temporary_files)?;
if !self.temporary_files.is_empty() {
writeln!(writer, "Found {} temporary files.", self.information.number_of_temporary_files).unwrap();
for file_entry in &self.temporary_files {
writeln!(writer, "{}", file_entry.path.display()).unwrap();
}
} else {
write!(writer, "Not found any temporary files.").unwrap();
}
true
}
}
impl PrintResults for Temporary {
#[fun_time(message = "print_results")]
fn print_results(&self) {
println!("Found {} temporary files.\n", self.information.number_of_temporary_files);
for file_entry in &self.temporary_files {
println!("{}", file_entry.path.display());
writeln!(writer, "{}", file_entry.path.display())?;
}
Ok(())
}
}

View file

@ -2,11 +2,10 @@ use std::cell::RefCell;
use std::collections::HashMap;
use std::rc::Rc;
use czkawka_core::common_traits::PrintResults;
use gtk4::prelude::*;
use gtk4::{Button, Entry};
use czkawka_core::common_traits::SaveResults;
use crate::flg;
use crate::gui_structs::gui_data::GuiData;
use crate::help_functions::BottomButtonsEnum;
@ -33,63 +32,72 @@ pub fn connect_button_save(gui_data: &GuiData) {
buttons_save.connect_clicked(move |_| {
let file_name;
match to_notebook_main_enum(notebook_main.current_page().unwrap()) {
let result = match to_notebook_main_enum(notebook_main.current_page().unwrap()) {
NotebookMainEnum::Duplicate => {
file_name = "results_duplicates.txt";
shared_duplication_state.borrow_mut().save_results_to_file(file_name);
shared_duplication_state.borrow_mut().print_results_to_file(file_name)
}
NotebookMainEnum::EmptyDirectories => {
file_name = "results_empty_folder.txt";
shared_empty_folders_state.borrow_mut().save_results_to_file(file_name);
shared_empty_folders_state.borrow_mut().print_results_to_file(file_name)
}
NotebookMainEnum::EmptyFiles => {
file_name = "results_empty_files.txt";
shared_empty_files_state.borrow_mut().save_results_to_file(file_name);
shared_empty_files_state.borrow_mut().print_results_to_file(file_name)
}
NotebookMainEnum::Temporary => {
file_name = "results_temporary_files.txt";
shared_temporary_files_state.borrow_mut().save_results_to_file(file_name);
shared_temporary_files_state.borrow_mut().print_results_to_file(file_name)
}
NotebookMainEnum::BigFiles => {
file_name = "results_big_files.txt";
shared_big_files_state.borrow_mut().save_results_to_file(file_name);
shared_big_files_state.borrow_mut().print_results_to_file(file_name)
}
NotebookMainEnum::SimilarImages => {
file_name = "results_similar_images.txt";
shared_similar_images_state.borrow_mut().save_results_to_file(file_name);
shared_similar_images_state.borrow_mut().print_results_to_file(file_name)
}
NotebookMainEnum::SimilarVideos => {
file_name = "results_similar_videos.txt";
shared_similar_videos_state.borrow_mut().save_results_to_file(file_name);
shared_similar_videos_state.borrow_mut().print_results_to_file(file_name)
}
NotebookMainEnum::SameMusic => {
file_name = "results_same_music.txt";
shared_same_music_state.borrow_mut().save_results_to_file(file_name);
shared_same_music_state.borrow_mut().print_results_to_file(file_name)
}
NotebookMainEnum::Symlinks => {
file_name = "results_invalid_symlinks.txt";
shared_same_invalid_symlinks.borrow_mut().save_results_to_file(file_name);
shared_same_invalid_symlinks.borrow_mut().print_results_to_file(file_name)
}
NotebookMainEnum::BrokenFiles => {
file_name = "results_broken_files.txt";
shared_broken_files_state.borrow_mut().save_results_to_file(file_name);
shared_broken_files_state.borrow_mut().print_results_to_file(file_name)
}
NotebookMainEnum::BadExtensions => {
file_name = "results_bad_extensions.txt";
shared_bad_extensions_state.borrow_mut().save_results_to_file(file_name);
shared_bad_extensions_state.borrow_mut().print_results_to_file(file_name)
}
};
match result {
Ok(()) => (),
Err(e) => {
entry_info.set_text(&format!("Failed to save results to file {e}"));
return;
}
}
post_save_things(
file_name,
&to_notebook_main_enum(notebook_main.current_page().unwrap()),