Crate tabled[][src]

Expand description

An easy to use library for pretty print tables of Rust structs and enums.

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

Structs

Cell denotes a particular of cells on a Grid

Column denotes a set of cells on given columns on a Grid

Combination struct which allows a chain of objects

Footer renders information at the bottom. see Panel

Format a structure which modifies a Grid

Head represent all cells on a Grid

Head represent a row with column names

Header renders information at the top. see Panel

Indent is responsilbe for a left/right/top/bottom indent.

Format a structure which modifies a Grid

Modify structure provide a conviniet way for applying a set of CellOptions to the same object.

Panel allows to add a custom panel to table.

Row denotes a set of cells on given rows on a Grid

Table structure provides an interface for building a table for types that implements Tabled.

Enums

Alignment represent a horizontal and vertical alignemt setting for a Table

Disable represent a disable setting for a Table

Traits

CellOption is trait for configuring a Cell which represented by ‘row’ and ‘column’ indexes.

Object helps to locate a nessesary part of a Grid.

A trait for configuring a Grid.

Tabled a trait responsible for providing a header filds and a row fields.

Functions

Build_grid function build a Grid from a data. A table macros should be prefered over this function.

Multiline a helper function for changing multiline content of cell by rows not as a whole.

Derive Macros