Parcourir la source

Allow passing in the --all option more than once

Benjamin Sago il y a 8 ans
Parent
commit
39fd905999
2 fichiers modifiés avec 36 ajouts et 5 suppressions
  1. 14 4
      src/options/filter.rs
  2. 22 1
      src/options/mod.rs

+ 14 - 4
src/options/filter.rs

@@ -60,7 +60,7 @@ pub struct FileFilter {
     ///   most of them are or whether they're still needed. Every file name
     ///   evaluation that goes through my home directory is slowed down by
     ///   this accumulated sludge.
-    dot_filter: DotFilter,
+    pub dot_filter: DotFilter,
 
     /// Glob patterns to ignore. Any file name that matches *any* of these
     /// patterns won't be displayed in the list.
@@ -87,6 +87,7 @@ impl FileFilter {
         match self.dot_filter {
             DotFilter::JustFiles => files.retain(|f| !f.is_dotfile()),
             DotFilter::ShowDotfiles => {/* keep all elements */},
+            DotFilter::ShowDotfilesAndDots => unimplemented!(),
         }
 
         files.retain(|f| !self.ignore_patterns.is_ignored(f));
@@ -253,8 +254,14 @@ impl SortField {
 }
 
 
+/// Usually files in Unix use a leading dot to be hidden or visible, but two
+/// entries in particular are "extra-hidden": `.` and `..`, which only become
+/// visible after an extra `-a` option.
 #[derive(PartialEq, Debug, Copy, Clone)]
-enum DotFilter {
+pub enum DotFilter {
+
+    /// Shows files, dotfiles, and `.` and `..`.
+    ShowDotfilesAndDots,
 
     /// Show files and dotfiles, but hide `.` and `..`.
     ShowDotfiles,
@@ -272,8 +279,11 @@ impl Default for DotFilter {
 
 impl DotFilter {
     pub fn deduce(matches: &getopts::Matches) -> DotFilter {
-        if matches.opt_present("all") { DotFilter::ShowDotfiles }
-                                 else { DotFilter::JustFiles }
+        match matches.opt_count("all") {
+            0 => DotFilter::JustFiles,
+            1 => DotFilter::ShowDotfiles,
+            _ => DotFilter::ShowDotfilesAndDots,
+        }
     }
 }
 

+ 22 - 1
src/options/mod.rs

@@ -69,7 +69,7 @@ impl Options {
 
         // Filtering and sorting options
         opts.optflag("",  "group-directories-first", "sort directories before other files");
-        opts.optflag("a", "all",         "don't hide hidden and 'dot' files");
+        opts.optflagmulti("a", "all",    "don't hide hidden and 'dot' files");
         opts.optflag("d", "list-dirs",   "list directories like regular files");
         opts.optopt ("L", "level",       "limit the depth of recursion", "DEPTH");
         opts.optflag("r", "reverse",     "reverse the sert order");
@@ -145,6 +145,7 @@ impl Options {
 #[cfg(test)]
 mod test {
     use super::{Options, Misfire, SortField, SortCase};
+    use super::filter::DotFilter;
     use fs::feature::xattr;
 
     fn is_helpful<T>(misfire: Result<T, Misfire>) -> bool {
@@ -277,4 +278,24 @@ mod test {
         let opts = Options::getopts(&[ "--level", "69105" ]);
         assert_eq!(opts.unwrap_err(), Misfire::Useless2("level", "recurse", "tree"))
     }
+
+
+    #[test]
+    fn nowt() {
+        let nothing: Vec<String> = Vec::new();
+        let dots = Options::getopts(&nothing).unwrap().0.filter.dot_filter;
+        assert_eq!(dots, DotFilter::JustFiles);
+    }
+
+    #[test]
+    fn all() {
+        let dots = Options::getopts(&[ "--all".to_string() ]).unwrap().0.filter.dot_filter;
+        assert_eq!(dots, DotFilter::ShowDotfiles);
+    }
+
+    #[test]
+    fn allall() {
+        let dots = Options::getopts(&[ "-a".to_string(), "-a".to_string() ]).unwrap().0.filter.dot_filter;
+        assert_eq!(dots, DotFilter::ShowDotfilesAndDots);
+    }
 }