From 90198502a3e9df59ebde00b687f594b44a28af80 Mon Sep 17 00:00:00 2001 From: sreedevk Date: Mon, 14 Jul 2025 12:53:52 +0000 Subject: [PATCH] added exclude file types. updated documentation to include exclude file types. --- README.md | 24 ++++++++++++++---------- src/params.rs | 44 +++++++++++++++++++++++++++++++++++++++++++- 2 files changed, 57 insertions(+), 11 deletions(-) diff --git a/README.md b/README.md index ab74588..f8a6074 100644 --- a/README.md +++ b/README.md @@ -15,16 +15,17 @@ Arguments: [scan_dir_path] Run Deduplicator on dir different from pwd (e.g., ~/Pictures ) Options: - -t, --types Filetypes to deduplicate [default = all] - -i, --interactive Delete files interactively - -m, --min-size Minimum filesize of duplicates to scan (e.g., 100B/1K/2M/3G/4T) [default: 1b] - -D, --max-depth Max Depth to scan while looking for duplicates - -d, --min-depth Min Depth to scan while looking for duplicates - -f, --follow-links Follow links while scanning directories - -s, --strict Guarantees that two files are duplicate (performs a full hash) - -p, --progress Show Progress spinners & metrics - -h, --help Print help - -V, --version Print version + -T, --exclude-types Exclude Filetypes [default = none] + -t, --types Filetypes to deduplicate [default = all] + -i, --interactive Delete files interactively + -m, --min-size Minimum filesize of duplicates to scan (e.g., 100B/1K/2M/3G/4T) [default: 1b] + -D, --max-depth Max Depth to scan while looking for duplicates + -d, --min-depth Min Depth to scan while looking for duplicates + -f, --follow-links Follow links while scanning directories + -s, --strict Guarantees that two files are duplicate (performs a full hash) + -p, --progress Show Progress spinners & metrics + -h, --help Print help + -V, --version Print version ``` ### Examples @@ -32,6 +33,9 @@ Options: # Scan for duplicates recursively from the current dir, only look for png, jpg & pdf file types & interactively delete files deduplicator -t pdf,jpg,png -i +# Scan for duplicates recursively from current dir, exclude png and jpg file types +deduplicator -T jpg,png + # Scan for duplicates recursively from the ~/Pictures dir, only look for png, jpeg, jpg & pdf file types & interactively delete files deduplicator ~/Pictures/ -t png,jpeg,jpg,pdf -i diff --git a/src/params.rs b/src/params.rs index c74013e..675ac41 100644 --- a/src/params.rs +++ b/src/params.rs @@ -2,10 +2,14 @@ use std::{fs, path::PathBuf}; use anyhow::Result; use clap::{Parser, ValueHint}; +use std::collections::HashSet; #[derive(Parser, Debug, Default, Clone)] #[command(author, version, about, long_about = None)] pub struct Params { + /// Exclude Filetypes [default = none] + #[arg(short = 'T', long)] + pub exclude_types: Option, /// Filetypes to deduplicate [default = all] #[arg(short, long)] pub types: Option, @@ -53,7 +57,45 @@ impl Params { Ok(dir) } + pub fn types_intersection(itypes: &str, xtypes: &str) -> String { + let iset = itypes + .split(",") + .map(String::from) + .collect::>(); + let xset = xtypes + .split(",") + .map(String::from) + .collect::>(); + + iset.difference(&xset) + .cloned() + .collect::>() + .join(",") + } + pub fn get_types(&self) -> Option { - self.types.clone() + match &self.types { + Some(itypes) => match &self.exclude_types { + Some(xtypes) => Some(Self::types_intersection(itypes, xtypes)), + None => Some(itypes.to_string()), + }, + None => self.exclude_types.as_ref().map(|xtypes| xtypes.to_string()), + } + } +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn mixing_include_and_exclude_types_works_as_expected() { + let params = Params { + types: Some(String::from("js,xml,ts,pdf,tiff")), + exclude_types: Some(String::from("js,ts,xml")), + ..Default::default() + }; + + assert_eq!(params.get_types(), Some(String::from("pdf,tiff"))); } }