MCPcopy Create free account
hub / github.com/PierreGode/Ragnar / disp_trim

Function disp_trim

pager_lib/tqdm/utils.py:386–399  ·  view source on GitHub ↗

Trim a string which may contain ANSI control characters.

(data, length)

Source from the content-addressed store, hash-verified

384
385
386def 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

Callers 1

format_meterMethod · 0.85

Calls 1

disp_lenFunction · 0.85

Tested by

no test coverage detected