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”
This commit is contained in:
Ben S 2016-10-29 19:07:43 +01:00
parent 54067bf765
commit a7e3456b0d
3 changed files with 14 additions and 14 deletions

View File

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

0
xtests/empty Normal file
View File

View File

@ -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_lhb || exit 1
$exa $testcases/files -lhB | diff -q - $results/files_lhb2 || 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 # Grid view tests
COLUMNS=40 $exa $testcases/files | diff -q - $results/files_40 || exit 1 COLUMNS=40 $exa $testcases/files | diff -q - $results/files_40 || exit 1
COLUMNS=80 $exa $testcases/files | diff -q - $results/files_80 || exit 1 COLUMNS=80 $exa $testcases/files | diff -q - $results/files_80 || exit 1