Skip to content

Commit

Permalink
Fixes #5 : Improved capability to not print column separators
Browse files Browse the repository at this point in the history
  • Loading branch information
pierresy committed Dec 8, 2015
1 parent b580bf8 commit 0f7aefb
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 18 deletions.
44 changes: 32 additions & 12 deletions src/format.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,15 @@ impl LineSeparator {
}

/// Print a full line separator to `out`. `col_width` is a slice containing the width of each column
pub fn print<T: Write+?Sized>(&self, out: &mut T, col_width: &[usize]) -> Result<(), Error> {
try!(out.write_all(&self.cross));
pub fn print<T: Write+?Sized>(&self, out: &mut T, col_width: &[usize], with_colsep: bool) -> Result<(), Error> {
if with_colsep {
try!(out.write_all(&self.cross));
}
for width in col_width {
try!(out.write_all(&vec![self.line[0]; width+2]));
try!(out.write_all(&self.cross));
if with_colsep {
try!(out.write_all(&self.cross));
}
}
return out.write_all(NEWLINE);
}
Expand All @@ -41,7 +45,7 @@ impl LineSeparator {
/// Contains the table formatting rules
#[derive(Clone, Debug)]
pub struct TableFormat {
col_sep: &'static str,
col_sep: Option<[u8; 1]>,
line_sep: Option<LineSeparator>,
title_sep: Option<LineSeparator>
}
Expand All @@ -54,29 +58,36 @@ impl TableFormat {
/// `line_sep` is an optional `LineSeparator` defining how to separate lines.
/// `title_sep` is an optional `LineSeparator` defining the format of the separator after the title line (if set).
/// If `title_sep` is set to `None`, then `line_sep` will be used, f it's set.
pub fn new(col_sep: &'static str, line_sep: Option<LineSeparator>, title_sep: Option<LineSeparator>) -> TableFormat {
return TableFormat{col_sep: col_sep, line_sep: line_sep, title_sep: title_sep};
pub fn new(col_sep: Option<char>, line_sep: Option<LineSeparator>, title_sep: Option<LineSeparator>) -> TableFormat {
let csep = match col_sep {
Some(c) => Some([c as u8]),
None => None
};
return TableFormat{col_sep: csep, line_sep: line_sep, title_sep: title_sep};
}

/// 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, col_width: &[usize]) -> Result<(), Error> {
if let Some(ref l) = self.line_sep {
return l.print(out, col_width);
return l.print(out, col_width, self.col_sep.is_some());
}
return Ok(());
}

/// Print a full title separator to `out`. `col_width` is a slice containing the width of each column
pub fn print_title_separator<T: Write+?Sized>(&self, out: &mut T, col_width: &[usize]) -> Result<(), Error> {
if let Some(ref l) = self.title_sep {
return l.print(out, col_width);
return l.print(out, col_width, self.col_sep.is_some());
}
return self.print_line_separator(out, col_width);
}

/// Print a column separator to `out`
pub fn print_column_separator<T: Write+?Sized>(&self, out: &mut T) -> Result<(), Error> {
return out.write_all(&self.col_sep.as_bytes());
return match self.col_sep {
Some(ref s) => out.write_all(s),
None => Ok(())
};
}
}

Expand All @@ -96,10 +107,19 @@ pub const EQU_PLUS_SEP: LineSeparator = LineSeparator{line: ['=' as u8], cross:
/// | | |
/// +----+----+
/// ```
pub const FORMAT_DEFAULT: TableFormat = TableFormat{col_sep: "|", line_sep: Some(MINUS_PLUS_SEP), title_sep: Some(EQU_PLUS_SEP)};
pub const FORMAT_DEFAULT: TableFormat = TableFormat{col_sep: Some(['|' as u8]), line_sep: Some(MINUS_PLUS_SEP), title_sep: Some(EQU_PLUS_SEP)};

/// Similar to `FORMAT_DEFAULT` but without special separator after title line
pub const FORMAT_NO_LINESEP: TableFormat = TableFormat{col_sep: "|", line_sep: None, title_sep: Some(MINUS_PLUS_SEP)};
pub const FORMAT_NO_TITLE: TableFormat = TableFormat{col_sep: Some(['|' as u8]), line_sep: Some(MINUS_PLUS_SEP), title_sep: Some(MINUS_PLUS_SEP)};

/// With no line separator, but with title separator
pub const FORMAT_NO_LINESEP_WITH_TITLE: TableFormat = TableFormat{col_sep: Some(['|' as u8]), line_sep: None, title_sep: Some(MINUS_PLUS_SEP)};

/// With no line or title separator
pub const FORMAT_NO_LINESEP: TableFormat = TableFormat{col_sep: Some(['|' as u8]), line_sep: None, title_sep: None};

/// No column seprarator
pub const FORMAT_NO_COLSEP: TableFormat = TableFormat{col_sep: None, line_sep: Some(MINUS_PLUS_SEP), title_sep: Some(EQU_PLUS_SEP)};

/// Format for printing a table without any separators (only alignment)
pub const FORMAT_BLANK: TableFormat = TableFormat{col_sep: " ", line_sep: None, title_sep: None};
pub const FORMAT_NO_BORDER: TableFormat = TableFormat{col_sep: None, line_sep: None, title_sep: None};
39 changes: 33 additions & 6 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -323,7 +323,7 @@ mod tests {
use Table;
use row::Row;
use cell::Cell;
use format::TableFormat;
use format::{FORMAT_NO_LINESEP, FORMAT_NO_COLSEP, FORMAT_NO_BORDER};

#[test]
fn table() {
Expand Down Expand Up @@ -369,7 +369,7 @@ mod tests {
#[test]
fn no_linesep() {
let mut table = Table::new();
table.set_format(TableFormat::new("|", None, None));
table.set_format(FORMAT_NO_LINESEP);
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")]));
Expand All @@ -385,11 +385,38 @@ mod tests {
";
assert_eq!(table.to_string().replace("\r\n", "\n"), out);
}

#[test]
fn no_colsep() {
let mut table = Table::new();
table.set_format(FORMAT_NO_COLSEP);
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")]));
assert_eq!(table[1][1].get_content(), "bc");

table[1][1] = Cell::new("newval");
assert_eq!(table[1][1].get_content(), "newval");

let out = "\
------------------
t1 t2 t3
==================
a bc def
------------------
def newval a
------------------
";
println!("{}", out);
println!("____");
println!("{}", table.to_string().replace("\r\n", "\n"));
assert_eq!(table.to_string().replace("\r\n", "\n"), out);
}

#[test]
fn no_borders() {
let mut table = Table::new();
table.set_format(TableFormat::new("", None, None));
table.set_format(FORMAT_NO_BORDER);
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")]));
Expand All @@ -403,9 +430,9 @@ mod tests {
a bc def
def newval a
";
println!("{}", out);
println!("____");
println!("{}", table.to_string().replace("\r\n", "\n"));
println!("{}", out);
println!("____");
println!("{}", table.to_string().replace("\r\n", "\n"));
assert_eq!(out, String::from("\n") + &table.to_string().replace("\r\n", "\n"));
}
}

1 comment on commit 0f7aefb

@hoodie
Copy link
Contributor

@hoodie hoodie commented on 0f7aefb Dec 8, 2015

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Very nice, thanks

Please sign in to comment.