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

Weird indentations if externally-formatted strings are used #46

Closed
MoSal opened this issue Jan 23, 2017 · 4 comments
Closed

Weird indentations if externally-formatted strings are used #46

MoSal opened this issue Jan 23, 2017 · 4 comments

Comments

@MoSal
Copy link

MoSal commented Jan 23, 2017

#[macro_use] extern crate prettytable;
extern crate ansi_term;

use prettytable::Table;
use ansi_term::Colour::Yellow;

fn main() {
    // Okay
    let mut table = Table::new();
    table.add_row(row![ Fybl->"AAAAAAA", Fmbl->"BBB", Fgbl->"CCC"]);
    table.add_row(row![ Fybl->"AAAAAAA", Fmbl->"BBB", Fgbl->"CCC"]);
    table.add_row(row![ Fybl->"AAAAAAA", Fmbl->"BBB", Fgbl->"CCC"]);
    table.add_row(row![ Fybl->"Yellow Yellow Yellow" ]);
    table.printstd();

    // Messed up
    let mut table2 = Table::new();
    table2.add_row(row![ Fybl->"AAAAAAA", Fmbl->"BBB", Fgbl->"CCC"]);
    table2.add_row(row![ Fybl->"AAAAAAA", Fmbl->"BBB", Fgbl->"CCC"]);
    table2.add_row(row![ Fybl->"AAAAAAA", Fmbl->"BBB", Fgbl->"CCC"]);
    table2.add_row(row![ Yellow.bold().paint("Yellow Yellow Yellow") ]);
    table2.printstd();
}

In table2, the first column in the first 3 rows has extra paddings. Those paddings exist regardless of the alignment requested.

@phsym
Copy link
Owner

phsym commented Jan 24, 2017

Hi,
Unfortunately this is a known issue.
The reason is that the "Yellow Yellow Yellow" string contains ANSI escape sequences which are included when computing the strings length (to get the column width). But those sequences have no length once printed in a terminal.
The solution would be to remove those sequences from the string before computing the length, somewhere around there

@m4b
Copy link

m4b commented Jul 14, 2017

I'm seeing this too in a crate with mixed colored and prettytable, would be nice if this was adapted to, taken care of somehow when the string is inserted into the cell.

also this crate is great ❤️

@liautaud
Copy link
Contributor

liautaud commented May 18, 2018

Another way to fix this is to take ANSI escape sequences into account when computing string lengths (see #79).

@phsym
Copy link
Owner

phsym commented Aug 9, 2018

Resolved by #79 thanks to @liautaud

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants