From 967801cf175ee8f4a826217589332b7672dc5096 Mon Sep 17 00:00:00 2001 From: Isaiah Odhner Date: Tue, 11 Jul 2023 18:54:52 -0400 Subject: [PATCH] Prevent digits in document from merging with mIRC color codes If digits were present in the document, they were concatenated after the color codes, and they ran together indistinguishably. This changed the color index numbers that were read back, to potentially invalid color indices, and lost the digits as part of the document. It needs the delimiter of the ending escape code to separate the digits. --- cspell.json | 1 + src/textual_paint/paint.py | 7 ++++++- 2 files changed, 7 insertions(+), 1 deletion(-) diff --git a/cspell.json b/cspell.json index e91ab12..97a5152 100644 --- a/cspell.json +++ b/cspell.json @@ -36,6 +36,7 @@ "dasharray", "Deutsch", "DIALOGEX", + "disambiguates", "domtree", "emacsclient", "executablepath", diff --git a/src/textual_paint/paint.py b/src/textual_paint/paint.py index 52e4fd9..e01e04a 100755 --- a/src/textual_paint/paint.py +++ b/src/textual_paint/paint.py @@ -916,6 +916,9 @@ class AnsiArtDocument: irc_text += str(closest_color(Color.from_rich_color(style.color))) irc_text += "," irc_text += str(closest_color(Color.from_rich_color(style.bgcolor))) + if text[0] in "0123456789,": + # separate the color code from the text with an ending escape character + irc_text += "\x03" irc_text += text else: irc_text += text @@ -1007,7 +1010,8 @@ class AnsiArtDocument: fg_color = default_fg color_escape = "\x03" - color_escape_re = re.compile(r"\x03(\d{1,2})?(?:,(\d{1,2}))?") + # an optional escape code at the end disambiguates a digit from part of the color code + color_escape_re = re.compile(r"\x03(\d{1,2})?(?:,(\d{1,2}))?\x03?") reset_escape = "\x0F" index = 0 @@ -1017,6 +1021,7 @@ class AnsiArtDocument: match = color_escape_re.match(text[index:]) if match: index += len(match.group(0)) + # TODO: should a one-value syntax reset the background? bg_color = default_bg fg_color = default_fg if match.group(1):