summaryrefslogtreecommitdiffstats
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
parent0837f83b7d50dfcd1d17025c6f09cde810c1b5c7 (diff)
Issue #2 : Refactored code
-rw-r--r--Cargo.toml2
-rw-r--r--README.md2
-rw-r--r--TODO.md1
-rw-r--r--src/cell.rs3
-rw-r--r--src/lib.rs28
-rw-r--r--src/main.rs2
-rw-r--r--src/row.rs32
7 files changed, 32 insertions, 38 deletions
diff --git a/Cargo.toml b/Cargo.toml
index 8611ae8..6baf54d 100644
--- a/Cargo.toml
+++ b/Cargo.toml
@@ -1,7 +1,7 @@
[package]
name = "prettytable-rs"
-version = "0.3.0"
+version = "0.4.0"
description = "A small rust library that print aligned and formatted tables"
repository = "https://github.com/phsym/prettytable-rs"
documentation = "http://phsym.github.io/prettytable-rs"
diff --git a/README.md b/README.md
index bc77aa2..c545bc6 100644
--- a/README.md
+++ b/README.md
@@ -29,7 +29,7 @@ More often, you will include the library as a dependency to your project. In ord
```toml
[dependencies]
-prettytable-rs = "0.2.0"
+prettytable-rs = "0.4.0"
```
diff --git a/TODO.md b/TODO.md
index 2021f56..6a8810a 100644
--- a/TODO.md
+++ b/TODO.md
@@ -2,7 +2,6 @@
## Features :
* Select a range of rows to print
-* Manage formatting with "term" library
* Limit cell width and split content if needed
* Limit table width and auto adjust cell width as needed
* Add capability to prevent new lines in cell, by replacing them with spaces
diff --git a/src/cell.rs b/src/cell.rs
index e47e6fc..04f8722 100644
--- a/src/cell.rs
+++ b/src/cell.rs
@@ -49,10 +49,12 @@ impl Cell {
self.align = align;
}
+ /// Add a style attribute to the cell
pub fn style(&mut self, attr: Attr) {
self.style.push(attr);
}
+ /// Add a style attribute to the cell. Can be chained
pub fn with_style(mut self, attr: Attr) -> Cell {
self.style(attr);
return self;
@@ -89,6 +91,7 @@ impl Cell {
}
}
+ /// 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> {
for a in &self.style {
try!(out.attr(a.clone()));
diff --git a/src/lib.rs b/src/lib.rs
index 5ef830c..1ab4f57 100644
--- a/src/lib.rs
+++ b/src/lib.rs
@@ -158,37 +158,31 @@ impl Table {
return ColumnIterMut(self.rows.iter_mut(), column);
}
- /// Print the table to `out`
- pub fn print<T: Write>(&self, out: &mut T) -> Result<(), Error> {
+ /// Internal only
+ fn __print<T: Write+?Sized, F>(&self, out: &mut T, f: F) -> Result<(), Error> where F: Fn(&Row, &mut T, &TableFormat, &[usize]) -> Result<(), Error> {
// Compute columns width
let col_width = self.get_all_column_width();
try!(self.format.print_line_separator(out, &col_width));
if let Some(ref t) = self.titles {
- try!(t.print(out, &self.format, &col_width));
+ try!(f(t, out, &self.format, &col_width));
try!(self.format.print_title_separator(out, &col_width));
}
// Print rows
for r in &self.rows {
- try!(r.print(out, &self.format, &col_width));
+ try!(f(r, out, &self.format, &col_width));
try!(self.format.print_line_separator(out, &col_width));
}
return out.flush();
}
+ /// Print the table to `out`
+ pub fn print<T: Write+?Sized>(&self, out: &mut T) -> Result<(), Error> {
+ return self.__print(out, Row::print);
+ }
+
+ /// Print the table to terminal `out`, applying styles when needed
pub fn print_term<T: Terminal+?Sized>(&self, out: &mut T) -> Result<(), Error> {
- // Compute columns width
- let col_width = self.get_all_column_width();
- try!(self.format.print_line_separator(out, &col_width));
- if let Some(ref t) = self.titles {
- try!(t.print_term(out, &self.format, &col_width));
- try!(self.format.print_title_separator(out, &col_width));
- }
- // Print rows
- for r in &self.rows {
- try!(r.print_term(out, &self.format, &col_width));
- try!(self.format.print_line_separator(out, &col_width));
- }
- return out.flush();
+ return self.__print(out, Row::print_term);
}
/// Print the table to standard output
diff --git a/src/main.rs b/src/main.rs
index 5c8f65d..281e7ea 100644
--- a/src/main.rs
+++ b/src/main.rs
@@ -14,7 +14,7 @@ fn main() {
table.add_row(row!["foobar", "bar", "foo"]);
table.add_row(Row::new(vec![
Cell::new("foobar2"),
- Cell::new("bar2").with_style(Attr::BackgroundColor(color::GREEN)).with_style(Attr::ForegroundColor(color::RED)),
+ Cell::new("bar2").with_style(Attr::ForegroundColor(color::RED)),
Cell::new("foo2")])
);
for cell in table.column_iter_mut(2) {
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);
}
}