An easy to use library for pretty print tables of Rust struct
s and enum
s.
To print a list of structs or enums as a table.
Implement Tabled
trait for your struct/enum with or annotate it with a #[derive(Tabled)]
macro. Then call a table
macro.
```rust use tabled::{Tabled, table};
struct Language { name: &'static str, designedby: &'static str, inventedyear: usize, }
let languages = vec![ Language{ name: "C", designedby: "Dennis Ritchie", inventedyear: 1972 }, Language{ name: "Rust", designedby: "Graydon Hoare", inventedyear: 2010 }, Language{ name: "Go", designedby: "Rob Pike", inventedyear: 2009 }, ];
let table = table!(&languages); let expected = "+------+----------------+---------------+\n\ | name | designedby | inventedyear |\n\ +------+----------------+---------------+\n\ | C | Dennis Ritchie | 1972 |\n\ +------+----------------+---------------+\n\ | Rust | Graydon Hoare | 2010 |\n\ +------+----------------+---------------+\n\ | Go | Rob Pike | 2009 |\n\ +------+----------------+---------------+\n";
assert_eq!(expected, table); ```
To be able to use a Tabled
macro each field should implement std::fmt::Display
otherwise it will not work.
The following example will cause a error.
```rust,compile_fail use tabled::Tabled;
struct SomeType { field1: SomeOtherType, }
struct SomeOtherType; ```
Most of the default types implements the trait out of the box.
rust
use tabled::table;
let some_numbers = [1, 2, 3];
let table = table!(&some_numbers);
A list of ready to use styles. A shocases for the data in the Usage section.
Styles can be chosen by passing a Style
argument like this to table!
macro.
rust
let table = table!(&data, Style::psql());
,
+------+----------------+---------------+
| name | designed_by | invented_year |
+------+----------------+---------------+
| C | Dennis Ritchie | 1972 |
+------+----------------+---------------+
| Rust | Graydon Hoare | 2010 |
+------+----------------+---------------+
| Go | Rob Pike | 2009 |
+------+----------------+---------------+
name | designed_by | invented_year
------+----------------+---------------
C | Dennis Ritchie | 1972
Rust | Graydon Hoare | 2010
Go | Rob Pike | 2009
| name | designed_by | invented_year |
|------+----------------+---------------|
| C | Dennis Ritchie | 1972 |
| Rust | Graydon Hoare | 2010 |
| Go | Rob Pike | 2009 |
┌──────┬────────────────┬───────────────┐
│ name │ designed_by │ invented_year │
├──────┼────────────────┼───────────────┤
│ C │ Dennis Ritchie │ 1972 │
├──────┼────────────────┼───────────────┤
│ Rust │ Graydon Hoare │ 2010 │
├──────┼────────────────┼───────────────┤
│ Go │ Rob Pike │ 2009 │
└──────┴────────────────┴───────────────┘
┌──────┬────────────────┬───────────────┐
│ name │ designed_by │ invented_year │
├──────┼────────────────┼───────────────┤
│ C │ Dennis Ritchie │ 1972 │
│ Rust │ Graydon Hoare │ 2010 │
│ Go │ Rob Pike │ 2009 │
└──────┴────────────────┴───────────────┘
name designed_by invented_year
C Dennis Ritchie 1972
Rust Graydon Hoare 2010
Go Rob Pike 2009
You can modify existing styles to fits your needs.
```rust
table!( &data, tabled::Style::noborder() .frame_bottom(Some(Line::short('*', ' ''))) .split(Some(Line::short(' ', ' '))) .inner(' ') ) ```
You can set a alignemt for a Header, Column, Row or All Cells.
rust
table!(
&data,
Style::psql(),
HorizontalAlignment(Full, Alignment::Left)
);
Format function provides an interface for a modification of cells.
rust
let table = table!(
&data,
Style::psql(),
Format(Column(..), |s| { format!("<< {} >>", s) }),
Format(Row(..1), |s| { format!("Head {}", s) }),
);
You can remove a certain rows or column from the table.
rust
table!(&data, Disable::Row(..1), Disable::Column(3..4));
The library doesn't bind you in usage of any color library but to be able to work corectly with color input you should provide a --features color
.
The folowing change on the script in the usage and it's result
rust
let table = table!(
&data,
Style::psql(),
Format(Column(..1), |s| { s.red().to_string() }),
Format(Column(1..2), |s| { s.blue().to_string() }),
Format(Column(2..), |s| { s.green().to_string() }),
);
You can use a #[header("")]
attribute to override a column name.
```rust
struct Person { #[header("Name")] firstname: &'static str, #[header("Surname")] lastname: &'static str, } ```
You can mark filds as hidden in which case they fill be ignored and not be present on a sheet.
A similar affect could be achived by the means of a Disable
setting.
rust
struct Person {
#[header(hidden = true)]
id: u8,
#[header("field 2", hidden)]
number: &'static str,
name: &'static str,
}
You also can combine objets which implements Tabled
by means of tuples, you will get a combined columns of them.
```rust use tabled::{Tabled, table, Style};
enum Domain { Security, Embeded, Frontend, Unknown, }
struct Developer(#[header("name")] &'static str);
let data = vec![ (Developer("Terri Kshlerin"), Domain::Embeded), (Developer("Catalina Dicki"), Domain::Security), (Developer("Jennie Schmeler"), Domain::Frontend), (Developer("Maxim Zhiburt"), Domain::Unknown), ];
let table = table!(data, Style::psql());
assert_eq!( table, concat!( " name | Security | Embeded | Frontend | Unknown \n", "-----------------+----------+---------+----------+---------\n", " Terri Kshlerin | | + | | \n", " Catalina Dicki | + | | | \n", " Jennie Schmeler | | | + | \n", " Maxim Zhiburt | | | | + \n" ) ); ```
You can peak your target for settings using and
and not
methods for an object.
rust
Full.not(Row(..1)) // peak all cells except header
Head.and(Column(..1)).not(Cell(0, 0)) // peak a header and first column except a (0, 0) cell