exa/src/options.rs

180 lines
5.7 KiB
Rust
Raw Normal View History

2014-05-25 16:14:50 +00:00
extern crate getopts;
extern crate natord;
2014-05-25 16:14:50 +00:00
use file::File;
use column::{Column, SizeFormat};
2014-11-23 21:29:11 +00:00
use column::Column::*;
use term::dimensions;
use std::ascii::AsciiExt;
2015-01-12 18:44:39 +00:00
use std::slice::Iter;
pub enum SortField {
2014-07-22 20:27:36 +00:00
Unsorted, Name, Extension, Size, FileInode
}
2014-12-14 18:22:56 +00:00
impl Copy for SortField { }
impl SortField {
fn from_word(word: String) -> SortField {
match word.as_slice() {
2014-11-23 21:29:11 +00:00
"name" => SortField::Name,
"size" => SortField::Size,
"ext" => SortField::Extension,
"none" => SortField::Unsorted,
"inode" => SortField::FileInode,
_ => panic!("Invalid sorting order"),
}
}
}
pub enum View {
Details(Vec<Column>),
Lines,
Grid(bool, usize),
}
pub struct Options {
pub header: bool,
pub list_dirs: bool,
2015-01-12 18:44:39 +00:00
path_strs: Vec<String>,
reverse: bool,
show_invisibles: bool,
sort_field: SortField,
pub view: View,
}
impl Options {
pub fn getopts(args: Vec<String>) -> Result<Options, isize> {
2014-11-25 15:54:42 +00:00
let opts = [
getopts::optflag("1", "oneline", "display one entry per line"),
getopts::optflag("a", "all", "show dot-files"),
getopts::optflag("b", "binary", "use binary prefixes in file sizes"),
2014-12-18 07:04:31 +00:00
getopts::optflag("B", "bytes", "list file sizes in bytes, without prefixes"),
getopts::optflag("d", "list-dirs", "list directories as regular files"),
getopts::optflag("g", "group", "show group as well as user"),
getopts::optflag("h", "header", "show a header row at the top"),
getopts::optflag("H", "links", "show number of hard links"),
getopts::optflag("l", "long", "display extended details and attributes"),
getopts::optflag("i", "inode", "show each file's inode number"),
getopts::optflag("r", "reverse", "reverse order of files"),
getopts::optopt ("s", "sort", "field to sort by", "WORD"),
getopts::optflag("S", "blocks", "show number of file system blocks"),
getopts::optflag("x", "across", "sort multi-column view entries across"),
2014-11-25 15:54:42 +00:00
getopts::optflag("?", "help", "show list of command-line options"),
2014-05-25 16:14:50 +00:00
];
2014-11-25 20:50:23 +00:00
2014-11-25 15:54:42 +00:00
let matches = match getopts::getopts(args.tail(), &opts) {
Ok(m) => m,
Err(e) => {
println!("Invalid options: {}", e);
return Err(1);
}
2014-11-25 15:54:42 +00:00
};
2014-11-25 20:50:23 +00:00
2014-11-25 15:54:42 +00:00
if matches.opt_present("help") {
println!("exa - ls with more features\n\n{}", getopts::usage("Usage:\n exa [options] [files...]", &opts));
return Err(2);
2014-05-25 16:14:50 +00:00
}
2014-11-25 15:54:42 +00:00
Ok(Options {
header: matches.opt_present("header"),
list_dirs: matches.opt_present("list-dirs"),
path_strs: if matches.free.is_empty() { vec![ ".".to_string() ] } else { matches.free.clone() },
reverse: matches.opt_present("reverse"),
show_invisibles: matches.opt_present("all"),
sort_field: matches.opt_str("sort").map(|word| SortField::from_word(word)).unwrap_or(SortField::Name),
view: Options::view(&matches),
})
2014-05-25 16:14:50 +00:00
}
2014-11-25 20:50:23 +00:00
2015-01-12 18:44:39 +00:00
pub fn path_strings(&self) -> Iter<String> {
self.path_strs.iter()
}
2014-11-23 22:36:03 +00:00
fn view(matches: &getopts::Matches) -> View {
if matches.opt_present("long") {
2014-11-23 21:29:11 +00:00
View::Details(Options::columns(matches))
}
else if matches.opt_present("oneline") {
2014-11-23 21:29:11 +00:00
View::Lines
}
else {
2014-11-23 21:29:11 +00:00
match dimensions() {
None => View::Lines,
Some((width, _)) => View::Grid(matches.opt_present("across"), width),
}
}
}
2014-11-25 20:50:23 +00:00
2014-11-23 22:36:03 +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);
}
2014-11-25 20:50:23 +00:00
if matches.opt_present("binary") {
columns.push(FileSize(SizeFormat::BinaryBytes))
}
2014-12-18 07:04:31 +00:00
else if matches.opt_present("bytes") {
columns.push(FileSize(SizeFormat::JustBytes))
}
else {
columns.push(FileSize(SizeFormat::DecimalBytes))
}
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);
columns
}
2014-05-26 19:24:51 +00:00
fn should_display(&self, f: &File) -> bool {
if self.show_invisibles {
true
2014-11-24 02:12:52 +00:00
}
else {
!f.name.as_slice().starts_with(".")
}
}
2014-05-25 18:42:31 +00:00
pub fn transform_files<'a>(&self, unordered_files: Vec<File<'a>>) -> Vec<File<'a>> {
let mut files: Vec<File<'a>> = unordered_files.into_iter()
.filter(|f| self.should_display(f))
.collect();
match self.sort_field {
2014-11-23 21:29:11 +00:00
SortField::Unsorted => {},
SortField::Name => files.sort_by(|a, b| natord::compare(a.name.as_slice(), b.name.as_slice())),
2014-11-23 21:29:11 +00:00
SortField::Size => files.sort_by(|a, b| a.stat.size.cmp(&b.stat.size)),
SortField::FileInode => files.sort_by(|a, b| a.stat.unstable.inode.cmp(&b.stat.unstable.inode)),
SortField::Extension => files.sort_by(|a, b| {
2015-01-01 02:37:10 +00:00
let exts = a.ext.clone().map(|e| e.to_ascii_lowercase()).cmp(&b.ext.clone().map(|e| e.to_ascii_lowercase()));
let names = a.name.to_ascii_lowercase().cmp(&b.name.to_ascii_lowercase());
exts.cmp(&names)
}),
}
if self.reverse {
files.reverse();
}
files
}
}