ratatui / ansi-to-tui

Convert ansi colored text to tui::text::Text
https://crates.io/crates/ansi-to-tui
MIT License
57 stars 21 forks source link

Properly reset styles #26

Closed dzfrias closed 1 year ago

dzfrias commented 1 year ago

There's a problem that prevents some specific styles from being reset with \x1b[0m. The problem is with Style::patch(...), but this PR should fix it.

Recreating

let contents = "\x1b[1mText\x1b[0m\x1b[36mText2\x1b[0m";
dbg!(contents.into_text().unwrap());

Expected

Text {
    lines: [
        Spans(
            [
                Span {
                    content: "Text",
                    style: Style {
                        fg: None,
                        bg: None,
                        add_modifier: BOLD,
                        sub_modifier: (empty),
                    },
                },
                Span {
                    content: "Text2",
                    style: Style {
                        fg: Some(
                            Cyan,
                        ),
                        bg: None,
                        add_modifier: (empty),
                        sub_modifier: (empty),
                    },
                },
            ],
        ),
    ],
}

Got

Text {
    lines: [
        Spans(
            [
                Span {
                    content: "Text",
                    style: Style {
                        fg: None,
                        bg: None,
                        add_modifier: BOLD,
                        sub_modifier: (empty),
                    },
                },
                Span {
                    content: "Text2",
                    style: Style {
                        fg: Some(
                            Cyan,
                        ),
                        bg: None,
                        add_modifier: BOLD,
                        sub_modifier: (empty),
                    },
                },
            ],
        ),
    ],
}
uttarayan21 commented 1 year ago

I rebased the last commit with this and will merge this pr. Thank you for the fixes :)