Author: andar

Revision: 5899

Log:
        Fix crash when string length makes line longer than terminal width

Diff:
Modified: branches/1.2_RC/ChangeLog
===================================================================
--- branches/1.2_RC/ChangeLog   2009-10-31 05:50:01 UTC (rev 5898)
+++ branches/1.2_RC/ChangeLog   2009-10-31 18:43:48 UTC (rev 5899)
@@ -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 ====

Modified: branches/1.2_RC/deluge/ui/console/colors.py
===================================================================
--- branches/1.2_RC/deluge/ui/console/colors.py 2009-10-31 05:50:01 UTC (rev 
5898)
+++ branches/1.2_RC/deluge/ui/console/colors.py 2009-10-31 18:43:48 UTC (rev 
5899)
@@ -115,7 +115,7 @@
 
     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 @@
     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 @@
     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

Modified: branches/1.2_RC/deluge/ui/console/commands/info.py
===================================================================
--- branches/1.2_RC/deluge/ui/console/commands/info.py  2009-10-31 05:50:01 UTC 
(rev 5898)
+++ branches/1.2_RC/deluge/ui/console/commands/info.py  2009-10-31 18:43:48 UTC 
(rev 5899)
@@ -112,7 +112,7 @@
 
         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 @@
                     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")

Modified: branches/1.2_RC/deluge/ui/console/screen.py
===================================================================
--- branches/1.2_RC/deluge/ui/console/screen.py 2009-10-31 05:50:01 UTC (rev 
5898)
+++ branches/1.2_RC/deluge/ui/console/screen.py 2009-10-31 18:43:48 UTC (rev 
5899)
@@ -230,7 +230,7 @@
         """
         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 @@
             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)
 

Modified: trunk/deluge/ui/console/colors.py
===================================================================
--- trunk/deluge/ui/console/colors.py   2009-10-31 05:50:01 UTC (rev 5898)
+++ trunk/deluge/ui/console/colors.py   2009-10-31 18:43:48 UTC (rev 5899)
@@ -115,7 +115,7 @@
 
     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 @@
     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 @@
     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

Modified: trunk/deluge/ui/console/commands/info.py
===================================================================
--- trunk/deluge/ui/console/commands/info.py    2009-10-31 05:50:01 UTC (rev 
5898)
+++ trunk/deluge/ui/console/commands/info.py    2009-10-31 18:43:48 UTC (rev 
5899)
@@ -112,7 +112,7 @@
 
         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 @@
                     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")

Modified: trunk/deluge/ui/console/screen.py
===================================================================
--- trunk/deluge/ui/console/screen.py   2009-10-31 05:50:01 UTC (rev 5898)
+++ trunk/deluge/ui/console/screen.py   2009-10-31 18:43:48 UTC (rev 5899)
@@ -230,7 +230,7 @@
         """
         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 @@
             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)
 



--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"deluge-commit" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to 
[email protected]
For more options, visit this group at 
http://groups.google.com/group/deluge-commit?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to