summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--src/cell.rs16
-rw-r--r--src/format.rs26
-rw-r--r--src/row.rs3
3 files changed, 28 insertions, 17 deletions
diff --git a/src/cell.rs b/src/cell.rs
index 7487234..4c028b1 100644
--- a/src/cell.rs
+++ b/src/cell.rs
@@ -179,9 +179,7 @@ impl Cell {
Some(s) => s.as_ref(),
None => ""
};
- try!(write!(out, " "));
- try!(print_align(out, self.align, c, ' ', col_width));
- return write!(out, " ");
+ return print_align(out, self.align, c, ' ', col_width)
}
/// Apply style then call `print` to print the cell into a terminal
@@ -284,7 +282,7 @@ mod tests {
let mut out = StringWriter::new();
let _ = ascii_cell.print(&mut out, 0, 10);
- assert_eq!(out.as_string(), " hello ");
+ assert_eq!(out.as_string(), "hello ");
}
#[test]
@@ -294,7 +292,7 @@ mod tests {
let mut out = StringWriter::new();
let _ = unicode_cell.print(&mut out, 0, 10);
- assert_eq!(out.as_string(), " привет ");
+ assert_eq!(out.as_string(), "привет ");
}
#[test]
@@ -303,7 +301,7 @@ mod tests {
assert_eq!(unicode_cell.get_width(), 14);
let mut out = StringWriter::new();
let _ = unicode_cell.print(&mut out, 0, 20);
- assert_eq!(out.as_string(), " 由系统自动更新 ");
+ assert_eq!(out.as_string(), "由系统自动更新 ");
}
#[test]
@@ -311,7 +309,7 @@ mod tests {
let cell = Cell::new_align("test", Alignment::LEFT);
let mut out = StringWriter::new();
let _ = cell.print(&mut out, 0, 10);
- assert_eq!(out.as_string(), " test ");
+ assert_eq!(out.as_string(), "test ");
}
#[test]
@@ -319,7 +317,7 @@ mod tests {
let cell = Cell::new_align("test", Alignment::CENTER);
let mut out = StringWriter::new();
let _ = cell.print(&mut out, 0, 10);
- assert_eq!(out.as_string(), " test ");
+ assert_eq!(out.as_string(), " test ");
}
#[test]
@@ -327,7 +325,7 @@ mod tests {
let cell = Cell::new_align("test", Alignment::RIGHT);
let mut out = StringWriter::new();
let _ = cell.print(&mut out, 0, 10);
- assert_eq!(out.as_string(), " test ");
+ assert_eq!(out.as_string(), " test");
}
#[test]
diff --git a/src/format.rs b/src/format.rs
index a08aa55..3d879f1 100644
--- a/src/format.rs
+++ b/src/format.rs
@@ -91,7 +91,11 @@ pub struct TableFormat {
/// Optional top line separator
top_sep: Option<LineSeparator>,
/// Optional bottom line separator
- bottom_sep: Option<LineSeparator>
+ bottom_sep: Option<LineSeparator>,
+ /// Left padding
+ pad_left: usize,
+ /// Right padding
+ pad_right: usize
}
impl Default for TableFormat {
@@ -116,10 +120,16 @@ impl TableFormat {
lsep: lsep,
tsep: tsep,
top_sep: None,
- bottom_sep: None
+ bottom_sep: None,
+ pad_left: 0,
+ pad_right: 0
};
}
+ pub fn get_padding(&self) -> (usize, usize) {
+ return (self.pad_left, self.pad_right);
+ }
+
fn get_sep_for_line(&self, pos: LinePosition) -> &Option<LineSeparator> {
return match pos {
LinePosition::Intern => return &self.lsep,
@@ -173,19 +183,19 @@ pub const EQU_PLUS_SEP: LineSeparator = LineSeparator{line: '=', junc: '+', ljun
/// | | |
/// +----+----+
/// ```
-pub const FORMAT_DEFAULT: TableFormat = TableFormat{csep: Some('|'), lborder: Some('|'), rborder: Some('|'), lsep: Some(MINUS_PLUS_SEP), tsep: Some(EQU_PLUS_SEP), top_sep: Some(MINUS_PLUS_SEP), bottom_sep: Some(MINUS_PLUS_SEP)};
+pub const FORMAT_DEFAULT: TableFormat = TableFormat{csep: Some('|'), lborder: Some('|'), rborder: Some('|'), lsep: Some(MINUS_PLUS_SEP), tsep: Some(EQU_PLUS_SEP), top_sep: Some(MINUS_PLUS_SEP), bottom_sep: Some(MINUS_PLUS_SEP), pad_left: 1, pad_right: 1};
/// Similar to `FORMAT_DEFAULT` but without special separator after title line
-pub const FORMAT_NO_TITLE: TableFormat = TableFormat{csep: Some('|'), lborder: Some('|'), rborder: Some('|'), lsep: Some(MINUS_PLUS_SEP), tsep: Some(MINUS_PLUS_SEP), top_sep: Some(MINUS_PLUS_SEP), bottom_sep: Some(MINUS_PLUS_SEP)};
+pub const FORMAT_NO_TITLE: TableFormat = TableFormat{csep: Some('|'), lborder: Some('|'), rborder: Some('|'), lsep: Some(MINUS_PLUS_SEP), tsep: Some(MINUS_PLUS_SEP), top_sep: Some(MINUS_PLUS_SEP), bottom_sep: Some(MINUS_PLUS_SEP), pad_left: 1, pad_right: 1};
/// With no line separator, but with title separator
-pub const FORMAT_NO_LINESEP_WITH_TITLE: TableFormat = TableFormat{csep: Some('|'), lborder: Some('|'), rborder: Some('|'), lsep: None, tsep: Some(MINUS_PLUS_SEP), top_sep: Some(MINUS_PLUS_SEP), bottom_sep: Some(MINUS_PLUS_SEP)};
+pub const FORMAT_NO_LINESEP_WITH_TITLE: TableFormat = TableFormat{csep: Some('|'), lborder: Some('|'), rborder: Some('|'), lsep: None, tsep: Some(MINUS_PLUS_SEP), top_sep: Some(MINUS_PLUS_SEP), bottom_sep: Some(MINUS_PLUS_SEP), pad_left: 1, pad_right: 1};
/// With no line or title separator
-pub const FORMAT_NO_LINESEP: TableFormat = TableFormat{csep: Some('|'), lborder: Some('|'), rborder: Some('|'), lsep: None, tsep: None, top_sep: None, bottom_sep: None};
+pub const FORMAT_NO_LINESEP: TableFormat = TableFormat{csep: Some('|'), lborder: Some('|'), rborder: Some('|'), lsep: None, tsep: None, top_sep: None, bottom_sep: None, pad_left: 1, pad_right: 1};
/// No column seprarator
-pub const FORMAT_NO_COLSEP: TableFormat = TableFormat{csep: None, lborder: None, rborder: None, lsep: Some(MINUS_PLUS_SEP), tsep: Some(EQU_PLUS_SEP), top_sep: Some(MINUS_PLUS_SEP), bottom_sep: Some(MINUS_PLUS_SEP)};
+pub const FORMAT_NO_COLSEP: TableFormat = TableFormat{csep: None, lborder: None, rborder: None, lsep: Some(MINUS_PLUS_SEP), tsep: Some(EQU_PLUS_SEP), top_sep: Some(MINUS_PLUS_SEP), bottom_sep: Some(MINUS_PLUS_SEP), pad_left: 1, pad_right: 1};
/// Format for printing a table without any separators (only alignment)
-pub const FORMAT_NO_BORDER: TableFormat = TableFormat{csep: None, lborder: None, rborder: None, lsep: None, tsep: None, top_sep: None, bottom_sep: None};
+pub const FORMAT_NO_BORDER: TableFormat = TableFormat{csep: None, lborder: None, rborder: None, lsep: None, tsep: None, top_sep: None, bottom_sep: None, pad_left: 1, pad_right: 1};
diff --git a/src/row.rs b/src/row.rs
index 1c39cc1..1edfa92 100644
--- a/src/row.rs
+++ b/src/row.rs
@@ -101,11 +101,14 @@ impl Row {
{
for i in 0..self.get_height() {
try!(format.print_column_separator(out, ColumnPosition::Left));
+ let (lp, rp) = format.get_padding();
for j in 0..col_width.len() {
+ try!(out.write(&vec![' ' as u8; lp]));
match self.get_cell(j) {
Some(ref c) => try!(f(c, out, i, col_width[j])),
None => try!(f(&Cell::default(), out, i, col_width[j]))
};
+ try!(out.write(&vec![' ' as u8; rp]));
if j < col_width.len() - 1 {
try!(format.print_column_separator(out, ColumnPosition::Intern));
}