summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--Cargo.toml1
-rw-r--r--src/cell.rs30
-rw-r--r--src/format.rs8
-rw-r--r--src/lib.rs23
-rw-r--r--src/main.rs5
-rw-r--r--src/row.rs17
6 files changed, 73 insertions, 11 deletions
diff --git a/Cargo.toml b/Cargo.toml
index a7515fd..8611ae8 100644
--- a/Cargo.toml
+++ b/Cargo.toml
@@ -19,3 +19,4 @@ name = "prettytable"
[dependencies]
unicode-width = "*"
+term = "*"
diff --git a/src/cell.rs b/src/cell.rs
index 54637fe..e47e6fc 100644
--- a/src/cell.rs
+++ b/src/cell.rs
@@ -3,6 +3,7 @@
use std::io::{Write, Error};
use std::string::ToString;
use unicode_width::UnicodeWidthStr;
+use term::{Attr, Terminal};
use super::format::Align;
/// Represent a table cell containing a string.
@@ -13,7 +14,8 @@ use super::format::Align;
pub struct Cell {
content: Vec<String>,
width: usize,
- align: Align
+ align: Align,
+ style: Vec<Attr>
}
impl Cell {
@@ -31,7 +33,8 @@ impl Cell {
return Cell {
content: content,
width: width,
- align: align
+ align: align,
+ style: Vec::new()
};
}
@@ -46,6 +49,15 @@ impl Cell {
self.align = align;
}
+ pub fn style(&mut self, attr: Attr) {
+ self.style.push(attr);
+ }
+
+ pub fn with_style(mut self, attr: Attr) -> Cell {
+ self.style(attr);
+ return self;
+ }
+
/// Return the height of the cell
pub fn get_height(&self) -> usize {
return self.content.len();
@@ -65,7 +77,7 @@ 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>(&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) -> Result<(), Error> {
let c = match self.content.get(idx) {
Some(s) => s.as_ref(),
None => ""
@@ -76,6 +88,15 @@ impl Cell {
Align::RIGHT => write!(out, " {: >1$} ", c, col_width),
}
}
+
+ 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()));
+ }
+ try!(self.print(out, idx, col_width));
+ try!(out.reset());
+ return Ok(());
+ }
}
impl <'a, T: ToString> From<&'a T> for Cell {
@@ -96,7 +117,8 @@ impl Default for Cell {
return Cell {
content: vec!["".to_string(); 1],
width: 0,
- align: Align::LEFT
+ align: Align::LEFT,
+ style: Vec::new()
};
}
}
diff --git a/src/format.rs b/src/format.rs
index 048d6d1..fe79b38 100644
--- a/src/format.rs
+++ b/src/format.rs
@@ -28,7 +28,7 @@ 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>(&self, out: &mut T, col_width: &[usize]) -> Result<(), Error> {
+ pub fn print<T: Write+?Sized>(&self, out: &mut T, col_width: &[usize]) -> Result<(), Error> {
try!(out.write_all(&self.cross));
for width in col_width {
try!(out.write_all(&vec![self.line[0]; width+2]));
@@ -59,7 +59,7 @@ impl TableFormat {
}
/// 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>(&self, out: &mut T, col_width: &[usize]) -> Result<(), Error> {
+ 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);
}
@@ -67,7 +67,7 @@ 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>(&self, out: &mut T, col_width: &[usize]) -> Result<(), Error> {
+ 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);
}
@@ -75,7 +75,7 @@ impl TableFormat {
}
/// Print a column separator to `out`
- pub fn print_column_separator<T: Write>(&self, out: &mut T) -> Result<(), Error> {
+ pub fn print_column_separator<T: Write+?Sized>(&self, out: &mut T) -> Result<(), Error> {
return out.write_all(&self.col_sep);
}
}
diff --git a/src/lib.rs b/src/lib.rs
index 08db98a..5ef830c 100644
--- a/src/lib.rs
+++ b/src/lib.rs
@@ -1,10 +1,13 @@
//! A formatted and aligned table printer written in rust
extern crate unicode_width;
+extern crate term;
-use std::io::{stdout, Write, Error};
+use std::io::{Write, Error};
use std::fmt;
use std::iter::{FromIterator, IntoIterator};
+use term::{Terminal, stdout};
+
pub mod cell;
pub mod row;
pub mod format;
@@ -172,11 +175,27 @@ impl Table {
return out.flush();
}
+ 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();
+ }
+
/// Print the table to standard output
/// # Panic
/// Panic if writing to standard output fails
pub fn printstd(&self) {
- self.print(&mut stdout())
+ self.print_term(&mut *stdout().unwrap())
.ok()
.expect("Cannot print table to standard output");
}
diff --git a/src/main.rs b/src/main.rs
index 82da8ac..5c8f65d 100644
--- a/src/main.rs
+++ b/src/main.rs
@@ -4,6 +4,9 @@ use prettytable::row::Row;
use prettytable::cell::Cell;
use prettytable::format::*;
+extern crate term;
+use term::{Attr, color};
+
#[allow(dead_code)]
fn main() {
let mut table = Table::new();
@@ -11,7 +14,7 @@ fn main() {
table.add_row(row!["foobar", "bar", "foo"]);
table.add_row(Row::new(vec![
Cell::new("foobar2"),
- Cell::new("bar2"),
+ Cell::new("bar2").with_style(Attr::BackgroundColor(color::GREEN)).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 4265fe2..bd21793 100644
--- a/src/row.rs
+++ b/src/row.rs
@@ -2,6 +2,8 @@
use std::io::{Write, Error};
use std::iter::FromIterator;
+use term::Terminal;
+
use super::utils::NEWLINE;
use super::cell::Cell;
use super::format::TableFormat;
@@ -108,6 +110,21 @@ impl Row {
}
return Ok(());
}
+
+ 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(());
+ }
}
impl Default for Row {