summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorpierresy <pierre-henri.symoneaux@alcatel-lucent.com>2015-11-19 09:54:38 +0100
committerpierresy <pierre-henri.symoneaux@alcatel-lucent.com>2015-11-19 09:54:38 +0100
commit92c144e657ede392d0ae116bab85f197e64a65cd (patch)
tree3dd26f3d48ffbbca745c2cb19a85dc753aa7fcc4
parent6add8e7b8adfb4f685224c56dae443ea522c3526 (diff)
Updated Cargo.toml and README.md.
Using style string don't panic anymore if some specifier are unknown. Tables and Rows are now Indexable. Empty rows are now correctly printed. New lines use "\n" for all platforms except windows (which uses "\r\n").
-rw-r--r--Cargo.toml7
-rw-r--r--README.md16
-rw-r--r--src/cell.rs13
-rw-r--r--src/lib.rs16
-rw-r--r--src/row.rs20
-rw-r--r--src/utils.rs2
6 files changed, 49 insertions, 25 deletions
diff --git a/Cargo.toml b/Cargo.toml
index 6baf54d..6180943 100644
--- a/Cargo.toml
+++ b/Cargo.toml
@@ -1,13 +1,14 @@
[package]
name = "prettytable-rs"
-version = "0.4.0"
-description = "A small rust library that print aligned and formatted tables"
+version = "0.5.0"
+description = "A library for printing pretty formatted tables in terminal"
+homepage = "https://github.com/phsym/prettytable-rs"
repository = "https://github.com/phsym/prettytable-rs"
documentation = "http://phsym.github.io/prettytable-rs"
readme = "README.md"
authors = [ "Pierre-Henri Symoneaux" ]
-keywords = ["tab", "table", "format", "pretty", "aligned"]
+keywords = ["tab", "table", "format", "pretty", "aligned", "terminal"]
license = "BSD-3-Clause"
[[bin]]
diff --git a/README.md b/README.md
index f232ae6..2166d23 100644
--- a/README.md
+++ b/README.md
@@ -14,16 +14,6 @@
A formatted and aligned table printer written in rust.
-# How to build
-
-As usual with Cargo project, simply run
-
-> cargo build
-
-And to build html documentation, run
-
-> cargo doc
-
# How to use
## Including
@@ -76,7 +66,7 @@ This code will produce the following output :
## Using macros
-To make the code simpler, the `table!` macro is there for you. The following code would produce the exact same output :
+To make the code simpler, the `table!` macro is there for you. The following code would produce the same output :
```rust
#[macro_use] extern crate prettytable;
@@ -171,6 +161,10 @@ Or for each rows :
```rust
table!([Frb -> "A", "B", "C"], [Frb -> 1, 2, 3, 4], [1, 2, 3]);
```
+Or a mix :
+```rust
+table!([Frb -> "A", "B", "C"], [Frb:1, Fgi:2, 3, 4], [1, 2, 3]);
+```
### List of style specifiers :
diff --git a/src/cell.rs b/src/cell.rs
index 2fd50d2..31006ff 100644
--- a/src/cell.rs
+++ b/src/cell.rs
@@ -102,9 +102,6 @@ impl Cell {
/// * **R** : Bright Red
/// * **B** : Bright Blue
/// * ... and so on ...
- ///
- /// # Panic
- /// If the spec string is wrong
pub fn style_spec(mut self, spec: &str) -> Cell {
let mut foreground = false;
let mut background = false;
@@ -127,7 +124,11 @@ impl Cell {
'W' => color::BRIGHT_WHITE,
'd' => color::BLACK,
'D' => color::BRIGHT_BLACK,
- _ => panic!("Unsupported color specifier {}", c)
+ _ => { // Silently ignore unknown tags
+ foreground = false;
+ background = false;
+ continue;
+ }
};
if foreground { self.style(Attr::ForegroundColor(color)); }
else if background { self.style(Attr::BackgroundColor(color)); }
@@ -144,8 +145,8 @@ impl Cell {
'c' => self.align(Align::CENTER),
'l' => self.align(Align::LEFT),
'r' => self.align(Align::RIGHT),
- 'd' => {/*Default : do nothing*/}
- _ => panic!("Unsupported style specifier {}", c)
+ 'd' => {/* Default : do nothing */}
+ _ => {/* Silently ignore unknown tags */}
}
}
}
diff --git a/src/lib.rs b/src/lib.rs
index f4f1361..e1db5e5 100644
--- a/src/lib.rs
+++ b/src/lib.rs
@@ -5,6 +5,7 @@ extern crate term;
use std::io::{Write, Error};
use std::fmt;
use std::iter::{FromIterator, IntoIterator};
+use std::ops::{Index, IndexMut};
use term::{Terminal, stdout};
@@ -32,7 +33,7 @@ impl Table {
return Self::init(Vec::new());
}
- /// Create a table initialized with ``rows`
+ /// Create a table initialized with `rows`
pub fn init(rows: Vec<Row>) -> Table {
return Table {
rows: rows,
@@ -195,6 +196,19 @@ impl Table {
}
}
+impl Index<usize> for Table {
+ type Output = Row;
+ fn index(&self, idx: usize) -> &Self::Output {
+ return &self.rows[idx];
+ }
+}
+
+impl IndexMut<usize> for Table {
+ fn index_mut(&mut self, idx: usize) -> &mut Self::Output {
+ return &mut self.rows[idx];
+ }
+}
+
impl fmt::Display for Table {
fn fmt(&self, fmt: &mut fmt::Formatter) -> Result<(), fmt::Error> {
let mut writer = StringWriter::new();
diff --git a/src/row.rs b/src/row.rs
index e3de678..0a4ca7c 100644
--- a/src/row.rs
+++ b/src/row.rs
@@ -1,6 +1,7 @@
//! This module contains definition of table rows stuff
use std::io::{Write, Error};
use std::iter::FromIterator;
+use std::ops::{Index, IndexMut};
use term::Terminal;
@@ -34,7 +35,7 @@ impl Row {
/// Get the height of this row
pub fn get_height(&self) -> usize {
- let mut height = 0;
+ let mut height = 1; // Minimum height must be 1 to print empty rows
for cell in &self.cells {
let h = cell.get_height();
if h > height {
@@ -131,6 +132,19 @@ impl Default for Row {
}
}
+impl Index<usize> for Row {
+ type Output = Cell;
+ fn index(&self, idx: usize) -> &Self::Output {
+ return &self.cells[idx];
+ }
+}
+
+impl IndexMut<usize> for Row {
+ fn index_mut(&mut self, idx: usize) -> &mut Self::Output {
+ return &mut self.cells[idx];
+ }
+}
+
impl <A: ToString> FromIterator<A> for Row {
fn from_iter<T>(iterator: T) -> Row where T: IntoIterator<Item=A> {
return Self::new(iterator.into_iter().map(|ref e| Cell::from(e)).collect());
@@ -167,9 +181,9 @@ impl <T, A> From<T> for Row where A: ToString, T : IntoIterator<Item=A> {
/// For details about style specifier syntax, check doc for [Cell::style_spec](cell/struct.Cell.html#method.style_spec) method
#[macro_export]
macro_rules! row {
- (($($out:tt)*); $value:expr) => (vec![$($out)* cell!($value),]);
+ (($($out:tt)*); $value:expr) => (vec![$($out)* cell!($value)]);
(($($out:tt)*); $value:expr, $($n:tt)*) => (row!(($($out)* cell!($value),); $($n)*));
- (($($out:tt)*); $style:ident : $value:expr) => (vec![$($out)* cell!($style : $value),]);
+ (($($out:tt)*); $style:ident : $value:expr) => (vec![$($out)* cell!($style : $value)]);
(($($out:tt)*); $style:ident : $value:expr, $($n: tt)*) => (row!(($($out)* cell!($style : $value),); $($n)*));
($($content:expr), *) => ($crate::row::Row::new(vec![$(cell!($content)), *]));
diff --git a/src/utils.rs b/src/utils.rs
index d460441..cfb5619 100644
--- a/src/utils.rs
+++ b/src/utils.rs
@@ -2,7 +2,7 @@
use std::io::{Error, ErrorKind, Write};
use std::str;
-#[cfg(any(unix, macos))]
+#[cfg(not(windows))]
pub static NEWLINE: &'static [u8] = b"\n";
#[cfg(windows)]
pub static NEWLINE: &'static [u8] = b"\r\n";