summaryrefslogtreecommitdiffstats
path: root/src/format.rs
diff options
context:
space:
mode:
authorpierresy <pierre-henri.symoneaux@alcatel-lucent.com>2015-12-08 14:44:56 +0100
committerpierresy <pierre-henri.symoneaux@alcatel-lucent.com>2015-12-08 14:44:56 +0100
commit0f7aefb3b86a2b2e2c4df44a013f2d2e9f8bb845 (patch)
tree3c0720cec5a533e223591fb3ee998fcc3848913a /src/format.rs
parentb580bf876b11c1be8e07b267cc402498faa4b7a4 (diff)
Fixes #5 : Improved capability to not print column separators
Diffstat (limited to 'src/format.rs')
-rw-r--r--src/format.rs44
1 files changed, 32 insertions, 12 deletions
diff --git a/src/format.rs b/src/format.rs
index c18570c..8b9a32d 100644
--- a/src/format.rs
+++ b/src/format.rs
@@ -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);
}
@@ -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>
}
@@ -54,14 +58,18 @@ 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(());
}
@@ -69,14 +77,17 @@ impl TableFormat {
/// 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(())
+ };
}
}
@@ -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};