10 releases (3 stable)

1.2.0 Oct 27, 2019
1.1.0 Dec 23, 2018
1.0.0 Nov 18, 2018
0.1.6 Oct 9, 2018
0.1.3 May 15, 2018

#26 in Command-line interface

Download history 8/week @ 2019-12-14 2/week @ 2019-12-21 14/week @ 2019-12-28 3/week @ 2020-01-04 14/week @ 2020-01-11 53/week @ 2020-01-18 7/week @ 2020-01-25 3/week @ 2020-02-01 26/week @ 2020-02-08 19/week @ 2020-02-15 88/week @ 2020-02-22 23/week @ 2020-02-29 65/week @ 2020-03-07 23/week @ 2020-03-14 357/week @ 2020-03-21 495/week @ 2020-03-28

156 downloads per month
Used in 4 crates

MIT license

72KB
990 lines

term-table-rs

The purpose of term-table-rs is to make displaying table data in CLI apps easier

example

Here is an example of how to create a table

let mut table = Table::new();
table.max_column_width = 40;

table.style = TableStyle::extended(); 

table.add_row(Row::new(vec![
    TableCell::new_with_alignment("This is some centered text", 2, Alignment::Center)
])); 

table.add_row(Row::new(vec![
    TableCell::new("This is left aligned text", 1),
    TableCell::new_with_alignment("This is right aligned text", 1, Alignment::Right)
]));

table.add_row(Row::new(vec![
    TableCell::new("This is left aligned text", 1),
    TableCell::new_with_alignment("This is right aligned text", 1, Alignment::Right)
]));

table.add_row(Row::new(vec![
    TableCell::new("This is some really really really really really really really really really that is going to wrap to the next line", 2),
]));   

println!("{}", table.render());

Here's the result

extended style

Table Styles

It is possible to define your own table styles by creating a new instance of TableStyle

This is what the extend table style implementation looks like. This is the defualy style in term-table-rs

pub fn extended() -> TableStyle {
    return TableStyle {
        top_left_corner: '',
        top_right_corner: '',
        bottom_left_corner: '',
        bottom_right_corner: '',
        outer_left_vertical: '',
        outer_right_vertical: '',
        outer_bottom_horizontal: '',
        outer_top_horizontal: '',
        intersection: '',
        vertical: '',
        horizontal: '',
    };
}

TableStyle also implements a simple() table style function and a blank() table style function

Those styles looks like this

Blank

blank style

Simple

simple style

Column Widths

It is possible to control the maximum width of table columns. The max_column_width property of Table can be set to restrict the width of all TableCells. The set_max_column_width function of Table can be used to set the max width of a specific column. The set_max_column_widths function provides the ability to set the width of multiple columns by passing in a Vec of tuples containing an index and width.

Disabling Row Separators

There are a few different options for disabling row seperation.

Table has three flags for controlling row seperation:

  1. separate_rows dictates whether rows are seperated within the table

    separate_rows

  2. has_top_boarder dictates whether or not the table has a top border

    has_top_boarder

  3. has_bottom_boarder dicates whether or not the table has a bottom border

    has_bottom_boarder

Separators can also be contolled per row by setting the has_separator flag on Row

has_separator

Dependencies

~1.5MB
~41K SLoC