From 903a60abae601b79f5d3fc007634f300dce9f8b1 Mon Sep 17 00:00:00 2001 From: Zachary Cutlip Date: Fri, 29 Jan 2021 12:18:18 -0800 Subject: [PATCH] added uncolour() function to strip ansi color escapes --- voltron/colour.py | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/voltron/colour.py b/voltron/colour.py index bc7353e..55f07ee 100644 --- a/voltron/colour.py +++ b/voltron/colour.py @@ -42,3 +42,17 @@ def fmt_esc(name): return ESC_TEMPLATE.format(escapes()[name]) FMT_ESCAPES = dict((k, fmt_esc(k)) for k in ESCAPES) + + +def uncolour(text): + """ + Remove ANSI color/style sequences from a string. The set of all possible + ANSI sequences is large, so does not try to strip every possible one. But + does strip some outliers by other ANSI colorizers in the wild. Those + include `\x1b[K` (aka EL or erase to end of line) and `\x1b[m`, a terse + version of the more common `\x1b[0m`. + + Stolen from: https://github.com/jonathaneunice/colors/blob/master/colors/colors.py + """ + text = re.sub('\x1b\\[(K|.*?m)', '', text) + return text