Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

skip non png files, if --recursive is used #548

Merged
merged 13 commits into from
Sep 25, 2023
15 changes: 12 additions & 3 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ use oxipng::RowFilter;
use oxipng::StripChunks;
use oxipng::{InFile, OutFile};
use rayon::prelude::*;
use std::ffi::OsString;
use std::fs::DirBuilder;
use std::io::Write;
#[cfg(feature = "zopfli")]
Expand Down Expand Up @@ -64,7 +65,7 @@ fn main() {
)
.arg(
Arg::new("recursive")
.help("Recurse into subdirectories")
.help("Recurse into subdirectories and optimize all *.png/*.apng files")
.short('r')
.long("recursive")
.action(ArgAction::SetTrue),
Expand Down Expand Up @@ -353,10 +354,10 @@ fn collect_files(
out_dir: &Option<PathBuf>,
out_file: &OutFile,
recursive: bool,
allow_stdin: bool,
top_level: bool, //explicitly specify files
) -> Vec<(InFile, OutFile)> {
let mut in_out_pairs = Vec::new();
let allow_stdin = allow_stdin && files.len() == 1;
let allow_stdin = top_level && files.len() == 1;
for input in files {
let using_stdin = allow_stdin && input.to_str().map_or(false, |p| p == "-");
if !using_stdin && input.is_dir() {
Expand Down Expand Up @@ -389,6 +390,14 @@ fn collect_files(
let in_file = if using_stdin {
InFile::StdIn
} else {
// Skip non png files if not given on top level
if !top_level && {
let extension = input.extension().map(|f| f.to_ascii_lowercase());
extension != Some(OsString::from("png"))
&& extension != Some(OsString::from("apng"))
} {
continue;
}
InFile::Path(input)
};
in_out_pairs.push((in_file, out_file));
Expand Down