From ff0bef66e5693088a64ab4bb896b1eb5f5105927 Mon Sep 17 00:00:00 2001 From: Ben S Date: Sun, 25 May 2014 17:14:50 +0100 Subject: [PATCH] Cache the lengths of formatting-striped fields The code could do with some comments, but it works. --- exa.rs | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-) diff --git a/exa.rs b/exa.rs index a181e6e..c5f0d68 100644 --- a/exa.rs +++ b/exa.rs @@ -64,25 +64,31 @@ fn list(options: Options, path: Path) { } let columns = defaultColumns(); + let num_columns = columns.len(); + let table: Vec> = files.iter() .filter(|&f| options.show(f)) .map(|f| columns.iter().map(|c| f.display(c)).collect()) .collect(); - let maxes: Vec = range(0, columns.len()) - .map(|n| table.iter().map(|row| colours::strip_formatting(row.get(n)).len()).max().unwrap()) + let lengths: Vec> = table.iter() + .map(|row| row.iter().map( |col| colours::strip_formatting(col).len() ).collect()) .collect(); - for row in table.iter() { + let maxes: Vec = 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; - 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 { first = false; } else { print!(" "); } print!("{}", cell.as_slice()); - for _ in range(colours::strip_formatting(cell).len(), *length) { + for _ in range(*field_length, *column_length) { print!(" "); } }