1

Is it possible to get width of a string as it appears on the terminal?

Assuming the terminal supports ansi-escpace sequences, this

s = u"\u001b[31m" +"foo" + u"\u001b[0m"
print(len(s))
print(s)

will print

12
foo

with the second line in red. Given only the already concatenated s, is there an easy way to get the width of it (3 in this case)?

Also \t is counted as 1 character, but thats easier to account for.

463035818_is_not_a_number
  • 88,680
  • 9
  • 76
  • 150
  • This may be helpful: https://stackoverflow.com/a/22632060/1692260. or `string.printable` if ASCII only. – Aaron May 22 '20 at 14:40
  • 1
    It seems like you're effectively asking how to strip the ansi-escape sequences from a string, which has already been answered here: https://stackoverflow.com/q/14693701/984421. – ekhumoro May 22 '20 at 15:00
  • @ekhumoro thanks that looks very promising. Unfortunately I have to leave it for now. – 463035818_is_not_a_number May 22 '20 at 15:02

0 Answers0