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!(&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"
        )
    );

Macros

table

Table macro returns a built table as a string. It may take a list of arguments such as Style, HorizontalAlignment, ChangeRing

Structs

ChangeRing

ChangeRing a structure which modifies a Grid in a series of rounds. It calls a function in a cycle for a set of cells.

Column

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

Full

Head represent all cells on a Grid

Head

Head represent a row with column names

HorizontalAlignment

HorizontalAlignment represent a horizontal alignemt setting for a table macros

Row

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

Enums

Alignment

Alignment represents an horizontal aligment of a cell content.

AlignmentObject

AlignmentObject represent a set of cells/rows which should be aligned.

Style

Style is responsible for a look of a table

Traits

Object

Object helps to locate a nessesary part of a Grid.

TableOption

A trait for configuring a Grid.

Tabled

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

Functions

build_grid

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

multiline

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

Derive Macros

Tabled