github crates.io docs.rs build status coverage dependency status

tabled

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

There are more examples and you can find in this README.

Usage

To print a list of structs or enums as a table your types should implement the the Tabled trait or derive it with a #[derive(Tabled)] macro. Most of the default types implement the trait out of the box.

```rust use tabled::{Table, Tabled};

[derive(Tabled)]

struct Language { name: String, designedby: String, inventedyear: usize, }

impl Language { fn new(name: &str, designedby: &str, inventedyear: usize) -> Self { Self { name: name.tostring(), designedby: designedby.tostring(), invented_year, } } }

let languages = vec![ Language::new("C", "Dennis Ritchie", 1972), Language::new("Go", "Rob Pike", 2009), Language::new("Rust", "Graydon Hoare", 2010), Language::new("Hare", "Drew DeVault", 2022), ];

let table = Table::new(languages).to_string();

let expected = "+------+----------------+---------------+\n\ | name | designedby | inventedyear |\n\ +------+----------------+---------------+\n\ | C | Dennis Ritchie | 1972 |\n\ +------+----------------+---------------+\n\ | Go | Rob Pike | 2009 |\n\ +------+----------------+---------------+\n\ | Rust | Graydon Hoare | 2010 |\n\ +------+----------------+---------------+\n\ | Hare | Drew DeVault | 2022 |\n\ +------+----------------+---------------+";

assert_eq!(table, expected); ```

The same example but we are building a table step by step.

```rust use tabled::{builder::Builder, settings::Style};

let mut builder = Builder::new(); builder.pushrecord(["C", "Dennis Ritchie", "1972"]); builder.pushrecord(["Go", "Rob Pike", "2009"]); builder.pushrecord(["Rust", "Graydon Hoare", "2010"]); builder.pushrecord(["Hare", "Drew DeVault", "2022"]);

let mut table = builder.build(); table.with(Style::ascii_rounded());

let table = table.to_string();

assert_eq!( table, concat!( ".------------------------------.\n", "| C | Dennis Ritchie | 1972 |\n", "| Go | Rob Pike | 2009 |\n", "| Rust | Graydon Hoare | 2010 |\n", "| Hare | Drew DeVault | 2022 |\n", "'------------------------------'" ) ); ```