summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--src/cell.rs20
-rw-r--r--src/format.rs2
-rw-r--r--src/lib.rs8
-rw-r--r--src/row.rs7
-rw-r--r--src/utils.rs31
5 files changed, 44 insertions, 24 deletions
diff --git a/src/cell.rs b/src/cell.rs
index 2263ca0..1aca166 100644
--- a/src/cell.rs
+++ b/src/cell.rs
@@ -174,16 +174,16 @@ impl Cell {
/// `idx` is the line index to print. `col_width` is the column width used to
/// fill the cells with blanks so it fits in the table.
/// If `ìdx` is higher than this cell's height, it will print empty content
- pub fn print<T: Write+?Sized>(&self, out: &mut T, idx: usize, col_width: usize) -> Result<(), Error> {
+ pub fn print<T: Write+?Sized>(&self, out: &mut T, idx: usize, col_width: usize, skip_right_fill: bool) -> Result<(), Error> {
let c = match self.content.get(idx) {
Some(s) => s.as_ref(),
None => ""
};
- return print_align(out, self.align, c, ' ', col_width)
+ return print_align(out, self.align, c, ' ', col_width, skip_right_fill)
}
/// Apply style then call `print` to print the cell into a terminal
- pub fn print_term<T: Terminal+?Sized>(&self, out: &mut T, idx: usize, col_width: usize) -> Result<(), Error> {
+ pub fn print_term<T: Terminal+?Sized>(&self, out: &mut T, idx: usize, col_width: usize, skip_right_fill: bool) -> Result<(), Error> {
for a in &self.style {
match out.attr(a.clone()) {
Ok(ok) => ok,
@@ -191,7 +191,7 @@ impl Cell {
Err(e) => return Err(term_error_to_io_error(e))
};
}
- try!(self.print(out, idx, col_width));
+ try!(self.print(out, idx, col_width, skip_right_fill));
try!(out.reset().map_err(term_error_to_io_error));
return Ok(());
}
@@ -281,7 +281,7 @@ mod tests {
assert_eq!(ascii_cell.get_width(), 5);
let mut out = StringWriter::new();
- let _ = ascii_cell.print(&mut out, 0, 10);
+ let _ = ascii_cell.print(&mut out, 0, 10, false);
assert_eq!(out.as_string(), "hello ");
}
@@ -291,7 +291,7 @@ mod tests {
assert_eq!(unicode_cell.get_width(), 6);
let mut out = StringWriter::new();
- let _ = unicode_cell.print(&mut out, 0, 10);
+ let _ = unicode_cell.print(&mut out, 0, 10, false);
assert_eq!(out.as_string(), "привет ");
}
@@ -300,7 +300,7 @@ mod tests {
let unicode_cell = Cell::new("由系统自动更新");
assert_eq!(unicode_cell.get_width(), 14);
let mut out = StringWriter::new();
- let _ = unicode_cell.print(&mut out, 0, 20);
+ let _ = unicode_cell.print(&mut out, 0, 20, false);
assert_eq!(out.as_string(), "由系统自动更新 ");
}
@@ -308,7 +308,7 @@ mod tests {
fn align_left() {
let cell = Cell::new_align("test", Alignment::LEFT);
let mut out = StringWriter::new();
- let _ = cell.print(&mut out, 0, 10);
+ let _ = cell.print(&mut out, 0, 10, false);
assert_eq!(out.as_string(), "test ");
}
@@ -316,7 +316,7 @@ mod tests {
fn align_center() {
let cell = Cell::new_align("test", Alignment::CENTER);
let mut out = StringWriter::new();
- let _ = cell.print(&mut out, 0, 10);
+ let _ = cell.print(&mut out, 0, 10, false);
assert_eq!(out.as_string(), " test ");
}
@@ -324,7 +324,7 @@ mod tests {
fn align_right() {
let cell = Cell::new_align("test", Alignment::RIGHT);
let mut out = StringWriter::new();
- let _ = cell.print(&mut out, 0, 10);
+ let _ = cell.print(&mut out, 0, 10, false);
assert_eq!(out.as_string(), " test");
}
diff --git a/src/format.rs b/src/format.rs
index c4b874e..030242b 100644
--- a/src/format.rs
+++ b/src/format.rs
@@ -174,7 +174,7 @@ impl TableFormat {
};
}
- fn get_column_separator(&self, pos: ColumnPosition) -> Option<char> {
+ pub fn get_column_separator(&self, pos: ColumnPosition) -> Option<char> {
return match pos {
ColumnPosition::Left => self.lborder,
ColumnPosition::Intern => self.csep,
diff --git a/src/lib.rs b/src/lib.rs
index 7e373e1..cde2dbe 100644
--- a/src/lib.rs
+++ b/src/lib.rs
@@ -523,11 +523,11 @@ mod tests {
let out = "\
------------------
- t1 t2 t3 \n\
+ t1 t2 t3 \n\
==================
a bc def \n\
------------------
- def newval a \n\
+ def newval a \n\
------------------
";
println!("{}", out);
@@ -549,9 +549,9 @@ mod tests {
assert_eq!(table[1][1].get_content(), "newval");
let out = "\
-\u{0020}t1 t2 t3 \n\
+\u{0020}t1 t2 t3 \n\
\u{0020}a bc def \n\
-\u{0020}def newval a \n\
+\u{0020}def newval a \n\
";
println!("{}", out);
println!("____");
diff --git a/src/row.rs b/src/row.rs
index 1c270ca..b4f7d7b 100644
--- a/src/row.rs
+++ b/src/row.rs
@@ -97,16 +97,17 @@ impl Row {
/// Internal only
fn __print<T:Write+?Sized, F>(&self, out: &mut T, format: &TableFormat, col_width: &[usize], f: F) -> Result<(), Error>
- where F: Fn(&Cell, &mut T, usize, usize) -> Result<(), Error>
+ where F: Fn(&Cell, &mut T, usize, usize, bool) -> Result<(), Error>
{
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]));
+ let skip_r_fill = (j == col_width.len() - 1) && format.get_column_separator(ColumnPosition::Right).is_none();
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]))
+ Some(ref c) => try!(f(c, out, i, col_width[j], skip_r_fill)),
+ None => try!(f(&Cell::default(), out, i, col_width[j], skip_r_fill))
};
try!(out.write(&vec![' ' as u8; rp]));
if j < col_width.len() - 1 {
diff --git a/src/utils.rs b/src/utils.rs
index 669b926..79f56b5 100644
--- a/src/utils.rs
+++ b/src/utils.rs
@@ -45,7 +45,7 @@ impl Write for StringWriter {
}
/// Align/fill a string and print it to `out`
-pub fn print_align<T: Write+?Sized>(out: &mut T, align: Alignment, text: &str, fill: char, size: usize) -> Result<(), Error> {
+pub fn print_align<T: Write+?Sized>(out: &mut T, align: Alignment, text: &str, fill: char, size: usize, skip_right_fill: bool) -> Result<(), Error> {
let text_len = UnicodeWidthStr::width(text);
let mut nfill = if text_len < size { size - text_len } else { 0 };
let n = match align {
@@ -58,7 +58,7 @@ pub fn print_align<T: Write+?Sized>(out: &mut T, align: Alignment, text: &str, f
nfill -= n;
}
try!(out.write(text.as_bytes()));
- if nfill > 0 {
+ if nfill > 0 && ! skip_right_fill {
try!(out.write(&vec![fill as u8; nfill]));
}
return Ok(());
@@ -83,19 +83,38 @@ mod tests {
#[test]
fn fill_align() {
let mut out = StringWriter::new();
- print_align(&mut out, Alignment::RIGHT, "foo", '*', 10).unwrap();
+ print_align(&mut out, Alignment::RIGHT, "foo", '*', 10, false).unwrap();
assert_eq!(out.as_string(), "*******foo");
let mut out = StringWriter::new();
- print_align(&mut out, Alignment::LEFT, "foo", '*', 10).unwrap();
+ print_align(&mut out, Alignment::LEFT, "foo", '*', 10, false).unwrap();
assert_eq!(out.as_string(), "foo*******");
let mut out = StringWriter::new();
- print_align(&mut out, Alignment::CENTER, "foo", '*', 10).unwrap();
+ print_align(&mut out, Alignment::CENTER, "foo", '*', 10, false).unwrap();
assert_eq!(out.as_string(), "***foo****");
let mut out = StringWriter::new();
- print_align(&mut out, Alignment::CENTER, "foo", '*', 1).unwrap();
+ print_align(&mut out, Alignment::CENTER, "foo", '*', 1, false).unwrap();
+ assert_eq!(out.as_string(), "foo");
+ }
+
+ #[test]
+ fn skip_right_fill() {
+ let mut out = StringWriter::new();
+ print_align(&mut out, Alignment::RIGHT, "foo", '*', 10, true).unwrap();
+ assert_eq!(out.as_string(), "*******foo");
+
+ let mut out = StringWriter::new();
+ print_align(&mut out, Alignment::LEFT, "foo", '*', 10, true).unwrap();
+ assert_eq!(out.as_string(), "foo");
+
+ let mut out = StringWriter::new();
+ print_align(&mut out, Alignment::CENTER, "foo", '*', 10, true).unwrap();
+ assert_eq!(out.as_string(), "***foo");
+
+ let mut out = StringWriter::new();
+ print_align(&mut out, Alignment::CENTER, "foo", '*', 1, false).unwrap();
assert_eq!(out.as_string(), "foo");
}
}