[Author Prev][Author Next][Thread Prev][Thread Next][Author Index][Thread Index]

[tor-commits] [arm/master] Adding text input validator for command backlogs



commit ec436c57401218a9680edbae4e1e043f3c977b30
Author: Damian Johnson <atagar@xxxxxxxxxxxxxx>
Date:   Tue Aug 23 10:07:27 2011 -0700

    Adding text input validator for command backlogs
    
    This allows the up and down arrow keys to scroll through a backlog of commands
    (it'll be used in the interpretor panel).
---
 src/util/textInput.py |   49 +++++++++++++++++++++++++++++++++++++++++++++++++
 1 files changed, 49 insertions(+), 0 deletions(-)

diff --git a/src/util/textInput.py b/src/util/textInput.py
index 9422f81..c3c229f 100644
--- a/src/util/textInput.py
+++ b/src/util/textInput.py
@@ -99,3 +99,52 @@ class BasicValidator(TextInputValidator):
     
     return PASS
 
+class HistoryValidator(TextInputValidator):
+  """
+  This intercepts the up and down arrow keys to scroll through a backlog of
+  previous commands.
+  """
+  
+  def __init__(self, commandBacklog = [], nextValidator = None):
+    TextInputValidator.__init__(self, nextValidator)
+    
+    # contents that can be scrolled back through, newest to oldest
+    self.commandBacklog = commandBacklog
+    
+    # selected item from the backlog, -1 if we're not on a backlog item
+    self.selectionIndex = -1
+    
+    # the fields input prior to selecting a backlog item
+    self.customInput = ""
+  
+  def handleKey(self, key, textbox):
+    if key in (curses.KEY_UP, curses.KEY_DOWN):
+      offset = 1 if key == curses.KEY_UP else -1
+      newSelection = self.selectionIndex + offset
+      
+      # constrains the new selection to valid bounds
+      newSelection = max(-1, newSelection)
+      newSelection = min(len(self.commandBacklog) - 1, newSelection)
+      
+      # skips if this is a no-op
+      if self.selectionIndex == newSelection:
+        return None
+      
+      # saves the previous input if we weren't on the backlog
+      if self.selectionIndex == -1:
+        self.customInput = textbox.gather().strip()
+      
+      if newSelection == -1: newInput = self.customInput
+      else: newInput = self.commandBacklog[newSelection]
+      
+      y, _ = textbox.win.getyx()
+      _, maxX = textbox.win.getmaxyx()
+      textbox.win.clear()
+      textbox.win.addstr(y, 0, newInput[:maxX - 1])
+      textbox.win.move(y, min(len(newInput), maxX - 1))
+      
+      self.selectionIndex = newSelection
+      return None
+    
+    return PASS
+

_______________________________________________
tor-commits mailing list
tor-commits@xxxxxxxxxxxxxxxxxxxx
https://lists.torproject.org/cgi-bin/mailman/listinfo/tor-commits