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

Only engage the view when there are files to show

This changes the way that views are used to display the actual lists of files. It used to pass empty vectors to the view methods, which most of the time would not print anything because there are no files to list — except when there’s a header row which gets printed for no files.

By not calling the view method at all when there’s nothing to print, exa won’t ever print extra things in the view unless it needs to for a file.

This fixes #106 “Don’t print the header if the result set is empty”
Ben S 9 лет назад
Родитель
Сommit
a7e3456b0d
3 измененных файлов с 14 добавлено и 14 удалено
  1. 12 14
      src/exa.rs
  2. 0 0
      xtests/empty
  3. 2 0
      xtests/run.sh

+ 12 - 14
src/exa.rs

@@ -95,10 +95,7 @@ impl<'w, W: Write + 'w> Exa<'w, W> {
         let no_files = files.is_empty();
         let is_only_dir = dirs.len() == 1 && no_files;
 
-        if !no_files {
-            try!(self.print_files(None, files));
-        }
-
+        try!(self.print_files(None, files));
         self.print_dirs(dirs, no_files, is_only_dir)
     }
 
@@ -142,11 +139,7 @@ impl<'w, W: Write + 'w> Exa<'w, W> {
                     }
 
                     try!(self.print_files(Some(&dir), children));
-
-                    if !child_dirs.is_empty() {
-                        try!(self.print_dirs(child_dirs, false, false));
-                    }
-
+                    try!(self.print_dirs(child_dirs, false, false));
                     continue;
                 }
             }
@@ -161,11 +154,16 @@ impl<'w, W: Write + 'w> Exa<'w, W> {
     /// For various annoying logistical reasons, each one handles
     /// printing differently...
     fn print_files(&mut self, dir: Option<&Dir>, files: Vec<File>) -> IOResult<()> {
-        match self.options.view {
-            View::Grid(g)         => g.view(&files, self.writer),
-            View::Details(d)      => d.view(dir, files, self.writer),
-            View::GridDetails(gd) => gd.view(dir, files, self.writer),
-            View::Lines(l)        => l.view(files, self.writer),
+        if !files.is_empty() {
+            match self.options.view {
+                View::Grid(g)         => g.view(&files, self.writer),
+                View::Details(d)      => d.view(dir, files, self.writer),
+                View::GridDetails(gd) => gd.view(dir, files, self.writer),
+                View::Lines(l)        => l.view(files, self.writer),
+            }
+        }
+        else {
+            Ok(())
         }
     }
 }

+ 0 - 0
xtests/empty


+ 2 - 0
xtests/run.sh

@@ -18,6 +18,8 @@ $exa $testcases/files -lh  | diff -q - $results/files_lh    || exit 1
 $exa $testcases/files -lhb | diff -q - $results/files_lhb   || exit 1
 $exa $testcases/files -lhB | diff -q - $results/files_lhb2  || exit 1
 
+$exa $testcases/attributes/dirs/empty-with-attribute -lh | diff -q - $results/empty  || exit 1
+
 # Grid view tests
 COLUMNS=40  $exa $testcases/files | diff -q - $results/files_40   || exit 1
 COLUMNS=80  $exa $testcases/files | diff -q - $results/files_80   || exit 1