zhiburt / tabled

An easy to use library for pretty print tables of Rust structs and enums.
MIT License
1.94k stars 77 forks source link

Using color for part of the text in a cell causes formatting issue #401

Closed fbonin closed 4 months ago

fbonin commented 4 months ago

Using color for part of the text in a cell causes formatting issue. The issue also happens with libraries like owo-colors or colored

To reproduce

fn main() {
    use tabled::{Table, Tabled};

    #[derive(Tabled)]
    struct Language {
        name: String,
        designed_by: String,
        invented_year: usize,
    }

    let languages = vec![
        Language{
            name: "Rust".to_string(),
            designed_by: format!("{} {}", "Graydon", "\x1b[31mHoare\x1b[0m"),
            invented_year: 2010
        },
    ];

    let table = Table::new(languages).to_string();

    println!("{}",table);
}

Bug

+------+----------------------+---------------+
| name | designed_by          | invented_year |
+------+----------------------+---------------+
| Rust | Graydon Hoare | 2010          |
+------+----------------------+---------------+
image

Expected

+------+----------------------+---------------+
| name | designed_by          | invented_year |
+------+----------------------+---------------+
| Rust | Graydon Hoare        | 2010          |
+------+----------------------+---------------+
zhiburt commented 4 months ago

Hi @fbonin

I do bet there's no actual issue. I think you have just forgotten to use ansi feature.

[dependencies]
tabled = { version = "0.15", features = ["ansi"] }

See

image

Let me know if it solves the problem

fbonin commented 4 months ago

I confirm that it fixes my issue.

Thank you very much !

You definitely win the bet 😀