add flag to allow compressing files with non png extensions; CC @TPS

This commit is contained in:
LuckyTurtleDev 2023-08-06 12:52:27 +02:00
parent 9c0bd838f3
commit 92b43d97c2

View file

@ -70,6 +70,12 @@ fn main() {
.long("recursive") .long("recursive")
.action(ArgAction::SetTrue), .action(ArgAction::SetTrue),
) )
.arg(
Arg::new("include-non-png")
.help("compress also files with non png extensions if --recursive flag is used")
.long("include-non-png")
.action(ArgAction::SetTrue),
)
.arg( .arg(
Arg::new("output_dir") Arg::new("output_dir")
.help("Write output file(s) to <directory>") .help("Write output file(s) to <directory>")
@ -324,6 +330,7 @@ Heuristic filter selection strategies:
&out_dir, &out_dir,
&out_file, &out_file,
matches.get_flag("recursive"), matches.get_flag("recursive"),
matches.get_flag("include-non-png"),
true, true,
); );
@ -354,6 +361,7 @@ fn collect_files(
out_dir: &Option<PathBuf>, out_dir: &Option<PathBuf>,
out_file: &OutFile, out_file: &OutFile,
recursive: bool, recursive: bool,
include_non_png: bool,
allow_stdin: bool, allow_stdin: bool,
) -> Vec<(InFile, OutFile)> { ) -> Vec<(InFile, OutFile)> {
let mut in_out_pairs = Vec::new(); let mut in_out_pairs = Vec::new();
@ -365,8 +373,14 @@ fn collect_files(
match input.read_dir() { match input.read_dir() {
Ok(dir) => { Ok(dir) => {
let files = dir.filter_map(|x| x.ok().map(|x| x.path())).collect(); let files = dir.filter_map(|x| x.ok().map(|x| x.path())).collect();
in_out_pairs in_out_pairs.extend(collect_files(
.extend(collect_files(files, out_dir, out_file, recursive, false)); files,
out_dir,
out_file,
recursive,
include_non_png,
false,
));
} }
Err(e) => { Err(e) => {
warn!("{}: {}", input.display(), e); warn!("{}: {}", input.display(), e);
@ -388,7 +402,7 @@ fn collect_files(
} else { } else {
//skip non png files if recusive flag is used. //skip non png files if recusive flag is used.
//(still allow the user to convert a non png file if recusive flag is not used) //(still allow the user to convert a non png file if recusive flag is not used)
if Some(OsStr::new("png")) != input.extension() && recursive { if Some(OsStr::new("png")) != input.extension() && recursive && !include_non_png {
continue; continue;
} }
InFile::Path(input) InFile::Path(input)