Reputation: 55
A trivial implementation:
extern crate unicode_width;
fn main () {
let prompt = "\x1b[1;32m>>\x1b[0m ";
println!("{}", unicode_width::UnicodeWidthStr::width(prompt));
}
returns 12
but 3
is expected.
I would also be happy to use a crate that already does this, if there is one.
Upvotes: 1
Views: 334
Reputation: 54505
You're not going to get the width of an escape-sequence using a Unicode width calculation, simply because none of the string is printable—on a terminal.
If you control the content of the string, you could calculate the width by
\x1b
, allowing any combination of [
, ]
, <
, >
', =
, ?
, ;
or decimal digits through the "final" characters in the range @
to ~
In your example
let prompt = "\x1b[1;32m>>\x1b[0m ";
only ">> "
would be left to measure.
For patterns... you would start here: Regex
Further reading:
Upvotes: 1