Skip to content

Commit e2a58bd

Browse files
committed
Add strip_irc and strip_all to colors.py
1 parent 7950e52 commit e2a58bd

4 files changed

Lines changed: 40 additions & 14 deletions

File tree

cloudbot/util/colors.py

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,7 @@
7878

7979

8080
COLOR_RE = re.compile(r"\$\(.*?\)", re.I)
81+
IRC_COLOR_RE = re.compile(r"(\x03(\d+,\d+|\d)|[\x0f\x02\x16\x1f])", re.I)
8182

8283

8384
def get_color(colour, return_formatted=True):
@@ -154,8 +155,13 @@ def parse(string):
154155
return formatted
155156

156157

158+
# Color stripping.
159+
157160
def strip(string):
158-
"""strip: Similiar to parse, only this removes colour codes"""
161+
"""
162+
Removes all $() syntax color codes from the input string and returns it.
163+
:rtype str
164+
"""
159165

160166
stripped = ""
161167

@@ -166,6 +172,27 @@ def strip(string):
166172
return stripped.strip()
167173

168174

175+
def strip_irc(string):
176+
"""
177+
Removes all raw MIRC color codes from the input string and returns it.
178+
:rtype str
179+
"""
180+
181+
return IRC_COLOR_RE.sub('', string)
182+
183+
184+
def strip_all(string):
185+
"""
186+
Removes all $() syntax and MIRC color codes from the input string and returns it.
187+
:rtype str
188+
"""
189+
190+
# we run strip_irc twice to avoid people putting a $() color code inside a MIRC one
191+
return strip_irc(strip(strip_irc(string)))
192+
193+
194+
# Internal use
195+
169196
def _convert(string):
170197
if not string.startswith("$(") and not string.endswith(")"):
171198
return string
@@ -183,4 +210,4 @@ def _convert(string):
183210
elif formatting in IRC_FORMATTING_DICT:
184211
ret += get_format(formatting)
185212

186-
return ret.strip()
213+
return ret.strip()

cloudbot/util/formatting.py

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66

77
from html.parser import HTMLParser
88

9+
from cloudbot.util.colors import strip_irc
10+
911

1012
# Constants
1113

@@ -163,17 +165,7 @@ def truncate(content, length=100, suffix='...'):
163165

164166
# compatibility
165167
truncate_str = truncate
166-
167-
168-
def strip_colors(text):
169-
"""
170-
Takes a string and removes all IRC color codes.
171-
:param text: Text to strip
172-
:type text: str
173-
:return Text stripped of IRC colors
174-
:rtype str
175-
"""
176-
return IRC_COLOR_RE.sub('', text)
168+
strip_colors = strip_irc
177169

178170

179171
def pluralize(num=0, text=''):

cloudbot/util/test/test_colors.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,26 @@
11
import pytest
22

33
from cloudbot.util.colors import parse, strip, get_available_colours, get_available_formats, get_color, get_format, \
4-
_convert, IRC_COLOUR_DICT
4+
_convert, strip_irc, strip_all, IRC_COLOUR_DICT
55

66
test_input = "The quick $(brown, red)brown$(clear) fox$(fake) jumps over the $(bold)lazy dog$(clear)."
77

88
test_parse_output = "The quick \x0305,04brown\x0f fox jumps over the \x02lazy dog\x0f."
99
test_strip_output = "The quick brown fox jumps over the lazy dog."
1010

11+
test_strip_irc_input = "\x02I am $(bold)bold\x02"
12+
test_strip_irc_result = "I am $(bold)bold"
13+
test_strip_all_result = "I am bold"
14+
1115

1216
def test_parse():
1317
assert parse(test_input) == test_parse_output
1418

1519

1620
def test_strip():
1721
assert strip(test_input) == test_strip_output
22+
assert strip_irc(test_strip_irc_input) == test_strip_irc_result
23+
assert strip_all(test_strip_irc_input) == test_strip_all_result
1824

1925

2026
def test_available_colors():

cloudbot/util/test/test_formatting.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ def test_pluralize():
5555

5656

5757
def test_strip_colors():
58+
# compatibility
5859
assert strip_colors(test_strip_colors_input) == test_strip_colors_result
5960

6061

0 commit comments

Comments
 (0)