Calculate the extra amount of width space the given source code segment might take if it were to be displayed on a fixed width output device. Supports wide unicode characters and emojis.
(line, offset=None)
| 958 | _WIDE_CHAR_SPECIFIERS = "WF" |
| 959 | |
| 960 | def _display_width(line, offset=None): |
| 961 | """Calculate the extra amount of width space the given source |
| 962 | code segment might take if it were to be displayed on a fixed |
| 963 | width output device. Supports wide unicode characters and emojis.""" |
| 964 | |
| 965 | if offset is None: |
| 966 | offset = len(line) |
| 967 | |
| 968 | # Fast track for ASCII-only strings |
| 969 | if line.isascii(): |
| 970 | return offset |
| 971 | |
| 972 | import unicodedata |
| 973 | |
| 974 | return sum( |
| 975 | 2 if unicodedata.east_asian_width(char) in _WIDE_CHAR_SPECIFIERS else 1 |
| 976 | for char in line[:offset] |
| 977 | ) |
| 978 | |
| 979 | |
| 980 |
no test coverage detected