summaryrefslogtreecommitdiffstats
path: root/src/row.rs
diff options
context:
space:
mode:
authorpierresy <pierre-henri.symoneaux@alcatel-lucent.com>2015-11-07 15:13:26 +0100
committerpierresy <pierre-henri.symoneaux@alcatel-lucent.com>2015-11-07 15:13:26 +0100
commitc551d1f70c4994ae75a0750d3bebb55eb56bb7e2 (patch)
tree37c5054979bae321e70d2b678abb34eaa7496760 /src/row.rs
parent0837f83b7d50dfcd1d17025c6f09cde810c1b5c7 (diff)
Issue #2 : Refactored code
Diffstat (limited to 'src/row.rs')
-rw-r--r--src/row.rs32
1 files changed, 15 insertions, 17 deletions
diff --git a/src/row.rs b/src/row.rs
index bd21793..7771298 100644
--- a/src/row.rs
+++ b/src/row.rs
@@ -94,15 +94,16 @@ impl Row {
}
}
- /// Print the row to `out`, with `separator` as column separator, and `col_width`
- /// specifying the width of each columns
- pub fn print<T: Write>(&self, out: &mut T, format: &TableFormat, col_width: &[usize]) -> Result<(), Error> {
+ /// 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>
+ {
for i in 0..self.get_height() {
try!(format.print_column_separator(out));
for j in 0..col_width.len() {
match self.get_cell(j) {
- Some(ref c) => try!(c.print(out, i, col_width[j])),
- None => try!(Cell::default().print(out, i, col_width[j]))
+ Some(ref c) => try!(f(c, out, i, col_width[j])),
+ None => try!(f(&Cell::default(), out, i, col_width[j]))
};
try!(format.print_column_separator(out));
}
@@ -111,19 +112,16 @@ impl Row {
return Ok(());
}
+ /// Print the row to `out`, with `separator` as column separator, and `col_width`
+ /// specifying the width of each columns
+ pub fn print<T: Write+?Sized>(&self, out: &mut T, format: &TableFormat, col_width: &[usize]) -> Result<(), Error> {
+ return self.__print(out, format, col_width, Cell::print);
+ }
+
+ /// Print the row to terminal `out`, with `separator` as column separator, and `col_width`
+ /// specifying the width of each columns. Apply style when needed
pub fn print_term<T: Terminal+?Sized>(&self, out: &mut T, format: &TableFormat, col_width: &[usize]) -> Result<(), Error> {
- for i in 0..self.get_height() {
- try!(format.print_column_separator(out));
- for j in 0..col_width.len() {
- match self.get_cell(j) {
- Some(ref c) => try!(c.print_term(out, i, col_width[j])),
- None => try!(Cell::default().print_term(out, i, col_width[j]))
- };
- try!(format.print_column_separator(out));
- }
- try!(out.write_all(NEWLINE));
- }
- return Ok(());
+ return self.__print(out, format, col_width, Cell::print_term);
}
}