提交 cb6dd57c 编写于 作者: B Ben S

Move getopts code into options module

上级 ff0bef66
......@@ -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<StrBuf> = 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<Vec<StrBuf>> = 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<uint> = range(0, num_columns)
let maxes: Vec<uint> = range(0, columns.len())
.map(|n| lengths.iter().map(|row| *row.get(n)).max().unwrap())
.collect();
......
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<StrBuf>,
}
impl SortField {
......@@ -35,6 +38,24 @@ impl SortField {
}
impl Options {
pub fn getopts(args: Vec<StrBuf>) -> Result<Options, getopts::Fail_> {
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<File>) {
self.sortField.sort(files);
}
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册