zhiburt / ansi-str

This is a library for work with colored and formatted strings on ANSI terminals.
MIT License
2 stars 0 forks source link

Clarification regarding the note about style preserving #4

Closed vikigenius closed 2 years ago

vikigenius commented 2 years ago

Hi I just came across your library and it seems very useful for my usecase where I want to implement things such as word wrapping etc.

I was going through the docs but I can't understand your notes about styles

You provide the following example:

let hello1 = "Hello World!".red();
let hello2 = hello.ansi_get(..).unwrap();
assert_eq!(hello1, hello2)

Why is this not guaranteed to be true?

zhiburt commented 2 years ago

Hi @vikigenius

Well in this specific example I believe it must be true. (now at least).

I meant to say that the ANSI sequences used after all might be not what you'd expect. See usage of split function instead.

    let text = "\u{1b}[31mHello World!\u{1b}[0m";
    let words = text.ansi_split(" ").collect::<Vec<_>>();

    println!("{:?}", words);

You'd see ["\u{1b}[31mHello\u{1b}[39m", "\u{1b}[31mWorld!\u{1b}[0m"]. Notice that 1st element uses 39m to close the color instead of 0m.

vikigenius commented 2 years ago

@zhiburt Thanks for the explanation. I will try to look at the code to understand why this is happening. But do you have any pointers/brief explanation?

Why does this happen? Where is the 39m coming from. It's supposed to be Grey right? 0m is just supposed to reset the color. What's wrong with using 0m to close both strings?

zhiburt commented 2 years ago

0m is just supposed to reset the color.

right

What's wrong with using 0m to close both strings?

Essentially nothing is wrong. But to do so would require a "look ahead" (which would make the operations a bit slower).

Personally I take current approach as a better one.

Why does this happen? Where is the 39m coming from. It's supposed to be Grey right? 0m is just supposed to reset the color. What's wrong with using 0m to close both strings?

39m ends only foreground colors, whereas 0m closes all ANSI sequences. So in this specific example they can be used interchangeably in a sense of how it will be displayed.

If you consider that the question is addressed, you can close the issue.