cli-grid

Crates.iocli-grid
lib.rscli-grid
version0.1.2
sourcesrc
created_at2020-11-14 14:40:33.624363
updated_at2020-11-14 14:53:39.234263
descriptionA small and dependency free crate for formatting the terminal outputs in a column based grid style.
homepage
repositoryhttps://github.com/mehdishojaei/cli-grid/
max_upload_size
id312348
size51,830
Mahdi Shojaee (mahdi-shojaee)

documentation

https://docs.rs/cli-grid/0.1.0/cli_grid/

README

A small and dependency free crate for formatting the terminal outputs in a column based grid style.

[---------------------]     [---------------------]     [---------------------]
[---------------------]     [---------------------]     [---------------------]
[---------------------]     [---------------------]     [---------------------]
<----------1---------->                            <-2->
1: Grid column
2: Grid padding

Each Cell of the grid can span into 1 or more columns.

[ Cell with colspan 1 ]     [---------------------]     [---------------------]
[-------------- Cell with colspan 2 --------------]     [---------------------]
[---------------------------- Cell with colspan 3 ----------------------------]

Horizontal alignments of cells are HAlign::Left, HAlign::Center, HAlign::Right and HAlign::Fill,

Vertical alignments of cells are VAlign::Top, VAlign::Middle and VAlign::Bottom.

Examples:

use cli_grid::*;
let grid = Grid::builder(vec![
    Row::new(vec![
        Cell::new("1".into(), 1),
        Cell::new("1".into(), 1),
        Cell::new("1".into(), 1),
    ]),
    Row::new(vec![
        Cell::new("2".into(), 2),
        Cell::new("1".into(), 1),
    ]),
    Row::new(vec![
        Cell::new("3".into(), 3),
    ]),
])
.default_blank_char('.')
.column_width(15)
.build();
let expected = format!(
    "{}\n{}\n{}\n",
    "1.............. 1.............. 1..............",
    "2.............................. 1..............",
    "3..............................................",
);
assert_eq!(grid.to_string(), expected);

Multi line Cells also is supported:

use cli_grid::*;
let grid = Grid::builder(vec![
    Row::new(vec![
        Cell::new("1".into(), 1),
        Cell::new("1\n1\n1".into(), 1),
        Cell::new("1".into(), 1),
    ]),
    Row::new(vec![
        Cell::new("2".into(), 2),
        Cell::new("1".into(), 1),
    ]),
    Row::new(vec![
        Cell::new("3".into(), 3),
    ]),
])
.default_blank_char('.')
.column_width(15)
.build();
let expected = format!(
    "{}\n{}\n{}\n{}\n{}\n",
    "1.............. 1.............. 1..............",
    "............... 1.............. ...............",
    "............... 1.............. ...............",
    "2.............................. 1..............",
    "3..............................................",
);
assert_eq!(grid.to_string(), expected);

So nested grids also is supported:

use cli_grid::*;
let nested_grid = Grid::builder(vec![
    Row::new(vec![
        Cell::new("1".into(), 1),
        Cell::new("1".into(), 1),
    ]),
    Row::new(vec![
        Cell::new("1".into(), 1),
        Cell::new("1".into(), 1),
    ]),
    Row::new(vec![
        Cell::new("1".into(), 1),
        Cell::new("1".into(), 1),
    ]),
])
.default_h_align(HAlign::Center)
.default_blank_char('-')
.column_width(5)
.build();
let grid = Grid::builder(vec![
    Row::new(vec![
        Cell::new("2".into(), 2),
        Cell::new("1".into(), 1),
    ]),
    Row::new(vec![
        Cell::new("1".into(), 1),
        Cell::new(nested_grid.to_string(), 1),
        Cell::new("1".into(), 1),
    ]),
    Row::new(vec![
        Cell::new("3".into(), 3),
    ]),
])
.default_h_align(HAlign::Center)
.default_v_align(VAlign::Middle)
.default_blank_char('.')
.column_width(15)
.build();
let expected = format!(
    "{}\n{}\n{}\n{}\n{}\n",
    "...............2............... .......1.......",
    "............... ..--1-- --1--.. ...............",
    ".......1....... ..--1-- --1--.. .......1.......",
    "............... ..--1-- --1--.. ...............",
    ".......................3.......................",
);
assert_eq!(grid.to_string(), expected);

Empty cells and rows can be created by Cell::new_empty and Row::new_empty methods.

Filled cells and rows can be created by Cell::new_fill and Row::new_fill methods.

Commit count: 6

cargo fmt