/prettytable-rs

A small rust library that print aligned and formatted tables

Primary LanguageRustBSD 3-Clause "New" or "Revised" LicenseBSD-3-Clause

License Build Status Build status Crates.io

prettytable-rs

Documentation

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 build

As usual with Cargo project, simply run

cargo build

And to build html documentation, run

cargo doc

How to use

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 :

[dependencies.prettytable-rs]
git = "https://github.com/phsym/prettytable-rs.git"

Then you can start using it the following way :

#[macro_use] extern crate prettytable;
use prettytable::Table;
use prettytable::row::Row;
use prettytable::cell::Cell;

fn main() {
	// Create the table
	let mut table = Table::new();
	// Add a row
	table.add_row(row!["ABC", "DEFG", "HIJKLMN"]);
    table.add_row(row!["foobar", "bar", "foo"]);
    // Or the more complicated way :
    table.add_row(Row::new(vec![
    		Cell::new(&"foobar2".to_string()),
    		Cell::new(&"bar2".to_string()),
    		Cell::new(&"foo2".to_string())])
    	);
    table.printstd();
}

This code will produce the following output :

+---------+------+---------+
| ABC     | DEFG | HIJKLMN |
+---------+------+---------+
| foobar  | bar  | foo     |
+---------+------+---------+
| foobar2 | bar2 | foo2    |
+---------+------+---------+

To make the code simpler, the table! macro is there for you. The following code would produce the exact same output :

#[macro_use] extern crate prettytable;

fn main() {
	let table = table!(["ABC", "DEFG", "HIJKLMN"],
    				   ["foobar", "bar", "foo"],
    				   ["foobar2", "bar2", "foo2"]
    				  );
    table.printstd();
}

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!(["ABC", "DEFG", "HIJKLMN"],
				   ["foobar", "bar", "foo"],
				   ["foobar2", "bar2", "foo2"]
				  );
let table2 = table!(["Title 1", "Title 2"],
					["This is\na multiline\ncell", "foo"],
					["Yo dawg ;) You can even\nprint tables\ninto tables", table1]
					);
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.

Additional examples are provided in documentation and in examples directory