Просмотр исходного кода

Merge pull request #381 from Hulxv/listing-files-only

feat: Listing files only
Christina Sørensen 2 лет назад
Родитель
Сommit
c5b544c5ae

+ 1 - 0
README.md

@@ -286,6 +286,7 @@ eza’s options are almost, but not quite, entirely unlike `ls`’s.
 - **-s**, **--sort=(field)**: which field to sort by
 - **-s**, **--sort=(field)**: which field to sort by
 - **--group-directories-first**: list directories before other files
 - **--group-directories-first**: list directories before other files
 - **-D**, **--only-dirs**: list only directories
 - **-D**, **--only-dirs**: list only directories
+- **-f**, **--only-files**: list only files
 - **--git-ignore**: ignore files mentioned in `.gitignore`
 - **--git-ignore**: ignore files mentioned in `.gitignore`
 - **-I**, **--ignore-glob=(globs)**: glob patterns (pipe-separated) of files to ignore
 - **-I**, **--ignore-glob=(globs)**: glob patterns (pipe-separated) of files to ignore
 
 

+ 1 - 0
completions/fish/eza.fish

@@ -56,6 +56,7 @@ complete -c eza -s s -l sort -d "Which field to sort by" -x -a "
 
 
 complete -c eza -s I -l ignore-glob -d "Ignore files that match these glob patterns" -r
 complete -c eza -s I -l ignore-glob -d "Ignore files that match these glob patterns" -r
 complete -c eza -s D -l only-dirs -d "List only directories"
 complete -c eza -s D -l only-dirs -d "List only directories"
+complete -c eza -s f -l only-files -d "List only files"
 
 
 # Long view options
 # Long view options
 complete -c eza -s b -l binary -d "List file sizes with binary prefixes"
 complete -c eza -s b -l binary -d "List file sizes with binary prefixes"

+ 1 - 0
completions/zsh/_eza

@@ -29,6 +29,7 @@ __eza() {
         {-a,--all}"[Show hidden and 'dot' files. Use this twice to also show the '.' and '..' directories]" \
         {-a,--all}"[Show hidden and 'dot' files. Use this twice to also show the '.' and '..' directories]" \
         {-d,--list-dirs}"[List directories like regular files]" \
         {-d,--list-dirs}"[List directories like regular files]" \
         {-D,--only-dirs}"[List only directories]" \
         {-D,--only-dirs}"[List only directories]" \
+        {-f,--only-files}"[List only files]" \
         {-L,--level}"+[Limit the depth of recursion]" \
         {-L,--level}"+[Limit the depth of recursion]" \
         {-w,--width}"+[Limits column output of grid, 0 implies auto-width]" \
         {-w,--width}"+[Limits column output of grid, 0 implies auto-width]" \
         {-r,--reverse}"[Reverse the sort order]" \
         {-r,--reverse}"[Reverse the sort order]" \

+ 3 - 0
man/eza.1.md

@@ -119,6 +119,9 @@ Sort fields starting with a capital letter will sort uppercase before lowercase:
 `-D`, `--only-dirs`
 `-D`, `--only-dirs`
 : List only directories, not files.
 : List only directories, not files.
 
 
+`-f`, `--only-files`
+: List only files, not directories.
+
 
 
 LONG VIEW OPTIONS
 LONG VIEW OPTIONS
 =================
 =================

+ 33 - 10
src/fs/filter.rs

@@ -9,6 +9,23 @@ use crate::fs::DotFilter;
 use crate::fs::File;
 use crate::fs::File;
 
 
 
 
+
+/// Flags used to manage the **file filter** process
+#[derive(PartialEq, Eq, Debug, Clone)]
+pub enum FileFilterFlags {
+    /// Whether to reverse the sorting order. This would sort the largest
+    /// files first, or files starting with Z, or the most-recently-changed
+    /// ones, depending on the sort field.
+    Reverse,
+
+    /// Whether to only show directories.
+    OnlyDirs,
+
+    /// Whether to only show files.
+    OnlyFiles
+}
+
+
 /// The **file filter** processes a list of files before displaying them to
 /// The **file filter** processes a list of files before displaying them to
 /// the user, by removing files they don’t want to see, and putting the list
 /// the user, by removing files they don’t want to see, and putting the list
 /// in the desired order.
 /// in the desired order.
@@ -33,13 +50,8 @@ pub struct FileFilter {
     /// The metadata field to sort by.
     /// The metadata field to sort by.
     pub sort_field: SortField,
     pub sort_field: SortField,
 
 
-    /// Whether to reverse the sorting order. This would sort the largest
-    /// files first, or files starting with Z, or the most-recently-changed
-    /// ones, depending on the sort field.
-    pub reverse: bool,
-
-    /// Whether to only show directories.
-    pub only_dirs: bool,
+    // Flags that the file filtering process follow
+    pub flags: Vec<FileFilterFlags>,
 
 
     /// Which invisible “dot” files to include when listing a directory.
     /// Which invisible “dot” files to include when listing a directory.
     ///
     ///
@@ -66,11 +78,22 @@ impl FileFilter {
     /// Remove every file in the given vector that does *not* pass the
     /// Remove every file in the given vector that does *not* pass the
     /// filter predicate for files found inside a directory.
     /// filter predicate for files found inside a directory.
     pub fn filter_child_files(&self, files: &mut Vec<File<'_>>) {
     pub fn filter_child_files(&self, files: &mut Vec<File<'_>>) {
+        use FileFilterFlags::{OnlyDirs, OnlyFiles};
+        
         files.retain(|f| ! self.ignore_patterns.is_ignored(&f.name));
         files.retain(|f| ! self.ignore_patterns.is_ignored(&f.name));
 
 
-        if self.only_dirs {
-            files.retain(File::is_directory);
+        match (self.flags.contains(&OnlyDirs), self.flags.contains(&OnlyFiles)) {
+            (true, false) => {
+                // On pass -'-only-dirs' flag only
+                files.retain(File::is_directory);
+            }
+            (false, true) => {
+                // On pass -'-only-files' flag only
+                files.retain(File::is_file);
+            }
+            _ => {}
         }
         }
+       
     }
     }
 
 
     /// Remove every file in the given vector that does *not* pass the
     /// Remove every file in the given vector that does *not* pass the
@@ -96,7 +119,7 @@ impl FileFilter {
             self.sort_field.compare_files(a.as_ref(), b.as_ref())
             self.sort_field.compare_files(a.as_ref(), b.as_ref())
         });
         });
 
 
-        if self.reverse {
+        if self.flags.contains(&FileFilterFlags::Reverse) {
             files.reverse();
             files.reverse();
         }
         }
 
 

+ 15 - 3
src/options/filter.rs

@@ -1,7 +1,7 @@
 //! Parsing the options for `FileFilter`.
 //! Parsing the options for `FileFilter`.
 
 
 use crate::fs::DotFilter;
 use crate::fs::DotFilter;
-use crate::fs::filter::{FileFilter, SortField, SortCase, IgnorePatterns, GitIgnore};
+use crate::fs::filter::{FileFilter,FileFilterFlags, SortField, SortCase, IgnorePatterns, GitIgnore};
 
 
 use crate::options::{flags, OptionsError};
 use crate::options::{flags, OptionsError};
 use crate::options::parser::MatchedFlags;
 use crate::options::parser::MatchedFlags;
@@ -11,10 +11,22 @@ impl FileFilter {
 
 
     /// Determines which of all the file filter options to use.
     /// Determines which of all the file filter options to use.
     pub fn deduce(matches: &MatchedFlags<'_>) -> Result<Self, OptionsError> {
     pub fn deduce(matches: &MatchedFlags<'_>) -> Result<Self, OptionsError> {
+        use FileFilterFlags as FFF;
+        let mut filter_flags:Vec<FileFilterFlags> = vec![];
+            
+        for (has,flag) in &[
+            (matches.has(&flags::REVERSE)?, FFF::Reverse),
+            (matches.has(&flags::ONLY_DIRS)?, FFF::OnlyDirs),
+            (matches.has(&flags::ONLY_FILES)?, FFF::OnlyFiles),
+        ] {
+            if *has { 
+                filter_flags.push(flag.clone());
+            }
+        }
+
         Ok(Self {
         Ok(Self {
             list_dirs_first:  matches.has(&flags::DIRS_FIRST)?,
             list_dirs_first:  matches.has(&flags::DIRS_FIRST)?,
-            reverse:          matches.has(&flags::REVERSE)?,
-            only_dirs:        matches.has(&flags::ONLY_DIRS)?,
+            flags: filter_flags,
             sort_field:       SortField::deduce(matches)?,
             sort_field:       SortField::deduce(matches)?,
             dot_filter:       DotFilter::deduce(matches)?,
             dot_filter:       DotFilter::deduce(matches)?,
             ignore_patterns:  IgnorePatterns::deduce(matches)?,
             ignore_patterns:  IgnorePatterns::deduce(matches)?,

+ 2 - 1
src/options/flags.rs

@@ -34,6 +34,7 @@ pub static IGNORE_GLOB: Arg = Arg { short: Some(b'I'), long: "ignore-glob", take
 pub static GIT_IGNORE:  Arg = Arg { short: None, long: "git-ignore",           takes_value: TakesValue::Forbidden };
 pub static GIT_IGNORE:  Arg = Arg { short: None, long: "git-ignore",           takes_value: TakesValue::Forbidden };
 pub static DIRS_FIRST:  Arg = Arg { short: None, long: "group-directories-first",  takes_value: TakesValue::Forbidden };
 pub static DIRS_FIRST:  Arg = Arg { short: None, long: "group-directories-first",  takes_value: TakesValue::Forbidden };
 pub static ONLY_DIRS:   Arg = Arg { short: Some(b'D'), long: "only-dirs", takes_value: TakesValue::Forbidden };
 pub static ONLY_DIRS:   Arg = Arg { short: Some(b'D'), long: "only-dirs", takes_value: TakesValue::Forbidden };
+pub static ONLY_FILES:   Arg = Arg { short: Some(b'f'), long: "only-files", takes_value: TakesValue::Forbidden };
 const SORTS: Values = &[ "name", "Name", "size", "extension",
 const SORTS: Values = &[ "name", "Name", "size", "extension",
                          "Extension", "modified", "changed", "accessed",
                          "Extension", "modified", "changed", "accessed",
                          "created", "inode", "type", "none" ];
                          "created", "inode", "type", "none" ];
@@ -83,7 +84,7 @@ pub static ALL_ARGS: Args = Args(&[
     &COLOR, &COLOUR, &COLOR_SCALE, &COLOUR_SCALE, &WIDTH,
     &COLOR, &COLOUR, &COLOR_SCALE, &COLOUR_SCALE, &WIDTH,
 
 
     &ALL, &ALMOST_ALL, &LIST_DIRS, &LEVEL, &REVERSE, &SORT, &DIRS_FIRST,
     &ALL, &ALMOST_ALL, &LIST_DIRS, &LEVEL, &REVERSE, &SORT, &DIRS_FIRST,
-    &IGNORE_GLOB, &GIT_IGNORE, &ONLY_DIRS,
+    &IGNORE_GLOB, &GIT_IGNORE, &ONLY_DIRS, &ONLY_FILES,
 
 
     &BINARY, &BYTES, &GROUP, &NUMERIC, &HEADER, &ICONS, &INODE, &LINKS, &MODIFIED, &CHANGED,
     &BINARY, &BYTES, &GROUP, &NUMERIC, &HEADER, &ICONS, &INODE, &LINKS, &MODIFIED, &CHANGED,
     &BLOCKSIZE, &TIME, &ACCESSED, &CREATED, &TIME_STYLE, &HYPERLINK, &MOUNTS,
     &BLOCKSIZE, &TIME, &ACCESSED, &CREATED, &TIME_STYLE, &HYPERLINK, &MOUNTS,

+ 1 - 0
src/options/help.rs

@@ -35,6 +35,7 @@ FILTERING AND SORTING OPTIONS
   -s, --sort SORT_FIELD      which field to sort by
   -s, --sort SORT_FIELD      which field to sort by
   --group-directories-first  list directories before other files
   --group-directories-first  list directories before other files
   -D, --only-dirs            list only directories
   -D, --only-dirs            list only directories
+  -f, --only-files           list only files
   -I, --ignore-glob GLOBS    glob patterns (pipe-separated) of files to ignore";
   -I, --ignore-glob GLOBS    glob patterns (pipe-separated) of files to ignore";
 
 
 static GIT_FILTER_HELP: &str = "  \
 static GIT_FILTER_HELP: &str = "  \

+ 0 - 0
tests/cmd/only_file_unix.stderr


+ 19 - 0
tests/cmd/only_file_unix.stdout

@@ -0,0 +1,19 @@
+a
+b
+c
+d
+e
+f
+g
+h
+i
+image.jpg.img.c.rs.log.png
+index.svg
+j
+k
+l
+m
+n
+o
+p
+q

+ 2 - 0
tests/cmd/only_file_unix.toml

@@ -0,0 +1,2 @@
+bin.name = "eza"
+args = "tests/itest --only-files"