Fix crash when string length makes line longer than terminal width

This commit is contained in:
Andrew Resch 2009-10-31 18:43:48 +00:00
parent f876c17efd
commit fd24e1c17c
3 changed files with 15 additions and 7 deletions

View File

@ -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

View File

@ -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")

View File

@ -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)