Trim a string which may contain ANSI control characters.
(data, length)
| 384 | |
| 385 | |
| 386 | def disp_trim(data, length): |
| 387 | """ |
| 388 | Trim a string which may contain ANSI control characters. |
| 389 | """ |
| 390 | if len(data) == disp_len(data): |
| 391 | return data[:length] |
| 392 | |
| 393 | ansi_present = bool(RE_ANSI.search(data)) |
| 394 | while disp_len(data) > length: # carefully delete one char at a time |
| 395 | data = data[:-1] |
| 396 | if ansi_present and bool(RE_ANSI.search(data)): |
| 397 | # assume ANSI reset is required |
| 398 | return data if data.endswith("\033[0m") else data + "\033[0m" |
| 399 | return data |
no test coverage detected