summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorAndrew Resch <andrewresch@gmail.com>2009-10-31 18:43:48 +0000
committerAndrew Resch <andrewresch@gmail.com>2009-10-31 18:43:48 +0000
commit41db357084cf80a83523d1677163d59f40dc03fa (patch)
tree55b5f84fb6f60f133e53bbe21a44f250009d9a8d
parent3611584b0b368f1114c6e0b96bfde5627f0022ca (diff)
downloaddeluge-41db357084cf80a83523d1677163d59f40dc03fa.tar.gz
deluge-41db357084cf80a83523d1677163d59f40dc03fa.tar.bz2
deluge-41db357084cf80a83523d1677163d59f40dc03fa.zip
Fix crash when string length makes line longer than terminal width
-rw-r--r--ChangeLog1
-rw-r--r--deluge/ui/console/colors.py9
-rw-r--r--deluge/ui/console/commands/info.py8
-rw-r--r--deluge/ui/console/screen.py5
4 files changed, 16 insertions, 7 deletions
diff --git a/ChangeLog b/ChangeLog
index 2eacdf817..ae6023851 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -22,6 +22,7 @@
==== Console ====
* Fix displaying non-ascii strings
* Fix #1052 crash when issuing commands while not connected to a daemon
+ * Fix crash when string length makes line longer than terminal width
=== Deluge 1.2.0_rc2 (25 October 2009) ===
==== GtkUI ====
diff --git a/deluge/ui/console/colors.py b/deluge/ui/console/colors.py
index 4681f9827..ab01915a1 100644
--- a/deluge/ui/console/colors.py
+++ b/deluge/ui/console/colors.py
@@ -115,7 +115,7 @@ def strip_colors(line):
return line
-def get_line_length(line):
+def get_line_length(line, encoding="UTF-8"):
"""
Returns the string length without the color formatting.
@@ -123,6 +123,8 @@ def get_line_length(line):
if line.count("{!") != line.count("!}"):
raise BadColorString("Number of {! is not equal to number of !}")
+ line = line.encode(encoding, "replace")
+
# Remove all the color tags
line = strip_colors(line)
@@ -130,16 +132,19 @@ def get_line_length(line):
line = replace_tabs(line)
return len(line)
-def parse_color_string(s):
+def parse_color_string(s, encoding="UTF-8"):
"""
Parses a string and returns a list of 2-tuples (color, string).
:param s:, string to parse
+ :param encoding: the encoding to use on output
"""
if s.count("{!") != s.count("!}"):
raise BadColorString("Number of {! is not equal to number of !}")
+ s = s.encode(encoding, "replace")
+
ret = []
# Keep track of where the strings
col_index = 0
diff --git a/deluge/ui/console/commands/info.py b/deluge/ui/console/commands/info.py
index 9f69484a7..57bd4e38d 100644
--- a/deluge/ui/console/commands/info.py
+++ b/deluge/ui/console/commands/info.py
@@ -112,7 +112,7 @@ class Command(BaseCommand):
if not args:
torrent_ids.extend(self.console.match_torrent(""))
-
+
def on_torrents_status(status):
# Print out the information for each torrent
for key, value in status.items():
@@ -185,6 +185,12 @@ class Command(BaseCommand):
s += "{!success!}"
s += " %s" % (fp)
+ # Check if this is too long for the screen and reduce the path
+ # if necessary
+ cols = self.console.screen.cols
+ slen = colors.get_line_length(s, self.console.screen.encoding)
+ if slen > cols:
+ s = s.replace(f["path"], f["path"][slen - cols + 1:])
self.console.write(s)
self.console.write(" {!info!}::Peers")
diff --git a/deluge/ui/console/screen.py b/deluge/ui/console/screen.py
index 22ccecba1..999d867f9 100644
--- a/deluge/ui/console/screen.py
+++ b/deluge/ui/console/screen.py
@@ -230,7 +230,7 @@ class Screen(CursesStdIO):
"""
col = 0
try:
- parsed = colors.parse_color_string(string)
+ parsed = colors.parse_color_string(string, self.encoding)
except colors.BadColorString, e:
log.error("Cannot add bad color string %s: %s", string, e)
return
@@ -239,9 +239,6 @@ class Screen(CursesStdIO):
if index + 1 == len(parsed):
# This is the last string so lets append some " " to it
s += " " * (self.cols - (col + len(s)) - 1)
- if isinstance(s, unicode):
- #Have to use replace as character counting has already been done\
- s = s.encode(self.encoding, 'replace')
self.stdscr.addstr(row, col, s, color)
col += len(s)