chiark / gitweb /
Colourise spaces properly.
authorSimon Tatham <anakin@pobox.com>
Sat, 2 Dec 2023 18:01:26 +0000 (18:01 +0000)
committerSimon Tatham <anakin@pobox.com>
Sat, 2 Dec 2023 18:01:26 +0000 (18:01 +0000)
It showed up in that error report, and it would have been nasty
copy-pasting as well.

text.py

diff --git a/text.py b/text.py
index 4c0e47339aa570baffe9a221ea5e753b4fea05fa..710dc120e9f40039b4bf065c630ba45fda20cc06 100644 (file)
--- a/text.py
+++ b/text.py
@@ -143,18 +143,19 @@ class Media:
 class Paragraph:
     def __init__(self):
         self.words = []
+        self.space_colours = []
         self.unfinished_word = ColouredString('')
 
     def render(self, width):
         # For the moment, greedy algorithm. We can worry about cleverness later
         line, space = ColouredString(''), ColouredString('')
-        for word in self.words:
+        for word, space_colour in zip(self.words, self.space_colours):
             if line != "" and (line + space + word).width >= width:
                 yield line
                 line, space = ColouredString(''), ColouredString('')
 
             line += space + word
-            space = ColouredString(' ')
+            space = ColouredString(' ', space_colour)
 
             if line.width >= width:
                 # FIXME: wrap explicitly?
@@ -167,15 +168,16 @@ class Paragraph:
     def empty(self):
         return len(self.words) == 0
 
-    def end_word(self):
+    def end_word(self, space_colour=' '):
         if len(self.unfinished_word) > 0:
             self.words.append(self.unfinished_word)
+            self.space_colours.append(space_colour)
             self.unfinished_word = ColouredString('')
 
     def add(self, text):
         for c in text:
             if str(c) == ' ':
-                self.end_word()
+                self.end_word(ColouredString(c).c)
             else:
                 self.unfinished_word += c
 
@@ -317,13 +319,13 @@ class RenderTests(unittest.TestCase):
         html = "<p>Test of some <code>literal code</code></p>"
         self.assertEqual(self.parse_html(html), [
             ColouredString('Test of some literal code',
-                           '             ccccccc cccc'),
+                           '             cccccccccccc'),
         ])
 
         html = "<p>Test of some <strong>strong text</strong></p>"
         self.assertEqual(self.parse_html(html), [
             ColouredString('Test of some strong text',
-                           '             ssssss ssss'),
+                           '             sssssssssss'),
         ])
 
         html = """<p>Test of a <a href="https://some.instance/tags/hashtag" class="mention hashtag" rel="nofollow noopener noreferrer" target="_blank">#<span>hashtag</span></a></p>"""
@@ -342,7 +344,7 @@ class RenderTests(unittest.TestCase):
         html = """<p>Test of some <nonsense>unsupported</nonsense> <blither>HTML tags</blither></p>"""
         self.assertEqual(self.parse_html(html), [
             ColouredString('Unsupported markup tags: <blither> <nonsense>',
-                           '!!!!!!!!!!! !!!!!! !!!!! !!!!!!!!! !!!!!!!!!!'),
+                           '!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!'),
             ColouredString(''),
             ColouredString('Test of some unsupported HTML tags'),
         ])
@@ -353,9 +355,9 @@ class RenderTests(unittest.TestCase):
             ColouredString('https://a.b/c',
                            'MMMMMMMMMMMMM'),
             ColouredString('  foo foo foo',
-                           '  mmm mmm mmm'),
+                           '  mmmmmmmmmmm'),
             ColouredString('  foo foo foo',
-                           '  mmm mmm mmm'),
+                           '  mmmmmmmmmmm'),
             ColouredString('  foo',
                            '  mmm'),
             ColouredString(''),
@@ -364,11 +366,11 @@ class RenderTests(unittest.TestCase):
             ColouredString('https://a.b/c',
                            'MMMMMMMMMMMMM'),
             ColouredString('  foo foo',
-                           '  mmm mmm'),
+                           '  mmmmmmm'),
             ColouredString('  foo foo',
-                           '  mmm mmm'),
+                           '  mmmmmmm'),
             ColouredString('  foo foo',
-                           '  mmm mmm'),
+                           '  mmmmmmm'),
             ColouredString('  foo',
                            '  mmm'),
             ColouredString(''),