diff --git a/src/output/details.rs b/src/output/details.rs index 1e084f389e5d0892a3a4af50f65bd00714aa7fa3..4c83f4880a8c3091b6d318ddd0bc8c98639f6cb9 100644 --- a/src/output/details.rs +++ b/src/output/details.rs @@ -154,14 +154,14 @@ impl<'a> Render<'a> { let mut table = Some(table); self.add_files_to_table(&mut table, &mut rows, &self.files, 0); - for row in self.iterate(table.as_ref(), rows) { + for row in self.iterate_with_table(table.unwrap(), rows) { writeln!(w, "{}", row.strings())? } } else { self.add_files_to_table(&mut None, &mut rows, &self.files, 0); - for row in self.iterate(None, rows) { + for row in self.iterate(rows) { writeln!(w, "{}", row.strings())? } } @@ -317,34 +317,41 @@ impl<'a> Render<'a> { } } - /// Render the table as a vector of Cells, to be displayed on standard output. - pub fn iterate(&self, table: Option<&'a Table<'a>>, rows: Vec) -> Iter<'a> { - Iter { + pub fn iterate_with_table(&'a self, table: Table<'a>, rows: Vec) -> TableIter<'a> { + TableIter { tree_trunk: TreeTrunk::default(), - total_width: table.map(|t| t.columns_count() + t.widths().iter().fold(0, Add::add)).unwrap_or(0), + total_width: table.columns_count() + table.widths().iter().fold(0, Add::add), table: table, inner: rows.into_iter(), colours: self.colours, } } + + pub fn iterate(&'a self, rows: Vec) -> Iter<'a> { + Iter { + tree_trunk: TreeTrunk::default(), + inner: rows.into_iter(), + colours: self.colours, + } + } } -pub struct Iter<'a> { - table: Option<&'a Table<'a>>, +pub struct TableIter<'a> { + table: Table<'a>, tree_trunk: TreeTrunk, total_width: usize, colours: &'a Colours, inner: VecIntoIter, } -impl<'a> Iterator for Iter<'a> { +impl<'a> Iterator for TableIter<'a> { type Item = TextCell; fn next(&mut self) -> Option { self.inner.next().map(|row| { let mut cell = - if let (Some(table), Some(cells)) = (self.table, row.cells) { - table.render(cells) + if let Some(cells) = row.cells { + self.table.render(cells) } else { let mut cell = TextCell::default(); @@ -352,23 +359,17 @@ impl<'a> Iterator for Iter<'a> { cell }; - let mut filename = TextCell::default(); - for tree_part in self.tree_trunk.new_row(row.depth, row.last) { - filename.push(self.colours.punctuation.paint(tree_part.ascii_art()), 4); + cell.push(self.colours.punctuation.paint(tree_part.ascii_art()), 4); } // If any tree characters have been printed, then add an extra // space, which makes the output look much better. if row.depth != 0 { - filename.add_spaces(1); + cell.add_spaces(1); } - // Print the name without worrying about padding. - filename.append(row.name); - - cell.append(filename); - + cell.append(row.name); cell }) } @@ -396,3 +397,34 @@ pub struct Row { /// when calculating the tree view. pub last: bool, } + + + +pub struct Iter<'a> { + tree_trunk: TreeTrunk, + colours: &'a Colours, + inner: VecIntoIter, +} + +impl<'a> Iterator for Iter<'a> { + type Item = TextCell; + + fn next(&mut self) -> Option { + self.inner.next().map(|row| { + let mut cell = TextCell::default(); + + for tree_part in self.tree_trunk.new_row(row.depth, row.last) { + cell.push(self.colours.punctuation.paint(tree_part.ascii_art()), 4); + } + + // If any tree characters have been printed, then add an extra + // space, which makes the output look much better. + if row.depth != 0 { + cell.add_spaces(1); + } + + cell.append(row.name); + cell + }) + } +} diff --git a/src/output/grid_details.rs b/src/output/grid_details.rs index b4fde8ed5ae4ba87b1080fa08c6097291feef94f..63df7096ef0f11157b7f7a87f3fcd8cac4bb617a 100644 --- a/src/output/grid_details.rs +++ b/src/output/grid_details.rs @@ -124,7 +124,7 @@ impl<'a> Render<'a> { } let columns: Vec<_> = tables.into_iter().map(|(table, details_rows)| { - drender.iterate(Some(&table), details_rows).collect::>() + drender.iterate_with_table(table, details_rows).collect::>() }).collect(); let direction = if self.grid.across { grid::Direction::LeftToRight }