diff options
author | Pierre-Henri Symoneaux <phsym@users.noreply.github.com> | 2017-06-05 20:21:24 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2017-06-05 20:21:24 +0200 |
commit | 1193d099ada1e3fe491726d11ed3c5fec243a7fc (patch) | |
tree | 37a57fb4328dbaca38b00ad475ebca715e9631b5 | |
parent | 4e36ac179fc5341b35221018573e265a1f1a0a53 (diff) | |
parent | ca4a6f7e96ccfd476536a6829504c61b11c18da0 (diff) |
Merge pull request #59 from phsym/custom_indent
Customize indent in format
-rw-r--r-- | src/format.rs | 37 | ||||
-rw-r--r-- | src/lib.rs | 23 | ||||
-rw-r--r-- | src/main.rs | 4 | ||||
-rw-r--r-- | src/row.rs | 2 |
4 files changed, 63 insertions, 3 deletions
diff --git a/src/format.rs b/src/format.rs index 4160737..6b83da7 100644 --- a/src/format.rs +++ b/src/format.rs @@ -133,6 +133,8 @@ pub struct TableFormat { pad_left: usize, /// Right padding pad_right: usize, + /// Global indentation when rendering the table + indent: usize, } impl TableFormat { @@ -148,6 +150,7 @@ impl TableFormat { bottom_sep: None, pad_left: 0, pad_right: 0, + indent: 0, } } @@ -204,6 +207,16 @@ impl TableFormat { } } + /// Set global indentation in spaces used when rendering a table + pub fn indent(&mut self, spaces: usize) { + self.indent = spaces; + } + + /// Get global indentation in spaces used when rendering a table + pub fn get_indent(&self) -> usize { + self.indent + } + /// Print a full line separator to `out`. `col_width` is a slice containing the width of each column pub fn print_line_separator<T: Write + ?Sized>(&self, out: &mut T, @@ -212,6 +225,8 @@ impl TableFormat { -> Result<(), Error> { match *self.get_sep_for_line(pos) { Some(ref l) => { + //TODO: Wrap this into dedicated function one day + try!(out.write_all(&vec![b' '; self.get_indent()])); l._print(out, col_width, self.get_padding(), @@ -292,12 +307,30 @@ impl FormatBuilder { self } - /// Consume this builder and return the generated `TableFormat` - pub fn build(self) -> TableFormat { + /// Set global indentation in spaces used when rendering a table + pub fn indent(mut self, spaces: usize) -> Self { + self.format.indent(spaces); + self + } + + /// Return the generated `TableFormat` + pub fn build(&self) -> TableFormat { *self.format } } +impl Into<TableFormat> for FormatBuilder { + fn into(self) -> TableFormat { + *self.format + } +} + +impl From<TableFormat> for FormatBuilder { + fn from(fmt: TableFormat) -> Self { + FormatBuilder { format: Box::new(fmt) } + } +} + /// Predifined formats. Those constants are lazily evaluated when /// the corresponding struct is dereferenced pub mod consts { @@ -283,6 +283,11 @@ impl Table { *self.format = format; } + /// Get a mutable reference to the internal format + pub fn get_format(&mut self) -> &mut TableFormat { + &mut self.format + } + /// Compute and return the number of column pub fn get_column_num(&self) -> usize { self.as_ref().get_column_num() @@ -762,6 +767,24 @@ mod tests { } #[test] + fn indent() { + let mut table = Table::new(); + table.add_row(Row::new(vec![Cell::new("a"), Cell::new("bc"), Cell::new("def")])); + table.add_row(Row::new(vec![Cell::new("def"), Cell::new("bc"), Cell::new("a")])); + table.set_titles(Row::new(vec![Cell::new("t1"), Cell::new("t2"), Cell::new("t3")])); + table.get_format().indent(8); + let out = r" +-----+----+-----+ + | t1 | t2 | t3 | + +=====+====+=====+ + | a | bc | def | + +-----+----+-----+ + | def | bc | a | + +-----+----+-----+ +"; + assert_eq!(table.to_string().replace("\r\n", "\n"), out); + } + + #[test] fn slices() { let mut table = Table::new(); table.set_titles(Row::new(vec![Cell::new("t1"), Cell::new("t2"), Cell::new("t3")])); diff --git a/src/main.rs b/src/main.rs index 52e53dd..dcd8aa8 100644 --- a/src/main.rs +++ b/src/main.rs @@ -37,6 +37,7 @@ fn main() { println!("Modified : "); table.set_element("new_foo", 2, 1).unwrap(); table.printstd(); + // table.get_format().indent(8); // Print a table with some styles on it : // FrBybl means : Foregound red, Background yellow, bold, left align @@ -47,6 +48,7 @@ fn main() { let mut table = table!([Frb => "A", "B", "C"], [1, 2, 3, 4], ["A\nBCCZZZ\nDDD", 2, table]); table.set_titles(row!["Title 1", "Title 2"]); table.set_format(*consts::FORMAT_DEFAULT); + table.get_format().indent(8); table.printstd(); - // println!("{:#?}", table); + // println!("{:#?}", table); } @@ -119,6 +119,8 @@ impl Row { where F: Fn(&Cell, &mut T, usize, usize, bool) -> Result<(), Error> { for i in 0..self.get_height() { + //TODO: Wrap this into dedicated function one day + try!(out.write_all(&vec![b' '; format.get_indent()])); try!(format.print_column_separator(out, ColumnPosition::Left)); let (lp, rp) = format.get_padding(); for j in 0..col_width.len() { |