Skip to content

Instantly share code, notes, and snippets.

@dnbaker
Created November 11, 2021 17:53
Show Gist options
  • Save dnbaker/75da10314d639e37e0faa96bcabab448 to your computer and use it in GitHub Desktop.
Save dnbaker/75da10314d639e37e0faa96bcabab448 to your computer and use it in GitHub Desktop.
De-color text - especially useful for R tbl output.
def decolor(x):
from re import compile
return compile(r'''
\x1B # ESC
(?: # 7-bit C1 Fe (except CSI)
[@-Z\\-_]
| # or [ for CSI, followed by a control sequence
\[
[0-?]* # Parameter bytes
[ -/]* # Intermediate bytes
[@-~] # Final byte
)
''', re.VERBOSE).sub('', x)
__all__ = ["decolor"]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment