mirror of
https://github.com/Llewellynvdm/exa.git
synced 2024-11-22 12:05:11 +00:00
Move getopts code into options module
This commit is contained in:
parent
ff0bef66e5
commit
cb6dd57ca0
38
exa.rs
38
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<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() {
|
||||
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 {
|
||||
matches.free.clone()
|
||||
opts.dirs.clone()
|
||||
};
|
||||
|
||||
for dir in strs.move_iter() {
|
||||
list(opts, Path::new(dir))
|
||||
exa(&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();
|
||||
|
||||
|
21
options.rs
21
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<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);
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user