Crates.io | tabled |
lib.rs | tabled |
version | 0.17.0 |
source | src |
created_at | 2020-03-23 18:57:09.376958 |
updated_at | 2024-11-22 21:15:07.64786 |
description | An easy to use library for pretty print tables of Rust `struct`s and `enum`s. |
homepage | https://github.com/zhiburt/tabled |
repository | https://github.com/zhiburt/tabled |
max_upload_size | |
id | 221861 |
size | 1,545,658 |
An easy to use library for pretty printing tables of Rust struct
s and enum
s.
There are more examples and you can find in this README
.
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};
use testing_table::assert_table;
#[derive(Tabled)]
struct Language<'a> {
name: &'a str,
designed_by: &'a str,
invented_year: usize,
}
let languages = vec![
Language { name: "C", designed_by: "Dennis Ritchie", invented_year: 1972 },
Language { name: "Go", designed_by: "Rob Pike", invented_year: 2009 },
Language { name: "Rust", designed_by: "Graydon Hoare", invented_year: 2010 },
Language { name: "Hare", designed_by: "Drew DeVault", invented_year: 2022 },
];
let table = Table::new(languages);
assert_table!(
table,
"+------+----------------+---------------+"
"| name | designed_by | invented_year |"
"+------+----------------+---------------+"
"| C | Dennis Ritchie | 1972 |"
"+------+----------------+---------------+"
"| Go | Rob Pike | 2009 |"
"+------+----------------+---------------+"
"| Rust | Graydon Hoare | 2010 |"
"+------+----------------+---------------+"
"| Hare | Drew DeVault | 2022 |"
"+------+----------------+---------------+"
);
The same example but we are building a table step by step.
use tabled::{builder::Builder, settings::Style};
use testing_table::assert_table;
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 mut table = builder.build();
table.with(Style::ascii_rounded());
assert_table!(
table,
".------------------------------."
"| C | Dennis Ritchie | 1972 |"
"| Go | Rob Pike | 2009 |"
"| Rust | Graydon Hoare | 2010 |"
"| Hare | Drew DeVault | 2022 |"
"'------------------------------'"
);