Crate tabled[][src]

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.

Examples

If you wan’t to build a table for your data. Most likely a starting point is to anotate your type with #[derive(Tabled)].

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

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(#[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"
    )
);

Settings

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

Re-exports

pub use crate::style::Style;
pub use papergrid;

Modules

Structs

Cell denotes a particular cell on a Grid.

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

Combination struct used for chaning Object’s.

Footer renders a Panel at the bottom. See Panel.

Formatting of particular cells on a Grid.

FormatFrom repeatedly uses first possible element from given array unless there’s any elements.

FormatWithIndex is like a Format. But it also provides a row and column index.

Full represents all cells on a Grid

Head represents the row at the top of a [Table].

Header inserts a Panel at the top. See Panel.

Indent is responsible for a left/right/top/bottom indent of particular cells.

Using MaxWidth you can set a max width of an object on a Grid.

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

Panel allows to add a Row which has 1 continues Cell to a 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 any cell on a Table.

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 Cell a single cell. Where cell represented by ‘row’ and ‘column’ indexes.

Object helps to locate a nessesary part of a Grid.

A trait which is responsilbe for configuration of a Grid.

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

Functions

Multiline a helper function for changing multiline content of cell. Using this formatting applied for all rows not to a string as a whole.

Derive Macros