diff --git a/file.rs b/file.rs index e0f11f5..e28fc76 100644 --- a/file.rs +++ b/file.rs @@ -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(".") } diff --git a/options.rs b/options.rs index d1ecfd9..4656a68 100644 --- a/options.rs +++ b/options.rs @@ -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) + }), } } }