Skip to content

ANSI escape breaks right to left Strings #97020

@Araxeus

Description

@Araxeus

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    C-bugCategory: This is a bug.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions