2014-05-25 16:14:50 +00:00
|
|
|
extern crate getopts;
|
2014-12-12 11:26:18 +00:00
|
|
|
extern crate natord;
|
2014-05-25 16:14:50 +00:00
|
|
|
|
2014-05-24 01:17:43 +00:00
|
|
|
use file::File;
|
2014-12-18 07:00:31 +00:00
|
|
|
use column::{Column, SizeFormat};
|
2014-11-23 21:29:11 +00:00
|
|
|
use column::Column::*;
|
2015-01-12 20:08:42 +00:00
|
|
|
use output::View;
|
2014-11-23 21:29:11 +00:00
|
|
|
use term::dimensions;
|
|
|
|
|
|
|
|
use std::ascii::AsciiExt;
|
2015-01-12 18:44:39 +00:00
|
|
|
use std::slice::Iter;
|
2015-01-23 19:27:06 +00:00
|
|
|
use std::fmt;
|
2014-05-24 01:17:43 +00:00
|
|
|
|
2015-01-24 12:38:05 +00:00
|
|
|
use self::Misfire::*;
|
2015-01-12 21:14:27 +00:00
|
|
|
|
2015-01-24 12:38:05 +00:00
|
|
|
/// The *Options* struct represents a parsed version of the user's
|
|
|
|
/// command-line options.
|
2015-01-23 19:27:06 +00:00
|
|
|
#[derive(PartialEq, Debug)]
|
2014-07-06 16:33:40 +00:00
|
|
|
pub struct Options {
|
2014-11-25 01:27:26 +00:00
|
|
|
pub list_dirs: bool,
|
2015-01-12 21:47:05 +00:00
|
|
|
pub path_strs: Vec<String>,
|
2015-01-12 18:44:39 +00:00
|
|
|
reverse: bool,
|
|
|
|
show_invisibles: bool,
|
|
|
|
sort_field: SortField,
|
2014-07-06 16:33:40 +00:00
|
|
|
pub view: View,
|
|
|
|
}
|
|
|
|
|
2014-05-24 01:17:43 +00:00
|
|
|
impl Options {
|
2015-01-24 12:38:05 +00:00
|
|
|
|
|
|
|
/// Call getopts on the given slice of command-line strings.
|
|
|
|
pub fn getopts(args: &[String]) -> Result<Options, Misfire> {
|
2015-01-12 21:14:27 +00:00
|
|
|
let opts = &[
|
2014-11-25 01:27:26 +00:00
|
|
|
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"),
|
2014-12-12 11:26:18 +00:00
|
|
|
getopts::optflag("d", "list-dirs", "list directories as regular files"),
|
2014-11-25 01:27:26 +00:00
|
|
|
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
|
|
|
|
2015-01-12 21:47:05 +00:00
|
|
|
let matches = match getopts::getopts(args, opts) {
|
2014-12-12 11:26:18 +00:00
|
|
|
Ok(m) => m,
|
2015-01-24 12:38:05 +00:00
|
|
|
Err(e) => return Err(Misfire::InvalidOptions(e)),
|
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") {
|
2015-01-24 12:38:05 +00:00
|
|
|
return Err(Misfire::Help(getopts::usage("Usage:\n exa [options] [files...]", opts)));
|
2014-05-25 16:14:50 +00:00
|
|
|
}
|
2014-11-25 15:54:42 +00:00
|
|
|
|
2015-01-12 21:14:27 +00:00
|
|
|
let sort_field = match matches.opt_str("sort") {
|
|
|
|
Some(word) => try!(SortField::from_word(word)),
|
|
|
|
None => SortField::Name,
|
|
|
|
};
|
|
|
|
|
2014-11-25 15:54:42 +00:00
|
|
|
Ok(Options {
|
2014-12-12 11:26:18 +00:00
|
|
|
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"),
|
2015-01-12 21:14:27 +00:00
|
|
|
sort_field: sort_field,
|
2015-01-12 23:31:30 +00:00
|
|
|
view: try!(view(&matches)),
|
2014-12-12 11:26:18 +00:00
|
|
|
})
|
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()
|
|
|
|
}
|
|
|
|
|
2015-01-24 12:38:05 +00:00
|
|
|
/// Transform the files somehow before listing them.
|
2015-01-12 23:31:30 +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 {
|
|
|
|
SortField::Unsorted => {},
|
|
|
|
SortField::Name => files.sort_by(|a, b| natord::compare(a.name.as_slice(), b.name.as_slice())),
|
|
|
|
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| {
|
|
|
|
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)
|
|
|
|
}),
|
2014-07-22 19:47:30 +00:00
|
|
|
}
|
2015-01-12 23:31:30 +00:00
|
|
|
|
|
|
|
if self.reverse {
|
|
|
|
files.reverse();
|
|
|
|
}
|
|
|
|
|
|
|
|
files
|
|
|
|
}
|
|
|
|
|
|
|
|
fn should_display(&self, f: &File) -> bool {
|
|
|
|
if self.show_invisibles {
|
|
|
|
true
|
2014-07-06 16:33:40 +00:00
|
|
|
}
|
|
|
|
else {
|
2015-01-12 23:31:30 +00:00
|
|
|
!f.name.as_slice().starts_with(".")
|
2014-07-06 16:33:40 +00:00
|
|
|
}
|
|
|
|
}
|
2015-01-12 23:31:30 +00:00
|
|
|
}
|
2014-11-25 20:50:23 +00:00
|
|
|
|
2015-01-24 12:38:05 +00:00
|
|
|
/// User-supplied field to sort by
|
|
|
|
#[derive(PartialEq, Debug)]
|
|
|
|
pub enum SortField {
|
|
|
|
Unsorted, Name, Extension, Size, FileInode
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Copy for SortField { }
|
|
|
|
|
|
|
|
impl SortField {
|
|
|
|
|
|
|
|
/// Find which field to use based on a user-supplied word.
|
|
|
|
fn from_word(word: String) -> Result<SortField, Misfire> {
|
|
|
|
match word.as_slice() {
|
|
|
|
"name" => Ok(SortField::Name),
|
|
|
|
"size" => Ok(SortField::Size),
|
|
|
|
"ext" => Ok(SortField::Extension),
|
|
|
|
"none" => Ok(SortField::Unsorted),
|
|
|
|
"inode" => Ok(SortField::FileInode),
|
|
|
|
field => Err(SortField::none(field))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// How to display an error when the word didn't match with anything.
|
|
|
|
fn none(field: &str) -> Misfire {
|
|
|
|
Misfire::InvalidOptions(getopts::Fail::UnrecognizedOption(format!("--sort {}", field)))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// One of these things could happen instead of listing files.
|
|
|
|
#[derive(PartialEq, Debug)]
|
|
|
|
pub enum Misfire {
|
|
|
|
|
|
|
|
/// The getopts crate didn't like these arguments.
|
|
|
|
InvalidOptions(getopts::Fail),
|
|
|
|
|
|
|
|
/// The user asked for help. This isn't strictly an error, which is why
|
|
|
|
/// this enum isn't named Error!
|
|
|
|
Help(String),
|
|
|
|
|
|
|
|
/// Two options were given that conflict with one another
|
|
|
|
Conflict(&'static str, &'static str),
|
|
|
|
|
|
|
|
/// An option was given that does nothing when another one either is or
|
|
|
|
/// isn't present.
|
|
|
|
Useless(&'static str, bool, &'static str),
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Misfire {
|
|
|
|
/// The OS return code this misfire should signify.
|
|
|
|
pub fn error_code(&self) -> isize {
|
|
|
|
if let Help(_) = *self { 2 }
|
|
|
|
else { 3 }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl fmt::Display for Misfire {
|
|
|
|
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
|
|
|
match *self {
|
|
|
|
InvalidOptions(ref e) => write!(f, "{}", e),
|
|
|
|
Help(ref text) => write!(f, "{}", text),
|
|
|
|
Conflict(a, b) => write!(f, "Option --{} conflicts with option {}", a, b),
|
|
|
|
Useless(a, false, b) => write!(f, "Option --{} is useless without option --{}", a, b),
|
|
|
|
Useless(a, true, b) => write!(f, "Option --{} is useless given option --{}", a, b),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Turns the Getopts results object into a View object.
|
|
|
|
fn view(matches: &getopts::Matches) -> Result<View, Misfire> {
|
2015-01-12 23:31:30 +00:00
|
|
|
if matches.opt_present("long") {
|
|
|
|
if matches.opt_present("across") {
|
2015-01-24 12:38:05 +00:00
|
|
|
Err(Misfire::Useless("across", true, "long"))
|
2014-06-22 06:44:00 +00:00
|
|
|
}
|
2015-01-12 23:31:30 +00:00
|
|
|
else if matches.opt_present("oneline") {
|
2015-01-24 12:38:05 +00:00
|
|
|
Err(Misfire::Useless("across", true, "long"))
|
2014-06-22 06:40:40 +00:00
|
|
|
}
|
2015-01-12 23:31:30 +00:00
|
|
|
else {
|
|
|
|
Ok(View::Details(try!(columns(matches)), matches.opt_present("header")))
|
2015-01-12 21:47:05 +00:00
|
|
|
}
|
2015-01-12 23:31:30 +00:00
|
|
|
}
|
|
|
|
else if matches.opt_present("binary") {
|
2015-01-24 12:38:05 +00:00
|
|
|
Err(Misfire::Useless("binary", false, "long"))
|
2015-01-12 23:31:30 +00:00
|
|
|
}
|
|
|
|
else if matches.opt_present("bytes") {
|
2015-01-24 12:38:05 +00:00
|
|
|
Err(Misfire::Useless("bytes", false, "long"))
|
2015-01-12 23:31:30 +00:00
|
|
|
}
|
|
|
|
else if matches.opt_present("oneline") {
|
|
|
|
if matches.opt_present("across") {
|
2015-01-24 12:38:05 +00:00
|
|
|
Err(Misfire::Useless("across", true, "oneline"))
|
2015-01-12 21:47:05 +00:00
|
|
|
}
|
|
|
|
else {
|
2015-01-12 23:31:30 +00:00
|
|
|
Ok(View::Lines)
|
2015-01-12 21:47:05 +00:00
|
|
|
}
|
2015-01-12 23:31:30 +00:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
match dimensions() {
|
|
|
|
None => Ok(View::Lines),
|
|
|
|
Some((width, _)) => Ok(View::Grid(matches.opt_present("across"), width)),
|
2014-06-22 07:09:16 +00:00
|
|
|
}
|
2015-01-12 23:31:30 +00:00
|
|
|
}
|
|
|
|
}
|
2014-06-22 07:09:16 +00:00
|
|
|
|
2015-01-24 12:38:05 +00:00
|
|
|
/// Finds out which file size the user has asked for.
|
|
|
|
fn file_size(matches: &getopts::Matches) -> Result<SizeFormat, Misfire> {
|
2015-01-12 23:31:30 +00:00
|
|
|
let binary = matches.opt_present("binary");
|
|
|
|
let bytes = matches.opt_present("bytes");
|
2014-05-26 17:08:58 +00:00
|
|
|
|
2015-01-12 23:31:30 +00:00
|
|
|
match (binary, bytes) {
|
2015-01-24 12:38:05 +00:00
|
|
|
(true, true ) => Err(Misfire::Conflict("binary", "bytes")),
|
2015-01-12 23:31:30 +00:00
|
|
|
(true, false) => Ok(SizeFormat::BinaryBytes),
|
|
|
|
(false, true ) => Ok(SizeFormat::JustBytes),
|
|
|
|
(false, false) => Ok(SizeFormat::DecimalBytes),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-01-24 12:38:05 +00:00
|
|
|
/// Turns the Getopts results object into a list of columns for the columns
|
|
|
|
/// view, depending on the passed-in command-line arguments.
|
|
|
|
fn columns(matches: &getopts::Matches) -> Result<Vec<Column>, Misfire> {
|
2015-01-12 23:31:30 +00:00
|
|
|
let mut columns = vec![];
|
2014-05-26 17:08:58 +00:00
|
|
|
|
2015-01-12 23:31:30 +00:00
|
|
|
if matches.opt_present("inode") {
|
|
|
|
columns.push(Inode);
|
2014-05-26 14:44:16 +00:00
|
|
|
}
|
|
|
|
|
2015-01-12 23:31:30 +00:00
|
|
|
columns.push(Permissions);
|
|
|
|
|
|
|
|
if matches.opt_present("links") {
|
|
|
|
columns.push(HardLinks);
|
2014-05-24 01:17:43 +00:00
|
|
|
}
|
2014-05-25 18:42:31 +00:00
|
|
|
|
2015-01-24 12:38:05 +00:00
|
|
|
// Fail early here if two file size flags are given
|
2015-01-12 23:31:30 +00:00
|
|
|
columns.push(FileSize(try!(file_size(matches))));
|
2014-05-26 10:50:46 +00:00
|
|
|
|
2015-01-12 23:31:30 +00:00
|
|
|
if matches.opt_present("blocks") {
|
|
|
|
columns.push(Blocks);
|
|
|
|
}
|
2014-05-26 10:50:46 +00:00
|
|
|
|
2015-01-12 23:31:30 +00:00
|
|
|
columns.push(User);
|
2014-05-26 10:50:46 +00:00
|
|
|
|
2015-01-12 23:31:30 +00:00
|
|
|
if matches.opt_present("group") {
|
|
|
|
columns.push(Group);
|
2014-05-26 10:50:46 +00:00
|
|
|
}
|
2015-01-12 23:31:30 +00:00
|
|
|
|
|
|
|
columns.push(FileName);
|
|
|
|
Ok(columns)
|
2014-05-24 01:17:43 +00:00
|
|
|
}
|
2015-01-12 21:47:05 +00:00
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod test {
|
|
|
|
use super::Options;
|
2015-01-24 12:38:05 +00:00
|
|
|
use super::Misfire;
|
|
|
|
use super::Misfire::*;
|
2015-01-12 21:47:05 +00:00
|
|
|
|
2015-01-23 19:27:06 +00:00
|
|
|
use std::fmt;
|
|
|
|
|
2015-01-24 12:38:05 +00:00
|
|
|
fn is_helpful(misfire: Result<Options, Misfire>) -> bool {
|
|
|
|
match misfire {
|
2015-01-12 21:47:05 +00:00
|
|
|
Err(Help(_)) => true,
|
|
|
|
_ => false,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-01-23 19:27:06 +00:00
|
|
|
impl fmt::Display for Options {
|
|
|
|
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
|
|
|
write!(f, "{:?}", self)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-01-12 21:47:05 +00:00
|
|
|
#[test]
|
|
|
|
fn help() {
|
|
|
|
let opts = Options::getopts(&[ "--help".to_string() ]);
|
|
|
|
assert!(is_helpful(opts))
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn help_with_file() {
|
|
|
|
let opts = Options::getopts(&[ "--help".to_string(), "me".to_string() ]);
|
|
|
|
assert!(is_helpful(opts))
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn files() {
|
|
|
|
let opts = Options::getopts(&[ "this file".to_string(), "that file".to_string() ]);
|
|
|
|
assert_eq!(opts.unwrap().path_strs, vec![ "this file".to_string(), "that file".to_string() ])
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn no_args() {
|
|
|
|
let opts = Options::getopts(&[]);
|
|
|
|
assert_eq!(opts.unwrap().path_strs, vec![ ".".to_string() ])
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2015-01-12 23:31:30 +00:00
|
|
|
fn file_sizes() {
|
|
|
|
let opts = Options::getopts(&[ "--long".to_string(), "--binary".to_string(), "--bytes".to_string() ]);
|
2015-01-24 12:38:05 +00:00
|
|
|
assert_eq!(opts.unwrap_err(), Misfire::Conflict("binary", "bytes"))
|
2015-01-12 23:31:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn just_binary() {
|
|
|
|
let opts = Options::getopts(&[ "--binary".to_string() ]);
|
2015-01-24 12:38:05 +00:00
|
|
|
assert_eq!(opts.unwrap_err(), Misfire::Useless("binary", false, "long"))
|
2015-01-12 21:47:05 +00:00
|
|
|
}
|
2015-01-12 23:31:30 +00:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn just_bytes() {
|
|
|
|
let opts = Options::getopts(&[ "--bytes".to_string() ]);
|
2015-01-24 12:38:05 +00:00
|
|
|
assert_eq!(opts.unwrap_err(), Misfire::Useless("bytes", false, "long"))
|
2015-01-12 23:31:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn long_across() {
|
|
|
|
let opts = Options::getopts(&[ "--long".to_string(), "--across".to_string() ]);
|
2015-01-24 12:38:05 +00:00
|
|
|
assert_eq!(opts.unwrap_err(), Misfire::Useless("across", true, "long"))
|
2015-01-12 23:31:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn oneline_across() {
|
|
|
|
let opts = Options::getopts(&[ "--oneline".to_string(), "--across".to_string() ]);
|
2015-01-24 12:38:05 +00:00
|
|
|
assert_eq!(opts.unwrap_err(), Misfire::Useless("across", true, "oneline"))
|
2015-01-12 23:31:30 +00:00
|
|
|
}
|
2015-01-12 21:47:05 +00:00
|
|
|
}
|