提交 1b58f012 编写于 作者: B Benjamin Sago

Split the details iterator in two

Instead of having one iterator that might or might not contain a table, have two, one per case.
上级 7b641769
...@@ -154,14 +154,14 @@ impl<'a> Render<'a> { ...@@ -154,14 +154,14 @@ impl<'a> Render<'a> {
let mut table = Some(table); let mut table = Some(table);
self.add_files_to_table(&mut table, &mut rows, &self.files, 0); 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())? writeln!(w, "{}", row.strings())?
} }
} }
else { else {
self.add_files_to_table(&mut None, &mut rows, &self.files, 0); 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())? writeln!(w, "{}", row.strings())?
} }
} }
...@@ -317,34 +317,41 @@ impl<'a> Render<'a> { ...@@ -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_with_table(&'a self, table: Table<'a>, rows: Vec<Row>) -> TableIter<'a> {
pub fn iterate(&self, table: Option<&'a Table<'a>>, rows: Vec<Row>) -> Iter<'a> { TableIter {
Iter {
tree_trunk: TreeTrunk::default(), 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, table: table,
inner: rows.into_iter(), inner: rows.into_iter(),
colours: self.colours, colours: self.colours,
} }
} }
pub fn iterate(&'a self, rows: Vec<Row>) -> Iter<'a> {
Iter {
tree_trunk: TreeTrunk::default(),
inner: rows.into_iter(),
colours: self.colours,
}
}
} }
pub struct Iter<'a> { pub struct TableIter<'a> {
table: Option<&'a Table<'a>>, table: Table<'a>,
tree_trunk: TreeTrunk, tree_trunk: TreeTrunk,
total_width: usize, total_width: usize,
colours: &'a Colours, colours: &'a Colours,
inner: VecIntoIter<Row>, inner: VecIntoIter<Row>,
} }
impl<'a> Iterator for Iter<'a> { impl<'a> Iterator for TableIter<'a> {
type Item = TextCell; type Item = TextCell;
fn next(&mut self) -> Option<Self::Item> { fn next(&mut self) -> Option<Self::Item> {
self.inner.next().map(|row| { self.inner.next().map(|row| {
let mut cell = let mut cell =
if let (Some(table), Some(cells)) = (self.table, row.cells) { if let Some(cells) = row.cells {
table.render(cells) self.table.render(cells)
} }
else { else {
let mut cell = TextCell::default(); let mut cell = TextCell::default();
...@@ -352,23 +359,17 @@ impl<'a> Iterator for Iter<'a> { ...@@ -352,23 +359,17 @@ impl<'a> Iterator for Iter<'a> {
cell cell
}; };
let mut filename = TextCell::default();
for tree_part in self.tree_trunk.new_row(row.depth, row.last) { 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 // If any tree characters have been printed, then add an extra
// space, which makes the output look much better. // space, which makes the output look much better.
if row.depth != 0 { if row.depth != 0 {
filename.add_spaces(1); cell.add_spaces(1);
} }
// Print the name without worrying about padding. cell.append(row.name);
filename.append(row.name);
cell.append(filename);
cell cell
}) })
} }
...@@ -396,3 +397,34 @@ pub struct Row { ...@@ -396,3 +397,34 @@ pub struct Row {
/// when calculating the tree view. /// when calculating the tree view.
pub last: bool, pub last: bool,
} }
pub struct Iter<'a> {
tree_trunk: TreeTrunk,
colours: &'a Colours,
inner: VecIntoIter<Row>,
}
impl<'a> Iterator for Iter<'a> {
type Item = TextCell;
fn next(&mut self) -> Option<Self::Item> {
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
})
}
}
...@@ -124,7 +124,7 @@ impl<'a> Render<'a> { ...@@ -124,7 +124,7 @@ impl<'a> Render<'a> {
} }
let columns: Vec<_> = tables.into_iter().map(|(table, details_rows)| { let columns: Vec<_> = tables.into_iter().map(|(table, details_rows)| {
drender.iterate(Some(&table), details_rows).collect::<Vec<_>>() drender.iterate_with_table(table, details_rows).collect::<Vec<_>>()
}).collect(); }).collect();
let direction = if self.grid.across { grid::Direction::LeftToRight } let direction = if self.grid.across { grid::Direction::LeftToRight }
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册