Determine how many columns are needed to display a string in a terminal. Returns -1 if the string contains non-printable characters.
(s: str)
| 42 | |
| 43 | |
| 44 | def wcswidth(s: str) -> int: |
| 45 | """Determine how many columns are needed to display a string in a terminal. |
| 46 | |
| 47 | Returns -1 if the string contains non-printable characters. |
| 48 | """ |
| 49 | width = 0 |
| 50 | for c in unicodedata.normalize("NFC", s): |
| 51 | wc = wcwidth(c) |
| 52 | if wc < 0: |
| 53 | return -1 |
| 54 | width += wc |
| 55 | return width |