exa/options.rs

119 lines
3.5 KiB
Rust
Raw Normal View History

2014-05-25 16:14:50 +00:00
extern crate getopts;
use file::File;
2014-05-24 01:32:57 +00:00
use std::cmp::lexical_ordering;
2014-06-22 07:09:16 +00:00
use column::{Column, Permissions, FileName, FileSize, User, Group, HardLinks, Inode, Blocks};
2014-06-01 12:07:45 +00:00
use std::ascii::StrAsciiExt;
pub enum SortField {
2014-05-24 01:32:57 +00:00
Name, Extension, Size
}
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-26 17:08:58 +00:00
pub columns: Vec<Column>,
2014-06-23 17:26:35 +00:00
pub header: bool,
}
impl SortField {
fn from_word(word: String) -> SortField {
match word.as_slice() {
"name" => Name,
"size" => Size,
2014-05-26 19:24:51 +00:00
"ext" => Extension,
_ => fail!("Invalid sorting order"),
}
}
}
impl Options {
2014-05-26 10:08:33 +00:00
pub fn getopts(args: Vec<String>) -> Result<Options, getopts::Fail_> {
let opts = [
2014-05-25 16:14:50 +00:00
getopts::optflag("a", "all", "show dot-files"),
getopts::optflag("b", "binary", "use binary prefixes in file sizes"),
2014-05-26 17:08:58 +00:00
getopts::optflag("g", "group", "show group as well as user"),
2014-06-23 17:26:35 +00:00
getopts::optflag("h", "header", "show a header row at the top"),
2014-06-22 06:44:00 +00:00
getopts::optflag("i", "inode", "show each file's inode number"),
getopts::optflag("l", "links", "show number of hard links"),
2014-05-25 16:14:50 +00:00
getopts::optflag("r", "reverse", "reverse order of files"),
getopts::optopt("s", "sort", "field to sort by", "WORD"),
2014-06-22 07:09:16 +00:00
getopts::optflag("S", "blocks", "show number of file system blocks"),
2014-05-25 16:14:50 +00:00
];
match getopts::getopts(args.tail(), opts) {
Err(f) => Err(f),
Ok(matches) => Ok(Options {
showInvisibles: matches.opt_present("all"),
reverse: matches.opt_present("reverse"),
2014-06-23 17:26:35 +00:00
header: matches.opt_present("header"),
2014-05-25 16:14:50 +00:00
sortField: matches.opt_str("sort").map(|word| SortField::from_word(word)).unwrap_or(Name),
dirs: matches.free.clone(),
columns: Options::columns(matches),
2014-05-25 16:14:50 +00:00
})
}
}
2014-05-26 17:08:58 +00:00
fn columns(matches: getopts::Matches) -> Vec<Column> {
2014-06-22 06:44:00 +00:00
let mut columns = vec![];
if matches.opt_present("inode") {
columns.push(Inode);
}
columns.push(Permissions);
if matches.opt_present("links") {
columns.push(HardLinks);
}
columns.push(FileSize(matches.opt_present("binary")));
2014-06-22 07:09:16 +00:00
if matches.opt_present("blocks") {
columns.push(Blocks);
}
columns.push(User);
2014-05-26 17:08:58 +00:00
if matches.opt_present("group") {
columns.push(Group);
}
columns.push(FileName);
return columns;
}
2014-05-26 19:24:51 +00:00
fn should_display(&self, f: &File) -> bool {
if self.showInvisibles {
true
} else {
!f.name.starts_with(".")
}
}
2014-05-25 18:42:31 +00:00
pub fn transform_files<'a>(&self, unordered_files: &'a Vec<File<'a>>) -> Vec<&'a File<'a>> {
let mut files: Vec<&'a File<'a>> = unordered_files.iter()
2014-05-26 19:24:51 +00:00
.filter(|&f| self.should_display(f))
.collect();
match self.sortField {
2014-06-01 10:54:31 +00:00
Name => files.sort_by(|a, b| a.parts.cmp(&b.parts)),
Size => files.sort_by(|a, b| a.stat.size.cmp(&b.stat.size)),
Extension => files.sort_by(|a, b| {
2014-06-01 12:07:45 +00:00
let exts = a.ext.map(|e| e.to_ascii_lower()).cmp(&b.ext.map(|e| e.to_ascii_lower()));
let names = a.name.to_ascii_lower().cmp(&b.name.to_ascii_lower());
lexical_ordering(exts, names)
}),
}
if self.reverse {
files.reverse();
}
return files;
}
}