colored-rs/colored

Control characters scewing padding.

Dygear opened this issue · 3 comments

This might not be your problem at all. I'm almost sure that is actually mine, but if that's the case, it would be nice to include some documentation on how to fix this. When formatting a string to pad right println!("{:>33}", "name".red()) extra padding must be applied to account for the control characters in the stream. Is there a way to tell the println!, format! and write! family of macros to not take into account control characters when calculating string length for output?

I ask because I'm building an interface that may apply multiple colors (one background, one foreground) to an output line and I'd love the text that it's outputting aligned correctly. The application that I'm making will make heavy use of colors to convey messages and meaning to the viewer. It take information from the Formula 1 '22 game over a UDP socket displays a timing and scoring screen like that found on the pitwall. As such, I've called it Pitwall. Displaying information like flag status for that driver (In a yellow flag, getting a blue flag) would be displayed by changing the background color of their name on the output making it exceedingly obvious when something has happened. I will also make use of it extensively for the timing component where when a fastest sector time or lap time is set that time will "go purple."

It is something we could look into, but it is by no means a trivial implementation.

If you want to progress with your project, you could possibly manually handle the padding with two print calls, one which just prints whitespace.

You can use the strip-ansi-escapes crate to remove such escape characters, and then you can measure the length of the resulting string.

@9999years Seems like a fair way to handle it.