summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorpierresy <pierre-henri.symoneaux@alcatel-lucent.com>2015-11-09 18:45:36 +0100
committerpierresy <pierre-henri.symoneaux@alcatel-lucent.com>2015-11-09 18:45:36 +0100
commit558bccf0dd5a07e2ec0ebab9e46ed14837167a8a (patch)
treef22251a88ee4890639a728c3edeafcf6abcbb064
parent95d6db992e2816da0bd8173aacbde600e279d78d (diff)
#2 : Improved macros, added some doc and updated readme.
-rw-r--r--README.md84
-rw-r--r--src/cell.rs73
-rw-r--r--src/lib.rs30
-rw-r--r--src/main.rs13
-rw-r--r--src/row.rs34
5 files changed, 192 insertions, 42 deletions
diff --git a/README.md b/README.md
index c545bc6..f232ae6 100644
--- a/README.md
+++ b/README.md
@@ -25,6 +25,9 @@ And to build html documentation, run
> cargo doc
# How to use
+
+## Including
+
More often, you will include the library as a dependency to your project. In order to do this, add the following lines to your **Cargo.toml** file :
```toml
@@ -33,7 +36,9 @@ prettytable-rs = "0.4.0"
```
-Then you can start using it the following way :
+## Basic usage
+
+You can start using it the following way :
```rust
#[macro_use] extern crate prettytable;
@@ -69,6 +74,8 @@ 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 :
```rust
#[macro_use] extern crate prettytable;
@@ -118,4 +125,79 @@ Would print the following text :
Rows may have different numbers of cells. The table will automatically adapt to the largest row by printing additional empty cells in smaller rows.
+## Do it with style
+
+Tables can be added some style like colors (background / foreground), bold, and italic, thanks to the `term` crate.
+
+You can add `term` style attributes to cells programmatically :
+```rust
+extern crate term;
+use term::{Attr, color};
+
+(...)
+
+table.add_row(Row::new(vec![
+ Cell::new("foobar2")
+ .with_style(Attr::ForegroundColor(color::GREEN))
+ .with_style(Attr::Bold),
+ Cell::new("bar2")
+ .with_style(Attr::ForegroundColor(color::RED)),
+ Cell::new("foo2")])
+);
+```
+
+Or you can use the style string :
+```rust
+Cell::new("foo2").style_spec("FrByc")
+```
+
+Where **FrBybc** means **F**oreground: **r**ed, **B**ackground: **y**ellow, **b**old, **c**enter
+
+With macros it's even simpler :
+
+In rows, for each cells :
+```rust
+row![FrByb:"ABC", FrByb:"DEFG", "HIJKLMN"];
+```
+Or for the whole row :
+```rust
+row![FY -> "styled", "bar", "foo"];
+```
+In tables, for each cells :
+```rust
+table!([FrBybl:"A", FrBybc:"B", FrBybr:"C"], [123, 234, 345, 456]);
+```
+Or for each rows :
+```rust
+table!([Frb -> "A", "B", "C"], [Frb -> 1, 2, 3, 4], [1, 2, 3]);
+```
+
+### List of style specifiers :
+
+* **F** : **F**oreground (must be followed by a color specifier)
+* **B** : **B**ackground (must be followed by a color specifier)
+* **b** : **b**old
+* **i** : **i**talic
+* **u** : **u**nderline
+* **c** : Align **c**enter
+* **l** : Align **l**eft
+* **r** : Align **r**ight
+* **d** : **d**efault style
+
+### List of color specifiers :
+
+* **r** : Red
+* **b** : Blue
+* **g** : Green
+* **y** : Yellow
+* **c** : Cyan
+* **m** : Magenta
+* **w** : White
+* **d** : Black
+
+Capital letters are for **bright** colors. Eg :
+* **R** : Bright Red
+* **B** : Bright Blue
+* ... and so on ...
+
Additional examples are provided in documentation and in [examples](./examples/) directory
diff --git a/src/cell.rs b/src/cell.rs
index bfe78c5..2fd50d2 100644
--- a/src/cell.rs
+++ b/src/cell.rs
@@ -66,6 +66,45 @@ impl Cell {
self.align(Align::LEFT);
}
+ /// Set the cell's style by applying the given specifier string
+ ///
+ /// # Style spec syntax
+ ///
+ /// The syntax for the style specifier looks like this :
+ /// **FrBybl** which means **F**oreground **r**ed **B**ackground **y**ellow **b**old **l**eft
+ ///
+ /// ### List of supported specifiers :
+ ///
+ /// * **F** : **F**oreground (must be followed by a color specifier)
+ /// * **B** : **B**ackground (must be followed by a color specifier)
+ /// * **b** : **b**old
+ /// * **i** : **i**talic
+ /// * **u** : **u**nderline
+ /// * **c** : Align **c**enter
+ /// * **l** : Align **l**eft
+ /// * **r** : Align **r**ight
+ /// * **d** : **d**efault style
+ ///
+ /// ### List of color specifiers :
+ ///
+ /// * **r** : Red
+ /// * **b** : Blue
+ /// * **g** : Green
+ /// * **y** : Yellow
+ /// * **c** : Cyan
+ /// * **m** : Magenta
+ /// * **w** : White
+ /// * **d** : Black
+ ///
+ /// And capital letters are for **bright** colors.
+ /// Eg :
+ ///
+ /// * **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;
@@ -179,6 +218,40 @@ impl Default for Cell {
}
}
+/// This macro simplifies `Cell` creation
+///
+/// Support 2 syntax : With and without style specification.
+/// # Syntax
+/// ```text
+/// cell!(value);
+/// ```
+/// or
+///
+/// ```text
+/// cell!(spec:value);
+/// ```
+/// Value must implement the `std::string::ToString` trait
+///
+/// For details about style specifier syntax, check doc for [Cell::style_spec](cell/struct.Cell.html#method.style_spec) method
+/// # Example
+/// ```
+/// # #[macro_use] extern crate prettytable;
+/// # fn main() {
+/// let cell = cell!("value");
+/// // Do something with the cell
+/// # drop(cell);
+/// // Create a cell with style (Red foreground, Bold, aligned to left);
+/// let styled = cell!(Frbl:"value");
+/// # drop(styled);
+/// # }
+/// ```
+#[macro_export]
+macro_rules! cell {
+ () => ($crate::cell::Cell::default());
+ ($value:expr) => ($crate::cell::Cell::new(&$value.to_string()));
+ ($style:ident : $value:expr) => (cell!($value).style_spec(stringify!($style)));
+}
+
#[cfg(test)]
mod tests {
use cell::Cell;
diff --git a/src/lib.rs b/src/lib.rs
index cef79c2..501c445 100644
--- a/src/lib.rs
+++ b/src/lib.rs
@@ -249,7 +249,9 @@ impl <'a> std::iter::Iterator for ColumnIterMut<'a> {
///
/// All the arguments used for elements must implement the `std::string::ToString` trait
/// # Syntax
+/// ```text
/// table!([Element1_ row1, Element2_ row1, ...], [Element1_row2, ...], ...);
+/// ```
///
/// # Example
/// ```
@@ -265,14 +267,8 @@ impl <'a> std::iter::Iterator for ColumnIterMut<'a> {
/// ```
#[macro_export]
macro_rules! table {
- ($([$($value:expr), *]), *) => (
- $crate::Table::init(vec![$(row![$($value), *]), *])
- );
- ($([$($style: tt : $value:expr), *]), *) => (
- $crate::Table::init(vec![$(row![$($style : $value), *]), *])
- );
- ($($style: tt : [$($value:expr), *]), *) => (
- $crate::Table::init(vec![$(row![$style : $($value), *]), *])
+ ($([$($content:tt)*]), *) => (
+ $crate::Table::init(vec![$(row![$($content)*]), *])
);
}
@@ -281,23 +277,9 @@ macro_rules! table {
/// The syntax is the same that the one for the `table!` macro
#[macro_export]
macro_rules! ptable {
- ($([$($value: expr), *]), *) => (
- {
- let tab = table!($([$($value), *]), *);
- tab.printstd();
- tab
- }
- );
- ($([$($style: tt : $value: expr), *]), *) => (
- {
- let tab = table!($([$($style : $value), *]), *);
- tab.printstd();
- tab
- }
- );
- ($($style: tt : [$($value: expr), *]), *) => (
+ ($($content:tt)*) => (
{
- let tab = table!($($style : [$($value), *]), *);
+ let tab = table!($($content)*);
tab.printstd();
tab
}
diff --git a/src/main.rs b/src/main.rs
index 8a55151..21d7bf5 100644
--- a/src/main.rs
+++ b/src/main.rs
@@ -1,3 +1,4 @@
+//#![feature(trace_macros)]
#[macro_use] extern crate prettytable;
extern crate term;
use prettytable::Table;
@@ -7,13 +8,17 @@ use prettytable::format::*;
use term::{Attr, color};
+//trace_macros!(true);
+
#[allow(dead_code)]
fn main() {
+ let _ = table!();
let mut table = Table::new();
- table.add_row(row![FrByb:"ABC", d:"DEFG", d:"HIJKLMN"]);
+ table.add_row(row![FrByb:"ABC", "DEFG", "HIJKLMN"]);
table.add_row(row!["foobar", "bar", "foo"]);
+ table.add_row(row![]);
// Add style to a full row
- table.add_row(row![FY: "styled", "bar", "foo"]);
+ table.add_row(row![FY -> "styled", "bar", "foo"]);
table.add_row(Row::new(vec![
Cell::new("foobar2"),
// Create a cell with a red foreground color
@@ -35,10 +40,10 @@ fn main() {
// Print a table with some styles on it :
// FrBybl means : Foregound red, Background yellow, bold, left align
// d means : Default, do nothing
- ptable!([FrBybl:"A", FrBybc:"B", FrBybr:"C"], [d:123, d:234, d:345, d:456]);
+ ptable!([FrBybl:"A", "B", FrBybr:"C"], [d:123, 234, 345, 456]);
// You can also apply style to full rows :
- let mut table = table!(Frb:["A", "B", "C"], d:[1, 2, 3, 4], d:["A\nBCCZZZ\nDDD", 2, table]);
+ let mut table = table!([Frb -> "A", "B", "C"], [1, 2, 3, 4], ["A\nBCCZZZ\nDDD", 2, table]);
table.set_titles(row!["Title 1", "Title 2"]);
table.set_format(FORMAT_DEFAULT);
table.printstd();
diff --git a/src/row.rs b/src/row.rs
index 54274de..72588cb 100644
--- a/src/row.rs
+++ b/src/row.rs
@@ -145,24 +145,32 @@ impl <T, A> From<T> for Row where A: ToString, T : IntoIterator<Item=A> {
/// This macro simplifies `Row` creation
///
+/// The syntax support style spec
/// # Example
/// ```
/// # #[macro_use] extern crate prettytable;
/// # fn main() {
-/// let row = row!["Element 1", "Element 2", "Element 3"];
-/// // Do something with row
-/// # drop(row);
+/// // Create a normal row
+/// let row1 = row!["Element 1", "Element 2", "Element 3"];
+/// // Create a row with all cells formatted with red foreground color, yellow background color
+/// // bold, italic, align in the center of the cell
+/// let row2 = row![FrBybic -> "Element 1", "Element 2", "Element 3"];
+/// // Create a row with first cell in blue, second one in red, and last one with default style
+/// let row3 = row![Fb:"blue", Fr:"red", "normal"];
+/// // Do something with rows
+/// # drop(row1);
+/// # drop(row2);
+/// # drop(row3);
/// # }
/// ```
#[macro_export]
macro_rules! row {
- ($($value: expr), *) => (
- $crate::row::Row::new(vec![$($crate::cell::Cell::new(&$value.to_string())), *]);
- );
- ($($style: tt : $value: expr), *) => (
- $crate::row::Row::new(vec![$($crate::cell::Cell::new(&$value.to_string()).style_spec(stringify!($style))), *]);
- );
- ($style: tt : $($value: expr), *) => (
- $crate::row::Row::new(vec![$($crate::cell::Cell::new(&$value.to_string()).style_spec(stringify!($style))), *]);
- );
-} \ No newline at end of file
+ (($($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, $($n: tt)*) => (row!(($($out)* cell!($style : $value),); $($n)*));
+
+ ($($content:expr), *) => ($crate::row::Row::new(vec![$(cell!($content)), *]));
+ ($style:ident -> $($content:expr), *) => ($crate::row::Row::new(vec![$(cell!($style : $content)), *]));
+ ($($content:tt)*) => ($crate::row::Row::new(row!((); $($content)*)));
+}