Repository: shssoichiro/oxipng (language: rust)

Issue:
`--recursive` flag trys to compress non png files
The `--recursive` flag, does also try to compress non png files (even it is not even an image). Is their any reason, why the files are not filtered by `.png` extension? This should speed up the recursive flag, since non `png` files need not be considered.

I can implement this, if this feature is wanted.


Discussion / hints:
This seems very sensible, I guess it's just never come up before. Feel free to open a PR 🙂
@LuckyTurtleDev If you _are_ implementing this, maybe consider an option to retain the current behavior, as well?
Note that regardless of how `--recursive` works, you can always use globbing to do the same thing. `**` for everything, `**/*.png` for just png files. So we really don't need two options for it.
(I realise this kind of globbing may be foreign to Windows users, but I'm pretty sure it does work in oxipng)
The problem is that due to the Windows globbing issues, `--recursive` is still the only way to recursively optimize folders. At least up until #536, which seems to work (I tested the build from CI PR).

Globbing is brittle on Windows (e.g., search "glob" in this repo's issues/PRs/commits & you'll see what I mean), b/c it's _not_ a shell/system function, so switches like this are preferred there.

Fix patch:
diff --git a/src/main.rs b/src/main.rs
index 79568f01..a83e4464 100644
--- a/src/main.rs
+++ b/src/main.rs
@@ -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")]
@@ -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),
@@ -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() {
@@ -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));


Test patch: