From da124869ca82057cf4dcdce88b4c87db6505b63e Mon Sep 17 00:00:00 2001 From: Abraham Toriz Date: Fri, 16 Jul 2021 09:33:45 -0500 Subject: [PATCH] dont actually print spaces if requested --- src/tabulate.rs | 35 ++++++++++++++++++++++++++++++++++- 1 file changed, 34 insertions(+), 1 deletion(-) diff --git a/src/tabulate.rs b/src/tabulate.rs index 8de2721..e04afe4 100644 --- a/src/tabulate.rs +++ b/src/tabulate.rs @@ -32,6 +32,14 @@ pub struct Col { } impl Col { + pub fn new() -> Col { + Col { + min_width: 0, + align: Align::Left, + max_width: None, + } + } + pub fn min_width(size: usize) -> Col { Col { min_width: size, @@ -125,7 +133,11 @@ impl Tabulate { self.data.into_iter().map(|row| match row { DataOrSep::Sep(c) => { - c.to_string().repeat(widths.iter().sum::() + widths.len() -1) + "\n" + if c == ' ' { + "\n".into() + } else { + c.to_string().repeat(widths.iter().sum::() + widths.len() -1) + "\n" + } }, DataOrSep::Data(d) => { d.into_iter().zip(widths.iter()).zip(cols.iter()).map(|((d, &w), &c)| { @@ -339,6 +351,27 @@ Sun Oct 05, 2008 16:00:00 - 18:00:00 2:00:00 quiúbole 2:00:00 ---------------------------------------------------------- Total 2:00:00 +")); + } + + #[test] + fn tabulate_a_blank_row() { + let mut tabs = Tabulate::with_columns(vec![ + Col::new() + ]); + + tabs.feed(vec!["Hola".into()]); + tabs.separator(' '); + tabs.feed(vec!["adiós".into()]); + tabs.separator('-'); + tabs.feed(vec!["ta güeno".into()]); + + assert_eq!(PrettyString(&tabs.print()), PrettyString("\ +Hola + +adiós +-------- +ta güeno ")); } }