Skip to content

Commit b6a238c

Browse files
skip non png files, if --recursive is used (#548)
Fix #547
1 parent 462e982 commit b6a238c

File tree

1 file changed

+12
-3
lines changed

1 file changed

+12
-3
lines changed

src/main.rs

+12-3
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ use oxipng::RowFilter;
2525
use oxipng::StripChunks;
2626
use oxipng::{InFile, OutFile};
2727
use rayon::prelude::*;
28+
use std::ffi::OsString;
2829
use std::fs::DirBuilder;
2930
use std::io::Write;
3031
#[cfg(feature = "zopfli")]
@@ -64,7 +65,7 @@ fn main() {
6465
)
6566
.arg(
6667
Arg::new("recursive")
67-
.help("Recurse into subdirectories")
68+
.help("Recurse into subdirectories and optimize all *.png/*.apng files")
6869
.short('r')
6970
.long("recursive")
7071
.action(ArgAction::SetTrue),
@@ -353,10 +354,10 @@ fn collect_files(
353354
out_dir: &Option<PathBuf>,
354355
out_file: &OutFile,
355356
recursive: bool,
356-
allow_stdin: bool,
357+
top_level: bool, //explicitly specify files
357358
) -> Vec<(InFile, OutFile)> {
358359
let mut in_out_pairs = Vec::new();
359-
let allow_stdin = allow_stdin && files.len() == 1;
360+
let allow_stdin = top_level && files.len() == 1;
360361
for input in files {
361362
let using_stdin = allow_stdin && input.to_str().map_or(false, |p| p == "-");
362363
if !using_stdin && input.is_dir() {
@@ -389,6 +390,14 @@ fn collect_files(
389390
let in_file = if using_stdin {
390391
InFile::StdIn
391392
} else {
393+
// Skip non png files if not given on top level
394+
if !top_level && {
395+
let extension = input.extension().map(|f| f.to_ascii_lowercase());
396+
extension != Some(OsString::from("png"))
397+
&& extension != Some(OsString::from("apng"))
398+
} {
399+
continue;
400+
}
392401
InFile::Path(input)
393402
};
394403
in_out_pairs.push((in_file, out_file));

0 commit comments

Comments
 (0)