Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Feature] Handle ANSI escape codes from external libraries #91

Closed
blueforesticarus opened this issue Sep 26, 2022 · 3 comments · Fixed by #93
Closed

[Feature] Handle ANSI escape codes from external libraries #91

blueforesticarus opened this issue Sep 26, 2022 · 3 comments · Fixed by #93
Assignees
Labels
t: feature A new feature

Comments

@blueforesticarus
Copy link
Contributor

blueforesticarus commented Sep 26, 2022

Describe the bug

Column alignment breaks if printed text has formatting control codes.

Expected behavior

Text in a column contains parts formated with color/style using console crate. Alignment of later columns should be correct.

Code

use comfy_table::{Table, Row, Cell};
use console::Style;

fn main() {
    let mut table = Table::new();
    table.load_preset(comfy_table::presets::NOTHING);
    
    let mut row = Row::new();
    row.add_cell(Cell::new(
        "cell1"
    ));
    row.add_cell(Cell::new(
        "cell2"
    ));

    table.add_row(row);

    let mut row = Row::new();
    row.add_cell(Cell::new(
        format!("cell{}", console::style("3").bold().red())
    ));
    row.add_cell(Cell::new(
        "cell4"
    ));
    table.add_row(row);

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

image

Additional context

  • Operating System: nixos linux
  • comfy-table version: 6.1.0
@blueforesticarus blueforesticarus added the t: bug Something isn't working label Sep 26, 2022
@blueforesticarus blueforesticarus changed the title [BUG] [BUG] does not handle interior escape codes in width calculation Sep 26, 2022
@blueforesticarus
Copy link
Contributor Author

blueforesticarus commented Sep 26, 2022

I forked the repo and hacked together a fix for alignment using console::measure_text_width, though it likely still has a few issue.

Main problem I can find is with wrapping, wrapping the escape sequence in device causes the next column to be colored as well.
image

@blueforesticarus
Copy link
Contributor Author

I wrote a version of split_long_word that respects ansi escape sequences, but it probably breaks other behavior somewhere

9c01db6

image

@Nukesor
Copy link
Owner

Nukesor commented Sep 26, 2022

See #77

@Nukesor Nukesor changed the title [BUG] does not handle interior escape codes in width calculation [Request] Handle ANSI escape codes from external libraries Sep 26, 2022
@Nukesor Nukesor removed the t: bug Something isn't working label Sep 26, 2022
@Nukesor Nukesor changed the title [Request] Handle ANSI escape codes from external libraries [Feature] Handle ANSI escape codes from external libraries Jan 6, 2023
@Nukesor Nukesor added the t: feature A new feature label Jan 6, 2023
@Nukesor Nukesor assigned blueforesticarus and unassigned Nukesor Jan 6, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
t: feature A new feature
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants