Allow sorting by file extension

This commit is contained in:
Ben S 2014-05-24 02:32:57 +01:00
parent cb1b8bc2d7
commit 184de0ce79
2 changed files with 13 additions and 1 deletions

View File

@ -31,6 +31,11 @@ impl<'a> File<'a> {
return File { path: path, stat: stat, name: filename };
}
pub fn ext(&self) -> Option<&'a str> {
let re = regex!(r"\.(.+)$");
re.captures(self.name).map(|caps| caps.at(1))
}
pub fn is_dotfile(&self) -> bool {
self.name.starts_with(".")
}

View File

@ -1,7 +1,8 @@
use file::File;
use std::cmp::lexical_ordering;
pub enum SortField {
Name, Size
Name, Extension, Size
}
pub struct Options {
@ -14,6 +15,7 @@ impl SortField {
match word.as_slice() {
"name" => Name,
"size" => Size,
"ext" => Extension,
_ => fail!("Invalid sorting order"),
}
}
@ -22,6 +24,11 @@ impl SortField {
match *self {
Name => files.sort_by(|a, b| a.name.cmp(&b.name)),
Size => files.sort_by(|a, b| a.stat.size.cmp(&b.stat.size)),
Extension => files.sort_by(|a, b| {
let exts = a.ext().cmp(&b.ext());
let names = a.name.cmp(&b.name);
lexical_ordering(exts, names)
}),
}
}
}