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 984f9031d..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)