Module: deluge
Branch: master
Commit: 62421080effd8889d2d83e8665d87d29b0f9b34f

Author: Nick Lanham <[email protected]>
Date:   Tue Feb 22 18:04:16 2011 +0100

better unicode handling and trimming

---

 deluge/ui/console/modes/format_utils.py |   22 ++++++++++++++++++++--
 1 files changed, 20 insertions(+), 2 deletions(-)

diff --git a/deluge/ui/console/modes/format_utils.py 
b/deluge/ui/console/modes/format_utils.py
index 81906d4..811c742 100644
--- a/deluge/ui/console/modes/format_utils.py
+++ b/deluge/ui/console/modes/format_utils.py
@@ -62,20 +62,38 @@ def format_priority(prio):
     else:
         return pstring
 
-def trim_string(string, w):
+def trim_string(string, w, have_dbls):
+    if have_dbls:
+        # have to do this the slow way
+        chrs = []
+        width = 4
+        idx = 0
+        while width < w:
+            chrs.append(string[idx])
+            if unicodedata.east_asian_width(string[idx]) in ['W','F']:
+                width += 2
+            else:
+                width += 1
+            idx += 1
+        if width != w:
+            chrs.pop()
+            chrs.append('.')
+        return "%s... "%("".join(chrs))
+    else:
         return "%s... "%(string[0:w-4])
 
 def format_column(col, lim):
     dbls = 0
     if haveud and isinstance(col,unicode):
         # might have some double width chars
+        col = unicodedata.normalize("NFC",col)
         for c in col:
             if unicodedata.east_asian_width(c) in ['W','F']:
                 # found a wide/full char
                 dbls += 1
     size = len(col)+dbls
     if (size >= lim - 1):
-        return trim_string(col,lim)
+        return trim_string(col,lim,dbls>0)
     else:
         return "%s%s"%(col," "*(lim-size))
 

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