Crate tabled[−][src]
Expand description
An easy to use library for pretty print tables of Rust struct
s and enum
s.
Get started
The common and probably the best way to begin is to annotate your type with
#[derive(Tabled)]
. You can also implement it on your own as well.
use tabled::{Tabled, table}; #[derive(Tabled)] struct Language { name: &'static str, designed_by: &'static str, invented_year: usize, } let languages = vec![ Language{ name: "C", designed_by: "Dennis Ritchie", invented_year: 1972 }, Language{ name: "Rust", designed_by: "Graydon Hoare", invented_year: 2010 }, Language{ name: "Go", designed_by: "Rob Pike", invented_year: 2009 }, ]; let table = table!(&languages); let expected = "+------+----------------+---------------+\n\ | name | designed_by | invented_year |\n\ +------+----------------+---------------+\n\ | C | Dennis Ritchie | 1972 |\n\ +------+----------------+---------------+\n\ | Rust | Graydon Hoare | 2010 |\n\ +------+----------------+---------------+\n\ | Go | Rob Pike | 2009 |\n\ +------+----------------+---------------+\n"; assert_eq!(expected, table);
We must to know what we print in the field
accordingly each field should implement std::fmt::Display
The example below is not compiled
#[derive(Tabled)] struct SomeType { field1: SomeOtherType, } struct SomeOtherType;
Most of the default types implements the trait out of the box.
use tabled::{Tabled, table}; let some_numbers = [1, 2, 3]; let table = table!(&some_numbers);
You also can combine structures by means of tuples.
use tabled::{Tabled, table, Style}; #[derive(Tabled)] enum Domain { Security, Embeded, Frontend, Unknown, } #[derive(Tabled)] 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" ) );
Re-exports
pub use crate::style::Style; |
Modules
style |
Macros
table | Table macro returns a built table as a string.
It may take a list of arguments such as |
Structs
Cell | Cell denotes a particular of cells on a |
Column | Column denotes a set of cells on given columns on a |
Combination | Combination struct which allows a chain of objects |
Format | ChangeRing a structure which modifies a |
Full | Head represent all cells on a |
Head | Head represent a row with column names |
HorizontalAlignment | HorizontalAlignment represent a horizontal alignemt setting for a |
Row | Row denotes a set of cells on given rows on a |
Enums
Alignment | Alignment represents an horizontal aligment of a cell content. |
Disable | Disable represent a disable setting for a |
Traits
Object | Object helps to locate a nessesary part of a |
TableOption | A trait for configuring a |
Tabled | Tabled a trait responsible for providing a header filds and a row fields. |
Functions
build_grid | Build_grid function build a |
multiline | Multiline a helper function for changing multiline content of cell by rows not as a whole. |
Derive Macros
Tabled |