This crate formats data as a text table, suitable for printing to the terminal or for inclusion in logs.
The following code prints a two-column table from a vector of strings:
``` let table = table!( "{B^:12:}" => "Food", "{G^:7:}" => "Count"; "Fish", "15", "Pizza", "10", "Tomato", "24" );
let output = table.format(); ```
Output:
(color codes not shown)
+--------------------+
| Food | Count |
+--------------------+
|Fish |15 |
+--------------------+
|Pizza |10 |
+--------------------+
|Tomato |24 |
+--------------------+
The contents of a table cell can be styled using either a style directive or a manually built ContentStyle struct. A content style consists of the following elements:
A short-hand for specifying content styles is to use a style directive. These strings are formatted as follows:
{alignment[color]|width|wrap}
Alignment is specified using one of: < ^ > * < (default) left aligned * ^ center aligned * > right aligned
Color is specified inside square brackets [] and consists of:
Lower-case color codes indicating regular (dark) colors. Upper-case codes indicate bright colors.
Example color codes:
[c] - Cyan on black (foreground color specified only)
[rG] - Dark red on bright breen
[Wb] - Bright white on dark blue
[-g] - White on dark green (background color specified only)
The width of a cell is used when describing headers. The cells within the table header row describe the width of those headers and are used to determine column breaks for the table, and where other content should be wrapped or truncated.
Width is specified between pipes ||.
There are three width rules that can be specified:
Example width specifiers:
|f15| - Fixed width of 15 chars
|m10| - Minimum width of 10 chars
|c| - (default) Sized according to content
When content is too big to fit into a cell it will either be wrapped to multiple lines or truncated. The wrap mode specifier specifies whether to wrap or truncate. This specifier is a single character at the end of the style directive.
Wrapping is indicated by including a semi-colon ; at the end of the directive. Truncation (default) requires no specifier.
Example of a style directive that will wrap content:
{<c;} - Left-aligned, cyan, wrapped
All of the four parts of the style directive are optional. The empty style directive {} simply means "left aligned, white on black, content width, truncate". It is not necessary to include an empty style directive.
Examples of partial directives:
{>;} - Right-aligned, white on black, content width, wrapped
{[-g]} - Left-aligned, white on dark green, content width, truncated
{;} - Left-aligned, white on black, content width, wrapped
{|f15|} - Left-aligned, white on black, fixed 15 char width, truncated
Example 1: Left-aligned, cyan on black, fixed 15-char width, truncate:
{<[c]|f15|}
Example 2: Center-aligned, bright yellow on dark green background, content
width, wrap content:
{^[Yg]|c|;}
Note: The |c| above is optional and can be omitted.