2014-05-25 16:14:50 +00:00
|
|
|
extern crate getopts;
|
|
|
|
|
2014-05-24 01:17:43 +00:00
|
|
|
use file::File;
|
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;
|
2014-07-22 19:47:30 +00:00
|
|
|
use term;
|
2014-05-24 01:17:43 +00:00
|
|
|
|
|
|
|
pub enum SortField {
|
2014-07-22 20:27:36 +00:00
|
|
|
Unsorted, Name, Extension, Size, FileInode
|
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() {
|
2014-07-22 20:27:36 +00:00
|
|
|
"name" => Name,
|
|
|
|
"size" => Size,
|
|
|
|
"ext" => Extension,
|
|
|
|
"none" => Unsorted,
|
|
|
|
"inode" => FileInode,
|
|
|
|
_ => fail!("Invalid sorting order"),
|
2014-05-24 01:17:43 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-07-06 16:33:40 +00:00
|
|
|
pub enum View {
|
2014-07-22 19:47:30 +00:00
|
|
|
Details(Vec<Column>),
|
|
|
|
Lines,
|
|
|
|
Grid(bool, uint),
|
2014-07-06 16:33:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
pub struct Options {
|
|
|
|
pub show_invisibles: bool,
|
|
|
|
pub sort_field: SortField,
|
|
|
|
pub reverse: bool,
|
|
|
|
pub dirs: Vec<String>,
|
|
|
|
pub view: View,
|
|
|
|
pub header: bool,
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-05-24 01:17:43 +00:00
|
|
|
impl Options {
|
2014-05-26 10:08:33 +00:00
|
|
|
pub fn getopts(args: Vec<String>) -> Result<Options, getopts::Fail_> {
|
2014-06-18 12:39:55 +00:00
|
|
|
let opts = [
|
2014-07-22 19:47:30 +00:00
|
|
|
getopts::optflag("1", "oneline", "display one entry per line"),
|
2014-05-25 16:14:50 +00:00
|
|
|
getopts::optflag("a", "all", "show dot-files"),
|
2014-05-26 14:44:16 +00:00
|
|
|
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-07-06 16:33:40 +00:00
|
|
|
getopts::optflag("H", "links", "show number of hard links"),
|
|
|
|
getopts::optflag("l", "long", "display extended details and attributes"),
|
2014-06-22 06:44:00 +00:00
|
|
|
getopts::optflag("i", "inode", "show each file's inode number"),
|
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-07-07 18:18:09 +00:00
|
|
|
getopts::optflag("x", "across", "sort multi-column view entries across"),
|
2014-05-25 16:14:50 +00:00
|
|
|
];
|
|
|
|
|
|
|
|
match getopts::getopts(args.tail(), opts) {
|
|
|
|
Err(f) => Err(f),
|
|
|
|
Ok(matches) => Ok(Options {
|
2014-07-06 16:33:40 +00:00
|
|
|
show_invisibles: matches.opt_present("all"),
|
2014-05-25 16:14:50 +00:00
|
|
|
reverse: matches.opt_present("reverse"),
|
2014-06-23 17:26:35 +00:00
|
|
|
header: matches.opt_present("header"),
|
2014-07-06 16:33:40 +00:00
|
|
|
sort_field: matches.opt_str("sort").map(|word| SortField::from_word(word)).unwrap_or(Name),
|
2014-07-05 21:36:43 +00:00
|
|
|
dirs: if matches.free.is_empty() { vec![ ".".to_string() ] } else { matches.free.clone() },
|
2014-07-06 16:33:40 +00:00
|
|
|
view: Options::view(matches),
|
2014-05-25 16:14:50 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
2014-07-06 16:33:40 +00:00
|
|
|
|
|
|
|
fn view(matches: getopts::Matches) -> View {
|
|
|
|
if matches.opt_present("long") {
|
2014-07-22 19:47:30 +00:00
|
|
|
Details(Options::columns(matches))
|
|
|
|
}
|
|
|
|
else if matches.opt_present("oneline") {
|
|
|
|
Lines
|
2014-07-06 16:33:40 +00:00
|
|
|
}
|
|
|
|
else {
|
2014-07-22 19:50:53 +00:00
|
|
|
match term::dimensions() {
|
|
|
|
None => Lines,
|
|
|
|
Some((width, _)) => Grid(matches.opt_present("across"), width),
|
|
|
|
}
|
2014-07-06 16:33:40 +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);
|
2014-06-22 06:40:40 +00:00
|
|
|
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2014-06-26 22:26:27 +00:00
|
|
|
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 14:44:16 +00:00
|
|
|
}
|
|
|
|
|
2014-05-26 19:24:51 +00:00
|
|
|
fn should_display(&self, f: &File) -> bool {
|
2014-07-06 16:33:40 +00:00
|
|
|
if self.show_invisibles {
|
2014-05-24 01:17:43 +00:00
|
|
|
true
|
|
|
|
} else {
|
2014-06-29 20:02:14 +00:00
|
|
|
!f.name.as_slice().starts_with(".")
|
2014-05-24 01:17:43 +00:00
|
|
|
}
|
|
|
|
}
|
2014-05-25 18:42:31 +00:00
|
|
|
|
2014-05-26 11:46:51 +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))
|
2014-05-26 11:46:51 +00:00
|
|
|
.collect();
|
2014-05-26 10:50:46 +00:00
|
|
|
|
2014-07-06 16:33:40 +00:00
|
|
|
match self.sort_field {
|
2014-07-22 20:27:36 +00:00
|
|
|
Unsorted => {},
|
2014-06-01 10:54:31 +00:00
|
|
|
Name => files.sort_by(|a, b| a.parts.cmp(&b.parts)),
|
2014-05-26 10:50:46 +00:00
|
|
|
Size => files.sort_by(|a, b| a.stat.size.cmp(&b.stat.size)),
|
2014-07-22 20:27:36 +00:00
|
|
|
FileInode => files.sort_by(|a, b| a.stat.unstable.inode.cmp(&b.stat.unstable.inode)),
|
2014-05-26 10:50:46 +00:00
|
|
|
Extension => files.sort_by(|a, b| {
|
2014-06-29 20:02:14 +00:00
|
|
|
let exts = a.ext.clone().map(|e| e.as_slice().to_ascii_lower()).cmp(&b.ext.clone().map(|e| e.as_slice().to_ascii_lower()));
|
|
|
|
let names = a.name.as_slice().to_ascii_lower().cmp(&b.name.as_slice().to_ascii_lower());
|
2014-07-21 21:05:04 +00:00
|
|
|
exts.cmp(&names)
|
2014-05-26 10:50:46 +00:00
|
|
|
}),
|
|
|
|
}
|
|
|
|
|
|
|
|
if self.reverse {
|
|
|
|
files.reverse();
|
|
|
|
}
|
|
|
|
|
|
|
|
return files;
|
|
|
|
}
|
2014-05-24 01:17:43 +00:00
|
|
|
}
|