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::new(languages).to_string(); 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!(table, expected);
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::new(&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::new(data).with(Style::psql()).to_string(); 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; | |
pub use papergrid; |
Modules
style |
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 | Format a structure which modifies a |
Full | Head represent all cells on a |
Head | Head represent a row with column names |
Indent | Indent is responsilbe for a left/right/top/bottom indent. |
MaxWidth | Format a structure which modifies a |
Modify | Modify structure provide a conviniet way for applying a set of |
Row | Row denotes a set of cells on given rows on a |
Table | Table structure provides an interface for building a table for types that implements |
Enums
Alignment | Alignment represent a horizontal and vertical alignemt setting for a |
Disable | Disable represent a disable setting for a |
Traits
CellOption | CellOption is trait for configuring a |
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 |