2014-05-25 16:14:50 +00:00
|
|
|
extern crate getopts;
|
|
|
|
|
2014-05-24 01:17:43 +00:00
|
|
|
use file::File;
|
2014-05-24 01:32:57 +00:00
|
|
|
use std::cmp::lexical_ordering;
|
2014-05-25 18:42:31 +00:00
|
|
|
use column::{Column, Permissions, FileName, FileSize, User, Group};
|
2014-05-24 01:17:43 +00:00
|
|
|
|
|
|
|
pub enum SortField {
|
2014-05-24 01:32:57 +00:00
|
|
|
Name, Extension, Size
|
2014-05-24 01:17:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
pub struct Options {
|
|
|
|
pub showInvisibles: bool,
|
|
|
|
pub sortField: SortField,
|
2014-05-24 20:45:24 +00:00
|
|
|
pub reverse: bool,
|
2014-05-26 10:08:33 +00:00
|
|
|
pub dirs: Vec<String>,
|
2014-05-24 01:17:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl SortField {
|
2014-05-26 10:50:46 +00:00
|
|
|
fn from_word(word: String) -> SortField {
|
2014-05-24 01:17:43 +00:00
|
|
|
match word.as_slice() {
|
|
|
|
"name" => Name,
|
|
|
|
"size" => Size,
|
2014-05-24 01:32:57 +00:00
|
|
|
"ext" => Extension,
|
2014-05-24 01:17:43 +00:00
|
|
|
_ => fail!("Invalid sorting order"),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Options {
|
2014-05-26 10:08:33 +00:00
|
|
|
pub fn getopts(args: Vec<String>) -> Result<Options, getopts::Fail_> {
|
2014-05-25 16:14:50 +00:00
|
|
|
let opts = ~[
|
|
|
|
getopts::optflag("a", "all", "show dot-files"),
|
|
|
|
getopts::optflag("r", "reverse", "reverse order of files"),
|
|
|
|
getopts::optopt("s", "sort", "field to sort by", "WORD"),
|
|
|
|
];
|
|
|
|
|
|
|
|
match getopts::getopts(args.tail(), opts) {
|
|
|
|
Err(f) => Err(f),
|
|
|
|
Ok(matches) => Ok(Options {
|
|
|
|
showInvisibles: matches.opt_present("all"),
|
|
|
|
reverse: matches.opt_present("reverse"),
|
|
|
|
sortField: matches.opt_str("sort").map(|word| SortField::from_word(word)).unwrap_or(Name),
|
|
|
|
dirs: matches.free,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-05-24 01:17:43 +00:00
|
|
|
pub fn show(&self, f: &File) -> bool {
|
|
|
|
if self.showInvisibles {
|
|
|
|
true
|
|
|
|
} else {
|
|
|
|
!f.name.starts_with(".")
|
|
|
|
}
|
|
|
|
}
|
2014-05-25 18:42:31 +00:00
|
|
|
|
2014-05-26 10:50:46 +00:00
|
|
|
pub fn transform_files<'a>(&self, unordered_files: Vec<File<'a>>) -> Vec<File<'a>> {
|
|
|
|
let mut files = unordered_files.clone();
|
|
|
|
|
|
|
|
match self.sortField {
|
|
|
|
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)
|
|
|
|
}),
|
|
|
|
}
|
|
|
|
|
|
|
|
if self.reverse {
|
|
|
|
files.reverse();
|
|
|
|
}
|
|
|
|
|
|
|
|
return files;
|
|
|
|
}
|
|
|
|
|
2014-05-25 18:42:31 +00:00
|
|
|
pub fn columns(&self) -> ~[Column] {
|
|
|
|
return ~[
|
|
|
|
Permissions,
|
|
|
|
FileSize(false),
|
|
|
|
User,
|
|
|
|
Group,
|
|
|
|
FileName,
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
2014-05-24 01:17:43 +00:00
|
|
|
}
|