termimad icon indicating copy to clipboard operation
termimad copied to clipboard

Support for `Colored`/terminal escape codes

Open ZedThree opened this issue 1 month ago • 1 comments

Thanks for the nifty package, it's very cool! I'd love to be able to use it with Colored, but using coloured text in a table breaks the layout:

use colored::Colorize;
use termimad::{MadSkin};

fn main() {
    let header = "|:-|:-\n|**Example**|**Table**|\n|-|-";
    let col1 = "Colourful text".bright_red();
    let col2 = "breaks the table :(".bold();
    let rest = format!("| {col1} | {col2} |");
    let table = format!("{header}\n{rest}\n|-");

    MadSkin::default().print_text(&table);
}
$ cargo run
┌───────────────────────┬───────────────────────────┐
│Example                │Table                      │
├───────────────────────┼───────────────────────────┤
│Colourful text│breaks the table :(│
└───────────────────────┴───────────────────────────┘
Image

My guess is it's counting escape codes when it shouldn't.

ZedThree avatar Nov 21 '25 17:11 ZedThree

My guess is it's counting escape codes when it shouldn't.

Termimad relies on unicode_width, which has no concept of TTY escape codes (just like Markdown).

Now, assuming we would want a special mode in which text would be assumed to contain TTY escape codes (and also assuming all TTY spans are INSIDE markdown spans), then we could, when computing visible sizes, first remove TTY escape sequences.

If I find some time, maybe I'll try it.

Canop avatar Nov 21 '25 17:11 Canop