r78803 - pyrepl/trunk/pyrepl/pyrepl

[email protected] Sun, 7 Nov 2010 10:53:54 +0100 (CET)
Newsgroups gmane.comp.python.pyrepl.checkins
Message-ID <[email protected]>
Author: antocuni
Date: Sun Nov  7 10:53:51 2010
New Revision: 78803

Modified:
   pyrepl/trunk/pyrepl/pyrepl/completing_reader.py
   pyrepl/trunk/pyrepl/pyrepl/unix_console.py
Log:
take care of escape sequences when computing the lenght of menu entries,
because in that case the length of the string might be different than the
number of characters actually displayed.  This lets pyrepl to display colored completions



Modified: pyrepl/trunk/pyrepl/pyrepl/completing_reader.py
==============================================================================
--- pyrepl/trunk/pyrepl/pyrepl/completing_reader.py	(original)
+++ pyrepl/trunk/pyrepl/pyrepl/completing_reader.py	Sun Nov  7 10:53:51 2010
@@ -42,16 +42,41 @@
     except IndexError:
         return wordlist[0][j:i]
 
-def build_menu(cons, wordlist, start):
-    maxlen = min(max(map(len, wordlist)), cons.width - 4)
-    cols = cons.width / (maxlen + 4)
+import re
+def stripcolor(s):
+    return stripcolor.regexp.sub('', s)
+stripcolor.regexp = re.compile(r"\x1B\[([0-9]{1,3}(;[0-9]{1,2})?)?[m|K]")
+
+def real_len(s):
+    return len(stripcolor(s))
+
+def left_align(s, maxlen):
+    stripped = stripcolor(s)
+    if len(stripped) > maxlen:
+        # too bad, we remove the color
+        return stripped[:maxlen]
+    padding = maxlen - len(stripped)
+    return s + ' '*padding
+
+USE_BRACKETS = True
+def build_menu(cons, wordlist, start, use_brackets=None):
+    if use_brackets is None:
+        use_brackets = USE_BRACKETS
+    if use_brackets:
+        item = "[ %s ]"
+        padding = 4
+    else:
+        item = "%s  "
+        padding = 2
+    maxlen = min(max(map(real_len, wordlist)), cons.width - padding)
+    cols = cons.width / (maxlen + padding)
     rows = (len(wordlist) - 1)/cols + 1
     menu = []
     i = start
     for r in range(rows):
         row = []
         for col in range(cols):
-            row.append("[ %-*s ]"%(maxlen, wordlist[i][:maxlen]))
+            row.append(item % left_align(wordlist[i], maxlen))
             i += 1
             if i >= len(wordlist):
                 break

Modified: pyrepl/trunk/pyrepl/pyrepl/unix_console.py
==============================================================================
--- pyrepl/trunk/pyrepl/pyrepl/unix_console.py	(original)
+++ pyrepl/trunk/pyrepl/pyrepl/unix_console.py	Sun Nov  7 10:53:51 2010
@@ -251,7 +251,11 @@
         # avoid writing code generators these days...)
         x = 0
         minlen = min(len(oldline), len(newline))
-        while x < minlen and oldline[x] == newline[x]:
+        #
+        # reuse the oldline as much as possible, but stop as soon as we
+        # encounter an ESCAPE, because it might be the start of an escape
+        # sequene
+        while x < minlen and oldline[x] == newline[x] and newline[x] != '\x1b':
             x += 1
         if oldline[x:] == newline[x+1:] and self.ich1:
             if ( y == self.__posxy[1] and x > self.__posxy[0]