tabled
An easy to use library for pretty printing tables of Rust structs and enums.
Agenda
Usage
To print a list of structs or enums as a table your types should implement the the Tabled trait or derive with a #[derive(Tabled)] macro.
use ;
let languages = vec!;
let table = new.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!;
Most of the default types implement the trait out of the box.
use TableIteratorExt;
let some_numbers = ;
let table = some_numbers.table;
Modificators
Style
There are a list of ready to use styles. But a custom style can be created as well.
A style can be used by passing it to .with method of Table.
let table = new.with;
Bellow rendered a list of pre configured styles.
If you think that there's some valuable style to be added, Please open an issue.
ASCII
+------+----------------+---------------+
| name | designed_by | invented_year |
+------+----------------+---------------+
| C | Dennis Ritchie | 1972 |
+------+----------------+---------------+
| Rust | Graydon Hoare | 2010 |
+------+----------------+---------------+
| Go | Rob Pike | 2009 |
+------+----------------+---------------+
Psql
name | designed_by | invented_year
------+----------------+---------------
C | Dennis Ritchie | 1972
Rust | Graydon Hoare | 2010
Go | Rob Pike | 2009
GithubMarkdown
| name | designed_by | invented_year |
|------+----------------+---------------|
| C | Dennis Ritchie | 1972 |
| Rust | Graydon Hoare | 2010 |
| Go | Rob Pike | 2009 |
Pseudo
┌──────┬────────────────┬───────────────┐
│ name │ designed_by │ invented_year │
├──────┼────────────────┼───────────────┤
│ C │ Dennis Ritchie │ 1972 │
├──────┼────────────────┼───────────────┤
│ Rust │ Graydon Hoare │ 2010 │
├──────┼────────────────┼───────────────┤
│ Go │ Rob Pike │ 2009 │
└──────┴────────────────┴───────────────┘
PseudoClean
┌──────┬────────────────┬───────────────┐
│ name │ designed_by │ invented_year │
├──────┼────────────────┼───────────────┤
│ C │ Dennis Ritchie │ 1972 │
│ Rust │ Graydon Hoare │ 2010 │
│ Go │ Rob Pike │ 2009 │
└──────┴────────────────┴───────────────┘
NoBorder
name designed_by invented_year
C Dennis Ritchie 1972
Rust Graydon Hoare 2010
Go Rob Pike 2009
Custom
You can modify existing styles to fits your needs.
let style = NO_BORDER
.frame_bottom
.split
.inner;
let table = new.with;
Alignment
You can set a horizontal and vertical alignment for a Header, Column, Row or Full set of cells.
new
.with;
Format
The Format function provides an interface for a modification of cells.
new
.with,
.with
.with;
It's also possible to use functions with signature Fn(&str) -> String as a formatter.
new
.with,
.with
.with;
IMPORTANT: you may need to specify type in your lambda otherwise compiler may be disagreed to work :)
There's 2 more Format modifiers. You can find more imformation about theire usage in the documentation.
FormatFrom- UsesVecelements as new content.FormatWithIndex- LikeFormatbut withrowandcolumnindex in lambda.
Indent
The Indent type provides an interface for a left, right, top and bottom indent of cells.
new.with;
Max width
Using MaxWidth type its possible to set a max width of an object.
While tinkering content we don't forget about its color.
// You can truncate it everything after 10 chars.
new.with;
// And you can wrap it content reaching 10 chars.
new.with;
Rotate
You can rotate table using Rotate.
Imagine you have a table already. And the output may look like this.
┌────┬──────────────┬───────────────────────────┐
│ id │ destribution │ link │
├────┼──────────────┼───────────────────────────┤
│ 0 │ Fedora │ https://getfedora.org/ │
├────┼──────────────┼───────────────────────────┤
│ 2 │ OpenSUSE │ https://www.opensuse.org/ │
├────┼──────────────┼───────────────────────────┤
│ 3 │ Endeavouros │ https://endeavouros.com/ │
└────┴──────────────┴───────────────────────────┘
Now we will add table.with(Rotate::Left) and the output will be;
┌──────────────┬────────────────────────┬───────────────────────────┬──────────────────────────┐
│ link │ https://getfedora.org/ │ https://www.opensuse.org/ │ https://endeavouros.com/ │
├──────────────┼────────────────────────┼───────────────────────────┼──────────────────────────┤
│ destribution │ Fedora │ OpenSUSE │ Endeavouros │
├──────────────┼────────────────────────┼───────────────────────────┼──────────────────────────┤
│ id │ 0 │ 2 │ 3 │
└──────────────┴────────────────────────┴───────────────────────────┴──────────────────────────┘
Disable
You can remove certain rows or columns from the table.
new
.with
.with;
Header and Footer
You can add a Header and Footer to display some information.
By the way you can even add such line by using Panel
new
.with
.with
A look will differ from a style you choose. But it's how it may look like.
┌────────────────────────────────────────────────────────────┐
│ Tabled Name │
├────────────────────────────────────────────────────────────┤
...
├───────┼──────────────┼─────────┼───────────────────────────┤
│ 3 elements │
└────────────────────────────────────────────────────────────┘
Concat
You can concatanate 2 tables using Concat.
It will stick 2 tables together either vertically or horizontally.
let t1: Table = ...;
let t2: Table = ...;
let t3: Table = t1.with;
Derive
To be able to use a Tabled macros each field must implement std::fmt::Display
otherwise it will not work.
The following example will cause a error.
use Tabled;
;
Column name override
You can use a #[header("")] attribute to override a column name.
Hide a column
You can mark filds as hidden in which case they fill be ignored and not be present on a sheet.
A similar affect could be achived by the means of a Disable setting.
Custom field formatting
#[derive(Tabled)] is possible only when all fields implement a Display trait.
However, this may be often not the case for example when a field uses the Option type.
There's 2 common ways how to solve this:
- Implement
Tabledtrait manually for a type. - Wrap
Optionto something likeDisplayedOption<T>(Option<T>)and implement a Display trait for it.
Or to use an attribute #[field(display_with = "func")] for the field. To use it you must provide a function name in a display_with parameter.
Inline
It's possible to inline internal data if it implements Tabled trait.
Use #[header(inline)] or #[header(inline("prefix>>"))].
The string argument is a prefix which will be used for all inlined elements.
And it works for enums as well.
Features
Color
The library doesn't bind you in usage of any color library but to be able to work corectly with color input you should provide a --features color.
new
.with
.with
.with
.with;

Tuple combination
You also can combine objets which implements Tabled by means of tuples, you will get a combined columns of them.
use ;
] &'static str);
let data = vec!;
let table = new.with.to_string;
assert_eq!;
Object
You can peak your target for settings using and and not methods for an object.
Full.not // peak all cells except header
Head.and.not // peak a header and first column except a (0, 0) cell
Views
Tabled supports not only Table view!
Expanded display
You can use ExpanedDisplay if your data structure has a lot of fields.
Here's an example.
use ;
You'll see the following.
-[ RECORD 0 ]------
name | Manjaro
is_active | true
is_cool | true
-[ RECORD 1 ]------
name | Debian
is_active | true
is_cool | true
-[ RECORD 2 ]------
name | Debian
is_active | true
is_cool | true
Notes
ANSI escape codes
By default tabled doesn't handle ANSI escape codes.
By default such things as hyperlinks, blinking and others things which can be achived via ANSI codes might not work correctly.
tabled support it by setting a color feature.
= { = "*", = ["color"] }
Emoji
The library support emojies out of the box but be aware that some of the terminals and editors may not render them as you would expect.
Let's add emojies to an example from a Usage section.
let languages = vec!;
The resultant table will look like the following.
As you can see Github triks a bit a return table, but GNOME terminal and Alacritty terminal handles it correctly.
+---------+----------------+---------------+
| name | designed_by | invented_year |
+---------+----------------+---------------+
| C 💕 | Dennis Ritchie | 1972 |
+---------+----------------+---------------+
| Rust 👍 | Graydon Hoare | 2010 |
+---------+----------------+---------------+
| Go 🧋 | Rob Pike | 2009 |
+---------+----------------+---------------+