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

[tor-commits] [nyx/master] Added history validation in interpreter panel



commit d9bfc5df053567b3e3df55b46c0666a7a7486188
Author: Sambuddha Basu <sambuddhabasu1@xxxxxxxxx>
Date:   Sat Jul 2 01:34:52 2016 -0700

    Added history validation in interpreter panel
---
 nyx/curses.py            | 39 +++++++++++++++++++++++++++++++++++++--
 nyx/panel/interpreter.py |  2 +-
 2 files changed, 38 insertions(+), 3 deletions(-)

diff --git a/nyx/curses.py b/nyx/curses.py
index 53a2c43..21b7a42 100644
--- a/nyx/curses.py
+++ b/nyx/curses.py
@@ -244,7 +244,7 @@ def key_input(input_timeout = None):
   return KeyInput(CURSES_SCREEN.getch())
 
 
-def str_input(x, y, initial_text = ''):
+def str_input(x, y, initial_text = '', backlog=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
@@ -284,6 +284,38 @@ def str_input(x, y, initial_text = ''):
     else:
       return key
 
+  history_dict = {'selection_index': -1, 'custom_input': ''}
+
+  def handle_history_key(textbox, key):
+    if key in (curses.KEY_UP, curses.KEY_DOWN):
+      offset = 1 if key == curses.KEY_UP else -1
+      new_selection = history_dict['selection_index'] + offset
+
+      new_selection = max(-1, new_selection)
+      new_selection = min(len(backlog) - 1, new_selection)
+
+      if history_dict['selection_index'] == new_selection:
+        return None
+
+      if history_dict['selection_index'] == -1:
+        history_dict['custom_input'] = textbox.gather().strip()
+
+      if new_selection == -1:
+        new_input = history_dict['custom_input']
+      else:
+        new_input = backlog[new_selection]
+
+      y, _ = textbox.win.getyx()
+      _, max_x = textbox.win.getmaxyx()
+      textbox.win.clear()
+      textbox.win.addstr(y, 0, new_input[:max_x - 1])
+      textbox.win.move(y, min(len(new_input), max_x - 1))
+
+      history_dict['selection_index'] = new_selection
+      return None
+
+    return handle_key(textbox, key)
+
   with CURSES_LOCK:
     if HALT_ACTIVITY:
       return None
@@ -300,7 +332,10 @@ def str_input(x, y, initial_text = ''):
     curses_subwindow.addstr(0, 0, initial_text[:width - 1])
 
     textbox = curses.textpad.Textbox(curses_subwindow, insert_mode = True)
-    user_input = textbox.edit(lambda key: handle_key(textbox, key)).strip()
+    if backlog is not None:
+      user_input = textbox.edit(lambda key: handle_history_key(textbox, key)).strip()
+    else:
+      user_input = textbox.edit(lambda key: handle_key(textbox, key)).strip()
 
     try:
       curses.curs_set(0)  # hide cursor
diff --git a/nyx/panel/interpreter.py b/nyx/panel/interpreter.py
index 6b79770..8a48b34 100644
--- a/nyx/panel/interpreter.py
+++ b/nyx/panel/interpreter.py
@@ -96,7 +96,7 @@ class InterpreterPanel(panel.Panel):
         self.redraw(True)
         _scroll(nyx.curses.KeyInput(curses.KEY_END))
         page_height = self.get_preferred_size()[0] - 1
-        user_input = nyx.curses.str_input(len(PROMPT) + self._x_offset, self.top + len(PROMPT_LINE[-page_height:]))
+        user_input = nyx.curses.str_input(len(PROMPT) + self._x_offset, self.top + len(PROMPT_LINE[-page_height:]), '', list(reversed(self._backlog)))
         user_input, is_done = user_input.strip(), False
 
         if not user_input:



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