Skip to content

Commit

Permalink
Always sort files the same way
Browse files Browse the repository at this point in the history
This fixes a bug where extra sorting options (dirs first, reverse) were not applied when listing in long mode. In other words, fixes #105.

The bug occurred because the sorting function only took Files, but the details view uses File eggs that only contain Files. This commit changes the sorting function to accept anything that AsRefs to File, and impls that on both File and Egg so the same function works for both.
  • Loading branch information
ogham committed Mar 31, 2016
1 parent eaa799c commit f6c5c89
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 6 deletions.
6 changes: 6 additions & 0 deletions src/file.rs
Original file line number Diff line number Diff line change
Expand Up @@ -399,6 +399,12 @@ impl<'dir> File<'dir> {
}
}

impl<'a> AsRef<File<'a>> for File<'a> {
fn as_ref(&self) -> &File<'a> {
&self
}
}

/// Extract the filename to display from a path, converting it from UTF-8
/// lossily, into a String.
///
Expand Down
8 changes: 5 additions & 3 deletions src/options.rs
Original file line number Diff line number Diff line change
Expand Up @@ -349,16 +349,18 @@ impl FileFilter {
}

/// Sort the files in the given vector based on the sort field option.
pub fn sort_files(&self, files: &mut Vec<File>) {
files.sort_by(|a, b| self.compare_files(a, b));
pub fn sort_files<'_, F>(&self, files: &mut Vec<F>)
where F: AsRef<File<'_>> {

files.sort_by(|a, b| self.compare_files(a.as_ref(), b.as_ref()));

if self.reverse {
files.reverse();
}

if self.list_dirs_first {
// This relies on the fact that `sort_by` is stable.
files.sort_by(|a, b| b.is_directory().cmp(&a.is_directory()));
files.sort_by(|a, b| b.as_ref().is_directory().cmp(&a.as_ref().is_directory()));
}
}

Expand Down
12 changes: 9 additions & 3 deletions src/output/details.rs
Original file line number Diff line number Diff line change
Expand Up @@ -229,12 +229,18 @@ impl Details {
let mut pool = Pool::new(num_cpus::get() as u32);
let mut file_eggs = Vec::new();

struct Egg<'_> {
struct Egg<'a> {
cells: Vec<TextCell>,
xattrs: Vec<Attribute>,
errors: Vec<(io::Error, Option<PathBuf>)>,
dir: Option<Dir>,
file: File<'_>,
file: File<'a>,
}

impl<'a> AsRef<File<'a>> for Egg<'a> {
fn as_ref(&self) -> &File<'a> {
&self.file
}
}

pool.scoped(|scoped| {
Expand Down Expand Up @@ -285,7 +291,7 @@ impl Details {
}
});

file_eggs.sort_by(|a, b| self.filter.compare_files(&a.file, &b.file));
self.filter.sort_files(&mut file_eggs);

let num_eggs = file_eggs.len();
for (index, egg) in file_eggs.into_iter().enumerate() {
Expand Down

0 comments on commit f6c5c89

Please sign in to comment.