diff --git a/exa.rs b/exa.rs index c5f0d68d36e1a74c261131dc10eb3e81f4342dcd..c0b876ce5d996f9f3266e34ae6c32b4a391fac3c 100644 --- a/exa.rs +++ b/exa.rs @@ -2,13 +2,12 @@ extern crate regex; #[phase(syntax)] extern crate regex_macros; -extern crate getopts; use std::os; use std::io::fs; use file::File; use column::defaultColumns; -use options::{Options, SortField, Name}; +use options::Options; pub mod colours; pub mod column; @@ -18,40 +17,28 @@ pub mod unix; pub mod options; fn main() { - let args: Vec = os::args().iter() + let args = os::args().iter() .map(|x| x.to_strbuf()) .collect(); - - let opts = ~[ - getopts::optflag("a", "all", "show dot-files"), - getopts::optflag("r", "reverse", "reverse order of files"), - getopts::optopt("s", "sort", "field to sort by", "WORD"), - ]; - - let matches = match getopts::getopts(args.tail(), opts) { - Ok(m) => m, - Err(f) => fail!("Invalid options\n{}", f.to_err_msg()), - }; - - let opts = Options { - showInvisibles: matches.opt_present("all"), - reverse: matches.opt_present("reverse"), - sortField: matches.opt_str("sort").map(|word| SortField::from_word(word)).unwrap_or(Name), - }; - - let strs = if matches.free.is_empty() { - vec!("./".to_strbuf()) - } - else { - matches.free.clone() + + match Options::getopts(args) { + Err(err) => println!("Invalid options:\n{}", err.to_err_msg()), + Ok(opts) => { + let strs = if opts.dirs.is_empty() { + vec!("./".to_strbuf()) + } + else { + opts.dirs.clone() + }; + + for dir in strs.move_iter() { + exa(&opts, Path::new(dir)) + } + } }; - - for dir in strs.move_iter() { - list(opts, Path::new(dir)) - } } -fn list(options: Options, path: Path) { +fn exa(options: &Options, path: Path) { let paths = match fs::readdir(&path) { Ok(paths) => paths, Err(e) => fail!("readdir: {}", e), @@ -64,7 +51,6 @@ fn list(options: Options, path: Path) { } let columns = defaultColumns(); - let num_columns = columns.len(); let table: Vec> = files.iter() .filter(|&f| options.show(f)) @@ -75,7 +61,7 @@ fn list(options: Options, path: Path) { .map(|row| row.iter().map( |col| colours::strip_formatting(col).len() ).collect()) .collect(); - let maxes: Vec = range(0, num_columns) + let maxes: Vec = range(0, columns.len()) .map(|n| lengths.iter().map(|row| *row.get(n)).max().unwrap()) .collect(); diff --git a/options.rs b/options.rs index fce4cdeea6ffa56669d007466a18e3ead8381458..3773e30caebfb816198d4c86de6d4144165ac893 100644 --- a/options.rs +++ b/options.rs @@ -1,3 +1,5 @@ +extern crate getopts; + use file::File; use std::cmp::lexical_ordering; @@ -9,6 +11,7 @@ pub struct Options { pub showInvisibles: bool, pub sortField: SortField, pub reverse: bool, + pub dirs: Vec, } impl SortField { @@ -35,6 +38,24 @@ impl SortField { } impl Options { + pub fn getopts(args: Vec) -> Result { + let opts = ~[ + getopts::optflag("a", "all", "show dot-files"), + getopts::optflag("r", "reverse", "reverse order of files"), + getopts::optopt("s", "sort", "field to sort by", "WORD"), + ]; + + match getopts::getopts(args.tail(), opts) { + Err(f) => Err(f), + Ok(matches) => Ok(Options { + showInvisibles: matches.opt_present("all"), + reverse: matches.opt_present("reverse"), + sortField: matches.opt_str("sort").map(|word| SortField::from_word(word)).unwrap_or(Name), + dirs: matches.free, + }) + } + } + pub fn sort(&self, files: &mut Vec) { self.sortField.sort(files); }