[tor-commits] [arm/release] fix: clearing content in a getstr input field

atagar at torproject.org atagar at torproject.org
Sun Jul 17 06:08:27 UTC 2011


commit 6fa3123d8ad9c669db3079d144f7de5117eabd2b
Author: Damian Johnson <atagar at torproject.org>
Date:   Thu Jun 23 09:03:31 2011 -0700

    fix: clearing content in a getstr input field
    
    Any stray characters within the textbox is interpreted as part of the initial
    input. Clearing the input field when we make it and giving an option for
    limiting its size.
---
 src/util/panel.py |   12 +++++++++++-
 1 files changed, 11 insertions(+), 1 deletions(-)

diff --git a/src/util/panel.py b/src/util/panel.py
index 5cbeaee..59d2b13 100644
--- a/src/util/panel.py
+++ b/src/util/panel.py
@@ -573,18 +573,23 @@ class Panel():
         baseMsg = "Unclosed formatting tag%s:" % ("s" if len(expectedCloseTags) > 1 else "")
         raise ValueError("%s: '%s'\n  \"%s\"" % (baseMsg, "', '".join(expectedCloseTags), msg))
   
-  def getstr(self, y, x, initialText = "", format = None):
+  def getstr(self, y, x, initialText = "", format = None, maxWidth = None):
     """
     Provides a text field where the user can input a string, blocking until
     they've done so and returning the result. If the user presses escape then
     this terminates and provides back None. This should only be called from
     the context of a panel's draw method.
     
+    This blanks any content within the space that the input field is rendered
+    (otherwise stray characters would be interpreted as part of the initial
+    input).
+    
     Arguments:
       y           - vertical location
       x           - horizontal location
       initialText - starting text in this field
       format      - format used for the text
+      maxWidth    - maximum width for the text field
     """
     
     if not format: format = curses.A_NORMAL
@@ -595,8 +600,13 @@ class Panel():
     
     # temporary subwindow for user input
     displayWidth = self.getPreferredSize()[1]
+    if maxWidth: displayWidth = min(displayWidth, maxWidth + x)
     inputSubwindow = self.parent.subwin(1, displayWidth - x, self.top + y, self.left + x)
     
+    # blanks the field's area, filling it with the font in case it's hilighting
+    inputSubwindow.clear()
+    inputSubwindow.bkgd(' ', format)
+    
     # prepopulates the initial text
     if initialText:
       inputSubwindow.addstr(0, 0, initialText[:displayWidth - x - 1], format)





More information about the tor-commits mailing list