From 8f12558148e70cc87b326f1970ec696fa746734a Mon Sep 17 00:00:00 2001
From: Eirik Byrkjeflot Anonsen <eirik@eirikba.org>
Date: Sat, 24 Mar 2012 16:28:06 +0100
Subject: [PATCH] Make sure Screen.add_line.get_line_chunks() does not discard beginning of line.

The old version would discard everything before the first '{!'.
---
 deluge/ui/console/screen.py |   29 +++++++++++------------------
 1 files changed, 11 insertions(+), 18 deletions(-)

diff --git a/deluge/ui/console/screen.py b/deluge/ui/console/screen.py
index 1a05c45..763551a 100644
--- a/deluge/ui/console/screen.py
+++ b/deluge/ui/console/screen.py
@@ -163,28 +163,21 @@ def add_line(self, text, refresh=True):
         def get_line_chunks(line):
             """
             Returns a list of 2-tuples (color string, text)
+            First tuple may have empty color string.
 
             """
+            spans = line.split('{!')
             chunks = []
-            num_chunks = line.count("{!")
-            for i in range(num_chunks):
-                # Find the beginning and end of the color tag
-                beg = line.find("{!")
-                end = line.find("!}") + 2
-                color = line[beg:end]
-                line = line[end:]
-
-                # Check to see if this is the last chunk
-                if i + 1 == num_chunks:
-                    text = line
+            if spans[0] != '':
+                chunks.append( ('', spans[0]) )
+            spans = spans[1:]
+            for span in spans:
+                span = '{!' + span
+                end = span.find('!}')
+                if end < 0:
+                    chunks.append((span, ''))
                 else:
-                    # Not the last chunk so get the text up to the next tag
-                    # and remove the text from line
-                    text = line[:line.find("{!")]
-                    line = line[line.find("{!"):]
-
-                chunks.append((color, text))
-
+                    chunks.append((span[:end+2], span[end+2:]))
             return chunks
 
         for line in text.splitlines():
-- 
1.7.2.5

