Have each row use the same column widths

This involves putting the entire output into a table before anything
is actually printed, in order to determine what the width of each
column should be. This should make it appear to output slower, as the
first line can only be printed after every file has been examined, but
it's still fast to me.
This commit is contained in:
Ben S 2014-05-22 01:02:47 +01:00
parent 818fc615a8
commit a0582132e5
2 changed files with 23 additions and 8 deletions

View File

@ -82,3 +82,8 @@ impl Colour {
Style(StyleStruct { foreground: *self, background: Some(background), bold: false, underline: false })
}
}
pub fn strip_formatting(input: &~str) -> ~str {
let re = regex!("\x1B\\[.+?m");
re.replace_all(*input, "").to_owned()
}

26
exa.rs
View File

@ -1,3 +1,7 @@
#![feature(phase)]
extern crate regex;
#[phase(syntax)] extern crate regex_macros;
extern crate getopts;
use std::os;
use std::io::fs;
@ -51,25 +55,31 @@ fn list(opts: Options, path: Path) {
Err(e) => fail!("readdir: {}", e),
};
files.sort_by(|a, b| a.filename_str().cmp(&b.filename_str()));
for subpath in files.iter() {
let file = File::from_path(subpath);
if file.is_dotfile() && !opts.showInvisibles {
continue;
}
let columns = defaultColumns();
let columns = defaultColumns();
let table: Vec<Vec<~str>> = files.iter()
.map(|p| File::from_path(p))
.filter(|f| !f.is_dotfile() || opts.showInvisibles )
.map(|f| columns.iter().map(|c| f.display(c)).collect())
.collect();
let mut cells = columns.iter().map(|c| file.display(c));
let maxes: Vec<uint> = range(0, columns.len())
.map(|n| table.iter().map(|row| colours::strip_formatting(row.get(n)).len()).max().unwrap())
.collect();
for row in table.iter() {
let mut first = true;
for cell in cells {
for (length, cell) in maxes.iter().zip(row.iter()) {
if first {
first = false;
} else {
print!(" ");
}
print!("{}", cell);
for _ in range(cell.len(), *length) {
print!(" ");
}
}
print!("\n");
}