From af629add58e0148eae11c77da712a8240b22fe49 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 '{!'.
Also, this version is less magic and all over easier to understand.
---
deluge/ui/console/screen.py | 42 +++++++++++++++++++++++-------------------
1 files changed, 23 insertions(+), 19 deletions(-)
diff --git a/deluge/ui/console/screen.py b/deluge/ui/console/screen.py
index 1a05c45..26327f6 100644
|
a
|
b
|
def add_line(self, text, refresh=True):
|
| 163 | 163 | def get_line_chunks(line): |
| 164 | 164 | """ |
| 165 | 165 | Returns a list of 2-tuples (color string, text) |
| | 166 | First tuple may have empty color string. |
| 166 | 167 | |
| 167 | 168 | """ |
| | 169 | if not line: |
| | 170 | return [] |
| 168 | 171 | chunks = [] |
| 169 | | num_chunks = line.count("{!") |
| 170 | | for i in range(num_chunks): |
| 171 | | # Find the beginning and end of the color tag |
| 172 | | beg = line.find("{!") |
| 173 | | end = line.find("!}") + 2 |
| 174 | | color = line[beg:end] |
| 175 | | line = line[end:] |
| 176 | | |
| 177 | | # Check to see if this is the last chunk |
| 178 | | if i + 1 == num_chunks: |
| 179 | | text = line |
| 180 | | else: |
| 181 | | # Not the last chunk so get the text up to the next tag |
| 182 | | # and remove the text from line |
| 183 | | text = line[:line.find("{!")] |
| 184 | | line = line[line.find("{!"):] |
| 185 | | |
| 186 | | chunks.append((color, text)) |
| 187 | | |
| | 172 | if not line.startswith('{!'): |
| | 173 | begin = line.find('{!') |
| | 174 | if begin < 0: |
| | 175 | begin = len(line) |
| | 176 | chunks.append( ('', line[:begin]) ) |
| | 177 | line = line[begin:] |
| | 178 | while line: |
| | 179 | # We know the line starts with '{!' here |
| | 180 | end_color = line.find('!}') |
| | 181 | if end_color < 0: |
| | 182 | # Could raise an exception here, but it may be better to |
| | 183 | # pass along what we've got to the real parser and see |
| | 184 | # what happens. |
| | 185 | chunks.append( (line, '') ) |
| | 186 | return chunks |
| | 187 | next_color = line.find('{!', end_color) |
| | 188 | if next_color < 0: |
| | 189 | next_color = len(line) |
| | 190 | chunks.append( (line[:end_color+2], line[end_color+2:next_color]) ) |
| | 191 | line = line[next_color:] |
| 188 | 192 | return chunks |
| 189 | 193 | |
| 190 | 194 | for line in text.splitlines(): |