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