An easy to use library for pretty print tables of Rust structs and enums.
An easy to use library for pretty print tables of Rust structs and enums.
An easy to use library for pretty printing tables of Rust structs and enums.
You can do a lot of things with the library.
If it does not do something which you feel it should or it is not clear how to, please file an issue.
This README contains lots of information but it might still be not complete,
you can find more examples in the examples folder.
To print a list of structs or enums as a table, there are 2 ways.
Tabled trait for your type.A builder pattern gets handy when a data schema is unknown.
While a typed struct is useful in cases where we know the data structure beforehand.
…The next example illustrates a builder pattern.
use tabled::{builder::Builder, settings::Style};
use tabled::assert::assert_table;
let mut b = Builder::with_capacity(3, 0);
b.push_record(["row=0 col=0", "row=0 col=1"]);
b.push_column(["row=0 col=2", "row=1 col=2"]);
let mut table = b.build();
table.with(Style::modern());
assert_table!(
table,
"┌─────────────┬─────────────┬─────────────┐"
"│ row=0 col=0 │ row=0 col=1 │ row=0 col=2 │"
"├─────────────┼─────────────┼─────────────┤"
"│ │ │ row=1 col=2 │"
"└─────────────┴─────────────┴─────────────┘"
);NOTICE: In case you got to perform table rendering fast with no footprint you might be worth looking at
IterTableandCompactTable.
There are a lot of functions available for your tables, as well as helpers such as derive macros and proc macros.
This section lists the settings you can apply to your table.
All of the settings are leveraged by Table::with and Table::modify.
But you can tweak things yourself by using table.get_records_mut() and table.get_config_mut().
or by creating a new setting, which is a simple do.
Style sole purpouse is to configure table look.
Style primarily usage is in const/static context.
If you want to peak style at runtime Theme might be better suited for it.
Any Style can be customized.
As well as a custom Style can be created from scratch.
use tabled::{Table, Style};
let mut table = Table::new(&data);
table.with(Style::psql());There are numerous preconfigured styles. Below there is a rendered list of them.
If you think that there's some valuable style to be added, please open an issue.
+------+----------------+---------------+
| name | designed_by | invented_year |
+------+----------------+---------------+
| C | Dennis Ritchie | 1972 |
+------+----------------+---------------+
| Rust | Graydon Hoare | 2010 |
+------+----------------+---------------+
| Go | Rob Pike | 2009 |
+------+----------------+---------------+┌──────┬────────────────┬───────────────┐
│ name │ designed_by │ invented_year │
├──────┼────────────────┼───────────────┤
│ C │ Dennis Ritchie │ 1972 │
├──────┼────────────────┼───────────────┤
│ Rust │ Graydon Hoare │ 2010 │
├──────┼────────────────┼───────────────┤
│ Go │ Rob Pike │ 2009 │
└──────┴────────────────┴───────────────┘┌──────┬────────────────┬───────────────┐
│ name │ designed_by │ invented_year │
├──────┼────────────────┼───────────────┤
│ C │ Dennis Ritchie │ 1972 │
│ Rust │ Graydon Hoare │ 2010 │
│ Go │ Rob Pike │ 2009 │
└──────┴────────────────┴───────────────┘╭──────┬────────────────┬───────────────╮
│ name │ designed_by │ invented_year │
├──────┼────────────────┼───────────────┤
│ C │ Dennis Ritchie │ 1972 │
│ Rust │ Graydon Hoare │ 2010 │
│ Go │ Rob Pike │ 2009 │
╰──────┴────────────────┴───────────────╯╔══════╦════════════════╦═══════════════╗
║ name ║ designed_by ║ invented_year ║
╠══════╬════════════════╬═══════════════╣
║ C ║ Dennis Ritchie ║ 1972 ║
╠══════╬════════════════╬═══════════════╣
║ Rust ║ Graydon Hoare ║ 2010 ║
╠══════╬════════════════╬═══════════════╣
║ Go ║ Rob Pike ║ 2009 ║
╚══════╩════════════════╩═══════════════╝ name | designed_by | invented_year
------+----------------+---------------
C | Dennis Ritchie | 1972
Rust | Graydon Hoare | 2010
Go | Rob Pike | 2009 | name | designed_by | invented_year |
|------|----------------|---------------|
| C | Dennis Ritchie | 1972 |
| Rust | Graydon Hoare | 2010 |
| Go | Rob Pike | 2009 |====== ================ ===============
name designed_by invented_year
====== ================ ===============
C Dennis Ritchie 1972
Rust Graydon Hoare 2010
Go Rob Pike 2009
====== ================ ===============.........................................
: name : designed_by : invented_year :
:......:................:...............:
: C : Dennis Ritchie : 1972 :
: Rust : Graydon Hoare : 2010 :
: Go : Rob Pike : 2009 :
:......:................:...............:.---------------------------------------.
| name | designed_by | invented_year |
| C | Dennis Ritchie | 1972 |
| Rust | Graydon Hoare | 2010 |
| Go | Rob Pike | 2009 |
'---------------------------------------' name designed_by invented_year
C Dennis Ritchie 1972
Rust Graydon Hoare 2010
Go Rob Pike 2009 name designed_by invented_year
C Dennis Ritchie 1972
Rust Graydon Hoare 2010
Go Rob Pike 2009 You can modify existing styles to fit your needs. Mind that all modifications are done at compile time.
Check the documentation for more customization options.
If you can't make desicions at compile time - use Theme.
An example for castomization, where we remove all vertical and horizontal lines but adding single line.
use tabled::settings::{Style, HorizontalLine, VerticalLine};
let style = Style::modern()
.horizontals([(1, HorizontalLine::inherit(Style::modern()).horizontal('═'))])
.verticals([(1, VerticalLine::inherit(Style::modern()))])
.remove_horizontal()
.remove_vertical();This style will look like the next table:
┌──────┬───────────────────────────────┐
│ name │ designed_by invented_year │
├══════┼═══════════════════════════════┤
│ C │ Dennis Ritchie 1972 │
│ Go │ Rob Pike 2009 │
│ Rust │ Graydon Hoare 2010 │
└──────┴───────────────────────────────┘As was said doing customization at constant context is not always a best choise,
you may need to change a style at runtime, you may use Theme object to do that.
Theme is quite powerful by itself,
you can check it in the documentation.
use tabled::grid::config::{Border, HorizontalLine};
use tabled::settings::Theme;
let mut style = Theme::default();
style.insert_horizontal_line(1, HorizontalLine::full('-', '-', '+', '+'));
style.set_frame(Border::filled('+'));This style will look like the following:
+++++++++++++++++++++++++++++++++++++++
+ name designed_by invented_year +
+-------------------------------------+
+ C Dennis Ritchie 1972 +
+ Go Rob Pike 2009 +
+ Rust Graydon Hoare 2010 +
+++++++++++++++++++++++++++++++++++++++Sometimes tabled::Style settings are not enough.
Sometimes it's necessary to change a border of a particular cell.
For this purpose you can use Border.
use tabled::{
settings::{object::Rows, Border, Style},
assert::assert_table,
Table,
};
let data = [["123", "456"], ["789", "000"]];
let mut table = Table::new(data);
table.modify(
Rows::first(),
Border::inherit(Style::ascii())
.top('=')
.corner_top_left('=')
.corner_top_right('='),
);
assert_table!(
table,
"============="
"| 0 | 1 |"
"+-----+-----+"
"| 123 | 456 |"
"+-----+-----+"
"| 789 | 000 |"
"+-----+-----+"
);You can wri
No open issues yet, or sync has not completed.