prettytable-rs icon indicating copy to clipboard operation
prettytable-rs copied to clipboard

Apply dynamic styles to nested table

Open samiraguiar opened this issue 1 year ago • 4 comments

Is it possible to have dynamic styles (e.g. different color based on the contents) for nested tables? I couldn't get it to work and it doesn't seem to be possible because the table is print to a string without styles when nested.

samiraguiar avatar Mar 10 '23 22:03 samiraguiar

Could we sketch some examples of the desired minimal / ideal support(s) ?

pinkforest avatar Mar 10 '23 23:03 pinkforest

Ideally it would be something like this:

pub fn bool_icon(v: bool) -> Cell {
    if v {
        cell![Fgb -> "✔"]
    } else {
        cell![Frb -> "✘"]
    }
}

...
subtable.add_row(row![ "Enabled?", bool_icon(d.can_connect) ]);
table.add_row(row![ "Details", subtable ]);
...

Or without macros if not possible. When it's not inside a subtable I can workaround it by not using macros:

table.add_row(Row::new(vec![
    Cell::new("Enabled?"),
    bool_icon(d.can_connect),
]));

But with a subtable this doesn't work because it is converted to a string without styles first.

samiraguiar avatar Mar 11 '23 18:03 samiraguiar

Would you like to try a PR ?

pinkforest avatar Mar 11 '23 18:03 pinkforest

Sure, I'd love to. I can't make promises on a time frame but I'll take a look when possible

samiraguiar avatar Mar 16 '23 19:03 samiraguiar