#table #pretty-table #terminal #format #print #pretty-print #macro

no-std tabled

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

31 releases (14 breaking)

0.15.0 Dec 20, 2023
0.14.0 Aug 4, 2023
0.13.0 Jul 24, 2023
0.10.0 Oct 18, 2022
0.1.0 Mar 23, 2020

#1 in Visualization

Download history 48728/week @ 2024-01-03 50540/week @ 2024-01-10 62237/week @ 2024-01-17 61566/week @ 2024-01-24 57501/week @ 2024-01-31 57495/week @ 2024-02-07 55795/week @ 2024-02-14 56913/week @ 2024-02-21 56322/week @ 2024-02-28 58876/week @ 2024-03-06 71436/week @ 2024-03-13 64408/week @ 2024-03-20 54786/week @ 2024-03-27 60173/week @ 2024-04-03 62762/week @ 2024-04-10 54197/week @ 2024-04-17

243,888 downloads per month
Used in 271 crates (214 directly)

MIT license

1MB
22K SLoC

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.

Most of a table configuration can be found in tabled::settings module.

use tabled::{Table, Tabled};

#[derive(Tabled)]
struct Language {
    name: String,
    designed_by: String,
    invented_year: usize,
}

impl Language {
    fn new(name: &str, designed_by: &str, invented_year: usize) -> Self {
        Self {
            name: name.to_string(),
            designed_by: designed_by.to_string(),
            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();

assert_eq!(
    table,
    "+------+----------------+---------------+\n\
     | name | designed_by    | invented_year |\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\
     +------+----------------+---------------+"
);

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

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

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

let table = builder.build()
    .with(Style::ascii_rounded())
    .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",
        "'------------------------------'"
    )
);

Dependencies

~110–560KB
~11K SLoC