Open fdncred opened 6 months ago
Hi @fdncred
I think it's a good proposition.
If my memory serves me well; it was discussed a long time ago, in regard of nushell
.
At least I was thinking of adding this method into ansi-str
, but to be honest I don't remember the outcome :smile:
I've just checked that it could be achieved via existing structure.
pub fn main() {
cmp_text("\u{1b}[1m\u{1b}[31;46m\u{1b}[1m\u{1b}[31;46m\u{1b}[1m\u{1b}[31;46m\u{1b}[1m\u{1b}[31;46mWhen the night has come\u{1b}[0m\u{1b}[0m\u{1b}[0m\u{1b}[0m\u{1b}[0m\u{1b}[0m\u{1b}[0m\u{1b}[0m");
}
fn cmp_text(text: &str) {
use std::fmt::Write;
let text1 = ansi_str::get_blocks(text).fold(String::new(), |mut acc, b| {
let _ = write!(
&mut acc,
"{}{}{}",
b.style().start(),
b.text(),
b.style().end()
);
acc
});
println!("ORIGIN => {text}");
println!("ORIGIN => {text:?}");
println!("CONVERTED => {text1}");
println!("CONVERTED => {text1:?}");
println!("RESULT => equal={} origin-len={} converted-len={}", text == text1, text.len(), text1.len());
}
Which results in
origin => "\u{1b}[1m\u{1b}[31;46m\u{1b}[1m\u{1b}[31;46m\u{1b}[1m\u{1b}[31;46m\u{1b}[1m\u{1b}[31;46mWhen the night has come\u{1b}[0m\u{1b}[0m\u{1b}[0m\u{1b}[0m\u{1b}[0m\u{1b}[0m\u{1b}[0m\u{1b}[0m"
result => "\u{1b}[1m\u{1b}[31m\u{1b}[46mWhen the night has come\u{1b}[22m\u{1b}[39m\u{1b}[49m"
origin-len=103 result-len=52
TLDR;
Seems like it already possible, the only thing is that probably we shall add a flag to squash continues ANSI sequences together (for prefix and suffix). So we would get smth. like for my example, with a length of 44 instead of 52.
"\u{1b}[1;31;46mWhen the night has come\u{1b}[22;39;49m"
Note: For your data; the code above produces even more lengthy string exactly cause of extensive \e
production.
PS: Yesss I haven't forgot about emojie issue; sorry took longer than shall to
So yes I'll take a look at it
Thanks @zhiburt. This is about nushell too. I've thought about adding such a functionality to nushell before it prints things out or perhaps a plugin. Or maybe a separate command like ansi optimize
too? So, I have some ideas of where to use it.
I've been looking around for something to optimize ansi escape sequences and I thought of your ansi crates and tabled.
I can't remember if we've talked about this before but It would be nice to have something that receives a string with ansi escapes and optimizes that string to use as few ansi escape characters as possible while maintaining the same output.
Here's a simple example in nushell.
This will print
foo
in green bold text.However, it's not optimized. One of these below would require less characters.
It's not that big of a deal when it's just green bold but looking at my prompt string, you can see that it's pretty optimized but it's big. More could be done to optimize this.
This is the image of the prompt.
If I counted correctly, there are 734 characters counting \e as 1 since it represents the escape character. There are 11 ansi resets \e[0m which I think could probably be reduced to 3 or 4. So, it could take this string to somewhere around 700 chars. Every little bit helps but if you're constructing this prompt by hand, you could easily have 2 or 3 times more characters due to lack of optimization.
So, I was wondering if in your ansi work, you've implemented or found such a crate to optimize ansi escape sequences?