mirror of
https://github.com/Llewellynvdm/exa.git
synced 2024-11-05 20:37:52 +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;
|
extern crate regex;
|
||||||
#[phase(syntax)] extern crate regex_macros;
|
#[phase(syntax)] extern crate regex_macros;
|
||||||
|
|
||||||
extern crate getopts;
|
|
||||||
use std::os;
|
use std::os;
|
||||||
use std::io::fs;
|
use std::io::fs;
|
||||||
|
|
||||||
use file::File;
|
use file::File;
|
||||||
use column::defaultColumns;
|
use column::defaultColumns;
|
||||||
use options::{Options, SortField, Name};
|
use options::Options;
|
||||||
|
|
||||||
pub mod colours;
|
pub mod colours;
|
||||||
pub mod column;
|
pub mod column;
|
||||||
@ -18,40 +17,28 @@ pub mod unix;
|
|||||||
pub mod options;
|
pub mod options;
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
let args: Vec<StrBuf> = os::args().iter()
|
let args = os::args().iter()
|
||||||
.map(|x| x.to_strbuf())
|
.map(|x| x.to_strbuf())
|
||||||
.collect();
|
.collect();
|
||||||
|
|
||||||
let opts = ~[
|
match Options::getopts(args) {
|
||||||
getopts::optflag("a", "all", "show dot-files"),
|
Err(err) => println!("Invalid options:\n{}", err.to_err_msg()),
|
||||||
getopts::optflag("r", "reverse", "reverse order of files"),
|
Ok(opts) => {
|
||||||
getopts::optopt("s", "sort", "field to sort by", "WORD"),
|
let strs = if opts.dirs.is_empty() {
|
||||||
];
|
|
||||||
|
|
||||||
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())
|
vec!("./".to_strbuf())
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
matches.free.clone()
|
opts.dirs.clone()
|
||||||
};
|
};
|
||||||
|
|
||||||
for dir in strs.move_iter() {
|
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) {
|
let paths = match fs::readdir(&path) {
|
||||||
Ok(paths) => paths,
|
Ok(paths) => paths,
|
||||||
Err(e) => fail!("readdir: {}", e),
|
Err(e) => fail!("readdir: {}", e),
|
||||||
@ -64,7 +51,6 @@ fn list(options: Options, path: Path) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
let columns = defaultColumns();
|
let columns = defaultColumns();
|
||||||
let num_columns = columns.len();
|
|
||||||
|
|
||||||
let table: Vec<Vec<StrBuf>> = files.iter()
|
let table: Vec<Vec<StrBuf>> = files.iter()
|
||||||
.filter(|&f| options.show(f))
|
.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())
|
.map(|row| row.iter().map( |col| colours::strip_formatting(col).len() ).collect())
|
||||||
.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())
|
.map(|n| lengths.iter().map(|row| *row.get(n)).max().unwrap())
|
||||||
.collect();
|
.collect();
|
||||||
|
|
||||||
|
21
options.rs
21
options.rs
@ -1,3 +1,5 @@
|
|||||||
|
extern crate getopts;
|
||||||
|
|
||||||
use file::File;
|
use file::File;
|
||||||
use std::cmp::lexical_ordering;
|
use std::cmp::lexical_ordering;
|
||||||
|
|
||||||
@ -9,6 +11,7 @@ pub struct Options {
|
|||||||
pub showInvisibles: bool,
|
pub showInvisibles: bool,
|
||||||
pub sortField: SortField,
|
pub sortField: SortField,
|
||||||
pub reverse: bool,
|
pub reverse: bool,
|
||||||
|
pub dirs: Vec<StrBuf>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl SortField {
|
impl SortField {
|
||||||
@ -35,6 +38,24 @@ impl SortField {
|
|||||||
}
|
}
|
||||||
|
|
||||||
impl Options {
|
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>) {
|
pub fn sort(&self, files: &mut Vec<File>) {
|
||||||
self.sortField.sort(files);
|
self.sortField.sort(files);
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user