summaryrefslogtreecommitdiffstats
path: root/README.md
blob: c2f1966d63c5fab0526c715415be96de2d5c8bc8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
![License](http://img.shields.io/badge/license-BSD-lightgrey.svg)
[![Build Status](https://travis-ci.org/phsym/tabprint.svg)](https://travis-ci.org/phsymtabprint)

# tabprint

*Copyright © 2015 Pierre-Henri Symoneaux*

> THIS SOFTWARE IS DISTRIBUTED WITHOUT ANY WARRANTY <br>
> Check LICENSE.txt file for more information. <br>

A formatted and aligned table printer written in rust. **This is a work in progress for now**.

# 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 :

```toml
[dependencies.tabprint]
git = "https://github.com/phsym/tabprint.git"

```

Then you can start using it the following way :

```rust
extern crate tabprint;
use tabprint::Table;
use tabprint::row::Row;
use tabprint::cell::Cell;

fn main() {
	// Create the table
	let mut table = Table::new(row!["ABC", "DEFG", "HIJKLMN"]);
	// Add a row
    table.add_row(row!["foobar", "bar", "foo"]).unwrap();
    // 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())])
    	).unwrap();
    table.printstd();
}
```

This code will produce the following output :

```text
+---------+------+---------+
| 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 :
```rust
#[macro_use] extern crate tabprint;

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
```rust
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 :
```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    | |
|                         | +---------+------+---------+ |
+-------------------------+------------------------------+
```

Additional examples are provided in documentation and in [examples](./examples/) directory