[or-cvs] r23820: {arm} using curses.textpad to improve input for all text fields, a (in arm/trunk/src: interface interface/graphing util)

Damian Johnson atagar1 at gmail.com
Thu Nov 18 06:50:38 UTC 2010


Author: atagar
Date: 2010-11-18 06:50:37 +0000 (Thu, 18 Nov 2010)
New Revision: 23820

Modified:
   arm/trunk/src/interface/controller.py
   arm/trunk/src/interface/graphing/psStats.py
   arm/trunk/src/util/panel.py
Log:
using curses.textpad to improve input for all text fields, and moving it into a utility
fix: skipping SETCONF calls if the value is unchanged
fix: crashing issue when the 'queries.ps.rate' config value was undefined and the stats graph was displayed



Modified: arm/trunk/src/interface/controller.py
===================================================================
--- arm/trunk/src/interface/controller.py	2010-11-17 18:13:04 UTC (rev 23819)
+++ arm/trunk/src/interface/controller.py	2010-11-18 06:50:37 UTC (rev 23820)
@@ -958,20 +958,9 @@
         panels["control"].setMsg("Path to save log snapshot: ")
         panels["control"].redraw(True)
         
-        # makes cursor and typing visible
-        try: curses.curs_set(1)
-        except curses.error: pass
-        curses.echo()
-        
         # gets user input (this blocks monitor updates)
-        pathInput = panels["control"].win.getstr(0, 27)
+        pathInput = panels["control"].getstr(0, 27)
         
-        # reverts visability settings
-        try: curses.curs_set(0)
-        except curses.error: pass
-        curses.noecho()
-        curses.halfdelay(REFRESH_RATE * 10) # evidenlty previous tweaks reset this...
-        
         if pathInput != "":
           try:
             panels["log"].saveSnapshot(pathInput)
@@ -999,11 +988,6 @@
         panels["control"].setMsg("Events to log: ")
         panels["control"].redraw(True)
         
-        # makes cursor and typing visible
-        try: curses.curs_set(1)
-        except curses.error: pass
-        curses.echo()
-        
         # lists event types
         popup = panels["popup"]
         popup.height = 11
@@ -1020,15 +1004,9 @@
         popup.refresh()
         
         # gets user input (this blocks monitor updates)
-        eventsInput = panels["control"].win.getstr(0, 15)
+        eventsInput = panels["control"].getstr(0, 15)
         eventsInput = eventsInput.replace(' ', '') # strips spaces
         
-        # reverts visability settings
-        try: curses.curs_set(0)
-        except curses.error: pass
-        curses.noecho()
-        curses.halfdelay(REFRESH_RATE * 10) # evidenlty previous tweaks reset this...
-        
         # it would be nice to quit on esc, but looks like this might not be possible...
         if eventsInput != "":
           try:
@@ -1075,20 +1053,9 @@
           panels["control"].setMsg("Regular expression: ")
           panels["control"].redraw(True)
           
-          # makes cursor and typing visible
-          try: curses.curs_set(1)
-          except curses.error: pass
-          curses.echo()
-          
           # gets user input (this blocks monitor updates)
-          regexInput = panels["control"].win.getstr(0, 20)
+          regexInput = panels["control"].getstr(0, 20)
           
-          # reverts visability settings
-          try: curses.curs_set(0)
-          except curses.error: pass
-          curses.noecho()
-          curses.halfdelay(REFRESH_RATE * 10)
-          
           if regexInput != "":
             try:
               panels["log"].setFilter(re.compile(regexInput))
@@ -1562,30 +1529,18 @@
         panels["control"].setMsg(titleMsg)
         panels["control"].redraw(True)
         
-        # makes cursor and typing visible
-        try: curses.curs_set(1)
-        except curses.error: pass
-        
-        # temporary subwindow for user input
         displayWidth = panels["control"].getPreferredSize()[1]
-        inputSubwindow = panels["control"].parent.subwin(1, displayWidth - len(titleMsg), panels["control"].top, len(titleMsg))
+        initialValue = selection.get(configStatePanel.FIELD_VALUE)
         
-        # prepopulates the current value
-        if CONFIG["features.config.prepopulateEditValues"]:
-          initialValue = selection.get(configStatePanel.FIELD_VALUE)
-          if initialValue != "<none>": inputSubwindow.addstr(0, 0, initialValue)
+        # initial input for the text field
+        initialText = ""
+        if CONFIG["features.config.prepopulateEditValues"] and initialValue != "<none>":
+          initialText = initialValue
         
-        # fetches the user's input for the new config value
-        textbox = curses.textpad.Textbox(inputSubwindow)
-        newConfigValue = textbox.edit().strip()
+        newConfigValue = panels["control"].getstr(0, len(titleMsg), initialText)
         
-        # reverts visability settings
-        try: curses.curs_set(0)
-        except curses.error: pass
-        #curses.halfdelay(REFRESH_RATE * 10) # evidenlty previous tweaks reset this...
-        
         # it would be nice to quit on esc, but looks like this might not be possible...
-        if newConfigValue != "":
+        if newConfigValue != "" and newConfigValue != initialValue:
           conn = torTools.getConn()
           
           # if the value's a boolean then allow for 'true' and 'false' inputs

Modified: arm/trunk/src/interface/graphing/psStats.py
===================================================================
--- arm/trunk/src/interface/graphing/psStats.py	2010-11-17 18:13:04 UTC (rev 23819)
+++ arm/trunk/src/interface/graphing/psStats.py	2010-11-18 06:50:37 UTC (rev 23820)
@@ -50,7 +50,7 @@
   def getRefreshRate(self):
     # provides the rate at which the panel has new stats to display
     if self._config["features.graph.ps.cachedOnly"]:
-      return int(conf.getConfig("arm").get("queries.ps.rate"))
+      return int(conf.getConfig("arm").get("queries.ps.rate", 5))
     else: return 1
   
   def getHeaderLabel(self, width, isPrimary):

Modified: arm/trunk/src/util/panel.py
===================================================================
--- arm/trunk/src/util/panel.py	2010-11-17 18:13:04 UTC (rev 23819)
+++ arm/trunk/src/util/panel.py	2010-11-18 06:50:37 UTC (rev 23820)
@@ -327,6 +327,40 @@
         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 = ""):
+    """
+    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 the initial value. This should only be
+    called from the context of a panel's draw method.
+    
+    Arguments:
+      y           - vertical location
+      x           - horizontal location
+      initialText - starting text in this field
+    """
+    
+    # makes cursor visible
+    try: previousCursorState = curses.curs_set(1)
+    except curses.error: previousCursorState = 0
+    
+    # temporary subwindow for user input
+    displayWidth = self.getPreferredSize()[1]
+    inputSubwindow = self.parent.subwin(1, displayWidth - x, self.top, x)
+    
+    # prepopulates the initial text
+    if initialText: inputSubwindow.addstr(0, 0, initialText)
+    
+    # displays the text field, blocking until the user's done
+    textbox = curses.textpad.Textbox(inputSubwindow)
+    userInput = textbox.edit().strip()
+    
+    # reverts visability settings
+    try: curses.curs_set(previousCursorState)
+    except curses.error: pass
+    
+    return userInput
+  
   def addScrollBar(self, top, bottom, size, drawTop = 0, drawBottom = -1):
     """
     Draws a left justified scroll bar reflecting position within a vertical



More information about the tor-commits mailing list