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

ANSI escape breaks right to left Strings #97020

Closed
Araxeus opened this issue May 13, 2022 · 4 comments
Closed

ANSI escape breaks right to left Strings #97020

Araxeus opened this issue May 13, 2022 · 4 comments
Labels
C-bug Category: This is a bug.

Comments

@Araxeus
Copy link

Araxeus commented May 13, 2022

I tried this code:

fn main() {
    let input = String::from("שלום");

    // ok (no color)
    for ch in input.chars() {
        print!("{}", ch);
    } println!();

    // ok (same color)
    for ch in input.chars() {
        print!("{}", color(ch));
    } println!();

    // not ok (mixed colors)
    for (i, ch) in input.chars().into_iter().enumerate() {
        print!("{}", if i % 2 == 0 { color(ch) } else { color2(ch) });
    } println!();
}

fn color(ch: char) -> String {
    format!("\x1b[93m{}\x1b[0m", ch)
}

fn color2(ch: char) -> String {
    format!("\x1b[96m{}\x1b[0m", ch)
}

I expected to see this happen:

  • String order is preserved even if inside different ANSI escapes

Instead, this happened:

  • when ANSI codes are mixed, the right to left order is mixed up
    image

Meta

rustc --version --verbose:

rustc 1.60.0 (7737e0b5c 2022-04-04)
binary: rustc
commit-hash: 7737e0b5c4103216d6fd8cf941b7ab9bdbaace7c
commit-date: 2022-04-04
host: x86_64-pc-windows-msvc
release: 1.60.0
LLVM version: 14.0.0
@wwylele
Copy link
Contributor

wwylele commented May 13, 2022

I don't think this has anything to do with rust. Rust doesn't interpret the ANSI escape sequence. It simply puts the string to stdout as-is. It is the terminal that interprets these sequence. This issue should be opened to the terminal implementer (in this particular case for "x86_64-pc-windows-msvc", it is cmd or powershell or whichever terminal you are using)

@Araxeus
Copy link
Author

Araxeus commented May 13, 2022

You're right, after some more testing it happens from other sources too

Sadly both cmd and Powershell have this behavior (inside Windows Terminal)

@wwylele out of curiosity, if you try the code above in bash does it work correctly?
I imagine those letters just appear as � for you

@Araxeus Araxeus closed this as completed May 13, 2022
@ehuss
Copy link
Contributor

ehuss commented May 13, 2022

If you are interested, I would also recommend commenting on microsoft/terminal#538 about how ANSI codes interfere with the bidi algorithm.

@Araxeus
Copy link
Author

Araxeus commented May 13, 2022

Thanks, I just left a short comment there

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
C-bug Category: This is a bug.
Projects
None yet
Development

No branches or pull requests

3 participants