|
|
@@ -1,6 +1,3 @@
|
|
|
-extern crate getopts;
|
|
|
-extern crate natord;
|
|
|
-
|
|
|
use dir::Dir;
|
|
|
use file::File;
|
|
|
use column::{Column, SizeFormat};
|
|
|
@@ -13,14 +10,17 @@ use std::cmp::Ordering;
|
|
|
use std::fmt;
|
|
|
use std::slice::Iter;
|
|
|
|
|
|
+use getopts;
|
|
|
+use natord;
|
|
|
+
|
|
|
use self::Misfire::*;
|
|
|
|
|
|
/// The *Options* struct represents a parsed version of the user's
|
|
|
/// command-line options.
|
|
|
#[derive(PartialEq, Debug)]
|
|
|
pub struct Options {
|
|
|
- pub list_dirs: bool,
|
|
|
- path_strs: Vec<String>,
|
|
|
+ pub dir_action: DirAction,
|
|
|
+ pub path_strs: Vec<String>,
|
|
|
reverse: bool,
|
|
|
show_invisibles: bool,
|
|
|
sort_field: SortField,
|
|
|
@@ -43,6 +43,7 @@ impl Options {
|
|
|
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::optflag("R", "recurse", "recurse into directories"),
|
|
|
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"),
|
|
|
@@ -64,7 +65,7 @@ impl Options {
|
|
|
};
|
|
|
|
|
|
Ok(Options {
|
|
|
- list_dirs: matches.opt_present("list-dirs"),
|
|
|
+ dir_action: try!(dir_action(&matches)),
|
|
|
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"),
|
|
|
@@ -73,11 +74,6 @@ impl Options {
|
|
|
})
|
|
|
}
|
|
|
|
|
|
- /// Iterate over the non-option arguments left oven from getopts.
|
|
|
- pub fn path_strings(&self) -> Iter<String> {
|
|
|
- self.path_strs.iter()
|
|
|
- }
|
|
|
-
|
|
|
/// Display the files using this Option's View.
|
|
|
pub fn view(&self, dir: Option<&Dir>, files: Vec<File>) {
|
|
|
self.view.view(dir, files)
|
|
|
@@ -113,7 +109,13 @@ impl Options {
|
|
|
}
|
|
|
}
|
|
|
|
|
|
-/// User-supplied field to sort by
|
|
|
+/// What to do when encountering a directory?
|
|
|
+#[derive(PartialEq, Debug, Copy)]
|
|
|
+pub enum DirAction {
|
|
|
+ AsFile, List, Recurse
|
|
|
+}
|
|
|
+
|
|
|
+/// User-supplied field to sort by.
|
|
|
#[derive(PartialEq, Debug, Copy)]
|
|
|
pub enum SortField {
|
|
|
Unsorted, Name, Extension, Size, FileInode
|
|
|
@@ -228,7 +230,7 @@ fn view(matches: &getopts::Matches) -> Result<View, Misfire> {
|
|
|
/// Finds out which file size the user has asked for.
|
|
|
fn file_size(matches: &getopts::Matches) -> Result<SizeFormat, Misfire> {
|
|
|
let binary = matches.opt_present("binary");
|
|
|
- let bytes = matches.opt_present("bytes");
|
|
|
+ let bytes = matches.opt_present("bytes");
|
|
|
|
|
|
match (binary, bytes) {
|
|
|
(true, true ) => Err(Misfire::Conflict("binary", "bytes")),
|
|
|
@@ -238,6 +240,18 @@ fn file_size(matches: &getopts::Matches) -> Result<SizeFormat, Misfire> {
|
|
|
}
|
|
|
}
|
|
|
|
|
|
+fn dir_action(matches: &getopts::Matches) -> Result<DirAction, Misfire> {
|
|
|
+ let recurse = matches.opt_present("recurse");
|
|
|
+ let list = matches.opt_present("list-dirs");
|
|
|
+
|
|
|
+ match (recurse, list) {
|
|
|
+ (true, true ) => Err(Misfire::Conflict("recurse", "list-dirs")),
|
|
|
+ (true, false) => Ok(DirAction::Recurse),
|
|
|
+ (false, true ) => Ok(DirAction::AsFile),
|
|
|
+ (false, false) => Ok(DirAction::List),
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
#[derive(PartialEq, Copy, Debug)]
|
|
|
pub struct Columns {
|
|
|
size_format: SizeFormat,
|