It can't ve done portably IMO, as PS1
can contain escape sequences for color codes, (multiple) new lines too.
It can be calculated. But that's a really hard task. What if the user codes colorcodes in variable names (it's a common scenario), how to decide (during evaluation/counting the length) if that's something that the user wants to display or is only style information?