#text #table #formatter

table_formatter

A simple text table formatter written in Rust

6 releases (3 breaking)

new 0.4.0 Jun 3, 2023
0.3.2 May 27, 2023
0.2.0 May 20, 2023
0.1.0 May 13, 2023

#50 in Value formatting

Download history 30/week @ 2023-05-12 46/week @ 2023-05-19 67/week @ 2023-05-26

143 downloads per month

MIT/Apache

31KB
778 lines

Tabel Formatter

This lib is used to format plain-text table.

Example

Code:

let table_header: Vec<TableCell> = vec![
    TableCell::new(Cell::TextCell("Cell Row".into())).with_width(20),
    TableCell::new(Cell::TextCell("Left".into()))
        .with_position(CellPosition::Left)
        .with_width(10),
    TableCell::new(Cell::TextCell("Middle".into()))
        .with_position(CellPosition::Middle)
        .with_width(10),
    TableCell::new(Cell::TextCell("Right".into()))
        .with_position(CellPosition::Right)
        .with_width(10),
];
let table_cells: Vec<Vec<TableCell>> = (0..=3_u8)
    .into_iter()
    .map(|r| {
        vec![
            TableCell::new(Cell::TextCell(format!("Cell Row: {}", r))),
            TableCell::new(Cell::TextCell("Left".into())).with_position(CellPosition::Left),
            TableCell::new(Cell::TextCell("Middle".into())).with_position(CellPosition::Middle),
            TableCell::new(Cell::TextCell("Right".into())).with_position(CellPosition::Right),
        ]
    })
    .collect_vec();
let table = Table::from_data(table_header, table_cells);
let render_res = table.render();
println!("{}", render_res);

Output:

────────────────────────────────────────────────────────────
| Cell Row              Left          Middle         Right |
|──────────────────────────────────────────────────────────|
| Cell Row: 0           Left          Middle         Right |
| Cell Row: 1           Left          Middle         Right |
| Cell Row: 2           Left          Middle         Right |
| Cell Row: 3           Left          Middle         Right |
────────────────────────────────────────────────────────────

Actually the head part of the table is bold, but it cannot be rendered in markdown.

Future Plan

Write a macro for a better experience when building the table.

Support cross-cell text.

License

MIT or Apache-2.0

Dependencies

~0.8–1.4MB
~31K SLoC