Function text_grid::cells_schema

source ·
pub fn cells_schema<T: ?Sized>(
    fmt: impl Fn(&mut CellsFormatter<'_, '_, T>)
) -> impl CellsSchema<Source = T>
Expand description

Create CellsSchema from closure.

§Examples

By calculating the number of columns at runtime and creating a schema, it is possible to create tables where the number of columns cannot be obtained statically.

use text_grid::*;
let rows = [vec![1, 2, 3], vec![1, 2], vec![1, 2, 3, 4]];
let max_colunm_count = rows.iter().map(|r| r.len()).max().unwrap_or(0);
let schema = cells_schema::<Vec<u32>>(move |f| {
    for i in 0..max_colunm_count {
        f.column(i, |x| x.get(i));
    }
});
let g = to_grid_with_schema(rows, schema);
assert_eq!(format!("\n{g}"), OUTPUT);

const OUTPUT: &str = r"
 0 | 1 | 2 | 3 |
---|---|---|---|
 1 | 2 | 3 |   |
 1 | 2 |   |   |
 1 | 2 | 3 | 4 |
";