Skip to content

Commit

Permalink
Merge pull request #59 from phsym/custom_indent
Browse files Browse the repository at this point in the history
Customize indent in format
  • Loading branch information
phsym authored Jun 5, 2017
2 parents 4e36ac1 + ca4a6f7 commit 1193d09
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 3 deletions.
37 changes: 35 additions & 2 deletions src/format.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -148,6 +150,7 @@ impl TableFormat {
bottom_sep: None,
pad_left: 0,
pad_right: 0,
indent: 0,
}
}

Expand Down Expand Up @@ -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,
Expand All @@ -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(),
Expand Down Expand Up @@ -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 {
Expand Down
23 changes: 23 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down Expand Up @@ -761,6 +766,24 @@ mod tests {
assert_eq!(out, table.to_string().replace("\r\n", "\n"));
}

#[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();
Expand Down
4 changes: 3 additions & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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);
}
2 changes: 2 additions & 0 deletions src/row.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down

0 comments on commit 1193d09

Please sign in to comment.