prettytable-rs
Copyright © 2015 Pierre-Henri Symoneaux
THIS SOFTWARE IS DISTRIBUTED WITHOUT ANY WARRANTY Check LICENSE.txt file for more information.
A formatted and aligned table printer written in rust.
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 :
[]
= "0.5.0"
Basic usage
You can start using it the following way :
extern crate prettytable;
use Table;
use Row;
use Cell;
This code will produce the following output :
+---------+------+---------+
| ABC | DEFG | HIJKLMN |
+---------+------+---------+
| foobar | bar | foo |
+---------+------+---------+
| foobar2 | bar2 | foo2 |
+---------+------+---------+
Using macros
To make the code simpler, the table!
macro is there for you. The following code would produce the same output :
extern crate prettytable;
Using the ptable!
macro would even print it on stdout for you.
Tables also support multiline cells content. As a consequence, you can print a table into another table (yo dawg ;). For example, the following code
let table1 = table!;
let table2 = table!;
table2.printstd;
Would print the following text :
+-------------------------+------------------------------+
| Title 1 | Title 2 |
+-------------------------+------------------------------+
| This is | foo |
| a multiline | |
| cell | |
+-------------------------+------------------------------+
| Yo dawg ;) You can even | +---------+------+---------+ |
| print tables | | ABC | DEFG | HIJKLMN | |
| into tables | +---------+------+---------+ |
| | | foobar | bar | foo | |
| | +---------+------+---------+ |
| | | foobar2 | bar2 | foo2 | |
| | +---------+------+---------+ |
+-------------------------+------------------------------+
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 :
extern crate term;
use ;
table.add_row;
Or you can use the style string :
.style_spec
new
Where FrBybc means Foreground: red, Background: yellow, bold, center
With macros it's even simpler :
In rows, for each cells :
row!;
Or for the whole row :
row!;
In tables, for each cells :
table!;
Or for each rows :
table!;
Or a mix :
table!;
List of style specifiers :
- F : Foreground (must be followed by a color specifier)
- B : Background (must be followed by a color specifier)
- b : bold
- i : italic
- u : underline
- c : Align center
- l : Align left
- r : Align right
- d : default 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 directory