15 releases (8 stable)

1.4.0 Jul 14, 2024
1.3.2 Sep 27, 2021
1.3.1 Feb 3, 2021
1.3.0 Apr 29, 2020
0.1.5 Jul 14, 2018

#60 in Command-line interface

Download history 4247/week @ 2024-04-01 3728/week @ 2024-04-08 4601/week @ 2024-04-15 4557/week @ 2024-04-22 4026/week @ 2024-04-29 3816/week @ 2024-05-06 4354/week @ 2024-05-13 3865/week @ 2024-05-20 4403/week @ 2024-05-27 3740/week @ 2024-06-03 5235/week @ 2024-06-10 5146/week @ 2024-06-17 5180/week @ 2024-06-24 4670/week @ 2024-07-01 5646/week @ 2024-07-08 5726/week @ 2024-07-15

21,452 downloads per month
Used in 45 crates (38 directly)

MIT license

84KB
1.5K SLoC

term-table

CLI Tables Made Easy

Example

use rand::Rng;
use term_table::{row, row::Row, rows, table_cell::*, Table, TableStyle};

fn main() {
    let mut rng = rand::thread_rng();
    let num_draws = 5;
    let num_numbers = 6;
    let range = 1..=99;

    let mut table = Table::builder()
        .rows(rows![row!(TableCell::builder("My Lucky Numbers")
            .alignment(Alignment::Center)
            .col_span(num_numbers))])
        .style(TableStyle::elegant())
        .build();

    for _ in 0..num_draws {
        let mut row = Row::empty();
        for _ in 0..num_numbers {
            let num: i32 = rng.gen_range(range.clone());
            row.add_cell(TableCell::new(num.to_string()));
        }
        table.add_row(row);
    }

    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 default 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 separation.

Table has three flags for controlling row separation:

  1. separate_rows dictates whether rows are separated 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 dictates whether or not the table has a bottom border

    has_bottom_boarder

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

has_separator

Dependencies

~2.4–3.5MB
~54K SLoC