Skip to content

Commit 90182ae

Browse files
committed
Add strip_ansi to terminal_colours.py
1 parent 7945abf commit 90182ae

File tree

1 file changed

+21
-2
lines changed

1 file changed

+21
-2
lines changed

domdf_python_tools/terminal_colours.py

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828
# Based on colorama
2929
# https://github.com/tartley/colorama
3030
# Copyright Jonathan Hartley 2013
31-
# Distrubuted under the BSD 3-Clause license.
31+
# Distributed under the BSD 3-Clause license.
3232
# | Redistribution and use in source and binary forms, with or without
3333
# | modification, are permitted provided that the following conditions are met:
3434
# |
@@ -56,10 +56,16 @@
5656
#
5757
# Includes modifications to colorama made by Bram Geelen in
5858
# https://github.com/tartley/colorama/pull/141/files
59+
#
60+
# _ansi_re and strip_ansi based on https://github.com/pallets/click
61+
# Copyright 2014 Pallets
62+
# Distributed under the BSD 3-Clause license.
63+
#
5964

6065
# stdlib
66+
import re
6167
from abc import ABC
62-
from typing import List
68+
from typing import List, Pattern
6369

6470
# 3rd party
6571
from colorama import init # type: ignore
@@ -112,6 +118,19 @@ def clear_line(mode: int = 2) -> str:
112118
return CSI + str(mode) + 'K'
113119

114120

121+
_ansi_re: Pattern[str] = re.compile(r"\033\[[;?0-9]*[a-zA-Z]")
122+
123+
124+
def strip_ansi(value: str) -> str:
125+
"""
126+
Strip ANSI colour codes from the given string to return a plaintext output.
127+
128+
:param value:
129+
"""
130+
131+
return _ansi_re.sub("", value)
132+
133+
115134
class Colour(str):
116135
r"""
117136
An ANSI escape sequence representing a colour.

0 commit comments

Comments
 (0)