diff options
author | Pierre-Henri Symoneaux <pierre-henri.symoneaux@nokia.com> | 2017-06-05 17:15:06 +0200 |
---|---|---|
committer | Pierre-Henri Symoneaux <pierre-henri.symoneaux@nokia.com> | 2017-06-05 17:15:06 +0200 |
commit | a99d195b4a4ed40c969c2dff64353d5273f310a5 (patch) | |
tree | d356d8dc66ef748ca404e27251b75c84e866f38b /src | |
parent | 4e36ac179fc5341b35221018573e265a1f1a0a53 (diff) |
Customize indent in format
Closes #51
Diffstat (limited to 'src')
-rw-r--r-- | src/format.rs | 25 | ||||
-rw-r--r-- | src/lib.rs | 5 | ||||
-rw-r--r-- | src/main.rs | 4 | ||||
-rw-r--r-- | src/row.rs | 1 |
4 files changed, 34 insertions, 1 deletions
diff --git a/src/format.rs b/src/format.rs index 4160737..96f9257 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,7 @@ impl TableFormat { -> Result<(), Error> { match *self.get_sep_for_line(pos) { Some(ref l) => { + write!(out, "{:1$}", "", self.get_indent()); l._print(out, col_width, self.get_padding(), @@ -262,6 +276,11 @@ impl FormatBuilder { FormatBuilder { format: Box::new(TableFormat::new()) } } + /// Creates a new builder initialized with provided format + pub fn from_format(format: TableFormat) -> FormatBuilder { + FormatBuilder { format: Box::new(format) } + } + /// Set left and right padding pub fn padding(mut self, left: usize, right: usize) -> Self { self.format.padding(left, right); @@ -292,6 +311,12 @@ impl FormatBuilder { self } + /// Set global indentation in spaces used when rendering a table + pub fn indent(mut self, spaces: usize) -> Self { + self.format.indent(spaces); + self + } + /// Consume this builder and return the generated `TableFormat` pub fn build(self) -> TableFormat { *self.format @@ -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() 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,7 @@ impl Row { where F: Fn(&Cell, &mut T, usize, usize, bool) -> Result<(), Error> { for i in 0..self.get_height() { + write!(out, "{:1$}", "", format.get_indent()); try!(format.print_column_separator(out, ColumnPosition::Left)); let (lp, rp) = format.get_padding(); for j in 0..col_width.len() { |