tabled
An easy to use library for pretty print tables of Rust struct
s and enum
s.
Agenda
Usage
To print a list of structs or enums as a table.
Implement Tabled
trait for your struct/enum with or annotate it with a #[derive(Tabled)]
macro. Then call a table
macro.
use ;
let languages = vec!;
let table = table!;
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!;
Derive information
To be able to use a Tabled
macro each field should implement std::fmt::Display
otherwise it will not work.
The following example will cause a error.
use Tabled;
;
Most of the default types implements the trait out of the box.
use table;
let some_numbers = ;
let table = table!;
Style
Styles
A list of ready to use styles. A shocases for the data in the Usage section.
Styles can be chosen by passing a Style
argument like this to table!
macro.
let table = table!;
Default
+------+----------------+---------------+
| 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 Style
You can modify existing styles to fits your needs.
table!
Alignment
You can set a alignemt for a Header, Column, Row or All Cells.
table!;
Format
Format function provides an interface for a modification of cells.
let table = table!;
Disable
You can remove a certain rows or column from the table.
table!;
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
.
The folowing change on the script in the usage and it's result
let table = table!;
Features
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.
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 = table!;
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