Add --binary flag

This finally means that the list of columns is no longer fixed, which means it has to be generated somewhere. And guess where it got moved to? That's right, the options object! (see previous commits)
This commit is contained in:
Ben S 2014-05-26 15:44:16 +01:00
parent 573765b62b
commit 0dd74a022f
2 changed files with 16 additions and 15 deletions

5
exa.rs
View File

@ -45,17 +45,16 @@ fn exa(options: &Options, path: Path) {
let unordered_files: Vec<File> = paths.iter().map(|path| File::from_path(path)).collect(); let unordered_files: Vec<File> = paths.iter().map(|path| File::from_path(path)).collect();
let files: Vec<&File> = options.transform_files(&unordered_files); let files: Vec<&File> = options.transform_files(&unordered_files);
let columns = options.columns();
let table: Vec<Vec<String>> = files.iter() let table: Vec<Vec<String>> = files.iter()
.map(|f| columns.iter().map(|c| f.display(c)).collect()) .map(|f| options.columns.iter().map(|c| f.display(c)).collect())
.collect(); .collect();
let lengths: Vec<Vec<uint>> = table.iter() let lengths: Vec<Vec<uint>> = table.iter()
.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, columns.len()) let maxes: Vec<uint> = range(0, options.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();

View File

@ -13,6 +13,7 @@ pub struct Options {
pub sortField: SortField, pub sortField: SortField,
pub reverse: bool, pub reverse: bool,
pub dirs: Vec<String>, pub dirs: Vec<String>,
pub columns: ~[Column],
} }
impl SortField { impl SortField {
@ -30,6 +31,7 @@ impl Options {
pub fn getopts(args: Vec<String>) -> Result<Options, getopts::Fail_> { pub fn getopts(args: Vec<String>) -> Result<Options, getopts::Fail_> {
let opts = ~[ let opts = ~[
getopts::optflag("a", "all", "show dot-files"), getopts::optflag("a", "all", "show dot-files"),
getopts::optflag("b", "binary", "use binary prefixes in file sizes"),
getopts::optflag("r", "reverse", "reverse order of files"), getopts::optflag("r", "reverse", "reverse order of files"),
getopts::optopt("s", "sort", "field to sort by", "WORD"), getopts::optopt("s", "sort", "field to sort by", "WORD"),
]; ];
@ -40,11 +42,22 @@ impl Options {
showInvisibles: matches.opt_present("all"), showInvisibles: matches.opt_present("all"),
reverse: matches.opt_present("reverse"), reverse: matches.opt_present("reverse"),
sortField: matches.opt_str("sort").map(|word| SortField::from_word(word)).unwrap_or(Name), sortField: matches.opt_str("sort").map(|word| SortField::from_word(word)).unwrap_or(Name),
dirs: matches.free, dirs: matches.free.clone(),
columns: Options::columns(matches),
}) })
} }
} }
fn columns(matches: getopts::Matches) -> ~[Column] {
return ~[
Permissions,
FileSize(matches.opt_present("binary")),
User,
Group,
FileName,
];
}
fn show(&self, f: &File) -> bool { fn show(&self, f: &File) -> bool {
if self.showInvisibles { if self.showInvisibles {
true true
@ -74,15 +87,4 @@ impl Options {
return files; return files;
} }
pub fn columns(&self) -> ~[Column] {
return ~[
Permissions,
FileSize(false),
User,
Group,
FileName,
];
}
} }