Expand description

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

The library is based on a Tabled trait which is used to actually build tables. It also provides an variate of dynamic settings for customization of a Table.

Table can be build from vast majority of Rust’s standart types.

Usage

If you want to build a table for your custom type. A starting point is to a anotate your type with #[derive(Tabled)].

Then you can create Table::new to create a table;

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);

You can also create a table by using TableIteratorExt.

use tabled::TableIteratorExt;
let table = languages.table();

Not all types can derive Tabled trait though. The example below can’t be compiled.

    #[derive(Tabled)]
    struct SomeType {
        field1: SomeOtherType,
    }

    struct SomeOtherType;

We must know what we’re up to print as a field. Because of this each field must implement std::fmt::Display.

Default implementations

As I’ve already mentioned most of the default types implements the trait out of the box.

This allows you to run the following code.

use tabled::{Tabled, Table};
let table = Table::new(&[1, 2, 3]);

Combination of types via tuples

Personally I consider this a feature which drives the library to shine. You can combine any types that implements Tabled trait into one table.

You can also see in this example a #[header("name")] usage which configures a header of a table which will be printed. You could change it dynamically as well.

use tabled::{Tabled, Table, Style};

#[derive(Tabled)]
enum Domain {
    Security,
    Embeded,
    Frontend,
    Unknown,
}

#[derive(Tabled)]
struct Developer(#[tabled(rename = "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"
    )
);

Settings

You can find more examples of settings and attributes in README.md

Re-exports

pub use crate::object::*;
pub use crate::style::Style;

Modules

Builder module provides a Builder type which helps building a Table dynamically.

This module contains settings for render strategy of papergrid.

This module contains a list of Styles which can be applied to change crate::Table styles.

Structs

Concat concatenate tables along a particular axis [Horizontal | Vertical]. It doesn’t do any key or column comparisions like SQL’s join does.

Returns a new Table that reflects a segment of the referenced Table

Footer renders a Panel at the bottom. See Panel.

Formatting function of particular cells on a Grid.

FormatWithIndex is like a Format an abstraction over a function you can use agains a cell.

Header inserts a Panel at the top. See Panel.

Highlight modifies a table style by changing a border of a target Table segment.

Margin is responsible for a left/right/top/bottom outer indent of a grid.

MaxWidth allows you to set a max width of an object on a Grid, using different strategies. It also allows you to set a MaxWidth for a whole table.

MinWidth changes a content in case if it’s length is lower then the boundry.

Modify structure provide an abstraction, to be able to apply a set of CellOptions to the same object.

Padding is responsible for a left/right/top/bottom inner indent of a particular cell.

Panel allows to add a Row which has 1 continues Cell to a Table.

Span represent a horizontal/column span setting for any cell on a crate::Table.

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

Truncate cut the string to a given width if its length exeeds it. Otherwise keeps the content of a cell untouched.

Wrap wraps a string to a new line in case it exeeds the provided max boundry. Otherwise keeps the content of a cell untouched.

Enums

Alignment represent a horizontal and vertical alignemt setting for any cell on a crate::Table.

AlignmentHorizontal represents an horizontal aligment of a cell content.

AlignmentVertical represents an vertical aligment of a cell content.

Disable removes particular rows/columns from a Table.

Rotate can be used to rotate a table by 90 degrees.

Traits

A trait for configuring a single cell. Where cell represented by ‘row’ and ‘column’ indexes.

A trait for IntoIterator whose Item type is bound to Tabled. Any type implements IntoIterator can call this function directly

A trait which is responsilbe for configuration of a Grid.

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

Derive Macros