Cache the lengths of formatting-striped fields

The code could do with some comments, but it works.
This commit is contained in:
Ben S 2014-05-25 17:14:50 +01:00
parent b116889abc
commit ff0bef66e5

16
exa.rs
View File

@ -64,25 +64,31 @@ 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))
.map(|f| columns.iter().map(|c| f.display(c)).collect()) .map(|f| columns.iter().map(|c| f.display(c)).collect())
.collect(); .collect();
let maxes: Vec<uint> = range(0, columns.len()) let lengths: Vec<Vec<uint>> = table.iter()
.map(|n| table.iter().map(|row| colours::strip_formatting(row.get(n)).len()).max().unwrap()) .map(|row| row.iter().map( |col| colours::strip_formatting(col).len() ).collect())
.collect(); .collect();
for row in table.iter() { let maxes: Vec<uint> = range(0, num_columns)
.map(|n| lengths.iter().map(|row| *row.get(n)).max().unwrap())
.collect();
for (field_lengths, row) in lengths.iter().zip(table.iter()) {
let mut first = true; let mut first = true;
for (length, cell) in maxes.iter().zip(row.iter()) { for ((column_length, cell), field_length) in maxes.iter().zip(row.iter()).zip(field_lengths.iter()) {
if first { if first {
first = false; first = false;
} else { } else {
print!(" "); print!(" ");
} }
print!("{}", cell.as_slice()); print!("{}", cell.as_slice());
for _ in range(colours::strip_formatting(cell).len(), *length) { for _ in range(*field_length, *column_length) {
print!(" "); print!(" ");
} }
} }