[tor-commits] [arm/master] fix: workaround for textpad insert mode issues

atagar at torproject.org atagar at torproject.org
Mon Jul 4 15:40:31 UTC 2011


commit dd81736d16d638343b3954f2def575378b5ea309
Author: Damian Johnson <atagar at torproject.org>
Date:   Sun Jul 3 16:54:53 2011 -0700

    fix: workaround for textpad insert mode issues
    
    The insert mode of the textpad class has a couple issues:
    - it's only available in Python 2.6+
    - if the text doens't have the default formatting then it doens't work (input
      is replacement mode instead)
    
    This drops usage for the insert mode flag, instead doing it ourselves via
    arm's textpad validator. This is kinda vital for the wizard since that text
    has attibutes and not having insert mode there was pretty painful.
---
 src/util/panel.py   |   29 +++++++++++++++++++----------
 src/util/uiTools.py |    8 +++++---
 2 files changed, 24 insertions(+), 13 deletions(-)

diff --git a/src/util/panel.py b/src/util/panel.py
index 59d2b13..1be2306 100644
--- a/src/util/panel.py
+++ b/src/util/panel.py
@@ -2,11 +2,11 @@
 Wrapper for safely working with curses subwindows.
 """
 
-import sys
 import copy
 import time
 import traceback
 import curses
+import curses.ascii
 import curses.textpad
 from threading import RLock
 
@@ -613,15 +613,9 @@ class Panel():
     
     # Displays the text field, blocking until the user's done. This closes the
     # text panel and returns userInput to the initial text if the user presses
-    # escape. Insert mode is available in Python 2.6+, before that the
-    # constructor only accepted a subwindow argument as per:
-    # https://trac.torproject.org/projects/tor/ticket/2354
+    # escape.
     
-    majorVersion, minorVersion = sys.version_info[:2]
-    if majorVersion == 2 and minorVersion >= 6:
-      textbox = curses.textpad.Textbox(inputSubwindow, True)
-    else:
-      textbox = curses.textpad.Textbox(inputSubwindow)
+    textbox = curses.textpad.Textbox(inputSubwindow)
     
     textbox.win.attron(format)
     userInput = textbox.edit(lambda key: _textboxValidate(textbox, key)).strip()
@@ -738,7 +732,22 @@ def _textboxValidate(textbox, key):
   """
   
   y, x = textbox.win.getyx()
-  if key == 27:
+  
+  if curses.ascii.isprint(key) and x < textbox.maxx:
+    # Shifts the existing text forward so input is an insert method rather
+    # than replacement. The curses.textpad accepts an insert mode flag but
+    # this has a couple issues...
+    # - The flag is only available for Python 2.6+, before that the
+    #   constructor only accepted a subwindow argument as per:
+    #   https://trac.torproject.org/projects/tor/ticket/2354
+    # - The textpad doesn't shift text that has text attributes. This is
+    #   because keycodes read by textbox.win.inch() includes formatting,
+    #   causing the curses.ascii.isprint() check it does to fail.
+    
+    currentInput = textbox.gather()
+    textbox.win.addstr(y, x + 1, currentInput[x:textbox.maxx - 1])
+    textbox.win.move(y, x) # reverts cursor movement during gather call
+  elif key == 27:
     # curses.ascii.BEL is a character codes that causes textpad to terminate
     return curses.ascii.BEL
   elif key == curses.KEY_HOME:
diff --git a/src/util/uiTools.py b/src/util/uiTools.py
index 52885de..68b93af 100644
--- a/src/util/uiTools.py
+++ b/src/util/uiTools.py
@@ -282,15 +282,17 @@ def cropStr(msg, size, minWordLen = 4, minCrop = 0, endType = Ending.ELLIPSE, ge
   if getRemainder: return (returnMsg, remainder)
   else: return returnMsg
 
-def padStr(msg, size):
+def padStr(msg, size, cropExtra = False):
   """
   Provides the string padded with whitespace to the given length.
   
   Arguments:
-    msg  - string to be padded
-    size - length to be padded to
+    msg       - string to be padded
+    size      - length to be padded to
+    cropExtra - crops string if it's longer than the size if true
   """
   
+  if cropExtra: msg = msg[:size]
   return ("%%-%is" % size) % msg
 
 def camelCase(label, divider = "_", joiner = " "):





More information about the tor-commits mailing list