commit 6017f4148bda6f8c5cb53373656afddce3f16c6b Author: Sambuddha Basu sambuddhabasu1@gmail.com Date: Sat Jul 2 10:43:58 2016 -0700
Added tab completion --- nyx/curses.py | 31 +++++++++++++++++++++++++++++-- nyx/panel/interpreter.py | 4 +++- 2 files changed, 32 insertions(+), 3 deletions(-)
diff --git a/nyx/curses.py b/nyx/curses.py index 21b7a42..48f015e 100644 --- a/nyx/curses.py +++ b/nyx/curses.py @@ -86,6 +86,7 @@ import collections import curses import curses.ascii import curses.textpad +import os import threading
import stem.util.conf @@ -244,7 +245,7 @@ def key_input(input_timeout = None): return KeyInput(CURSES_SCREEN.getch())
-def str_input(x, y, initial_text = '', backlog=None): +def str_input(x, y, initial_text = '', backlog=None, tab_completion=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 @@ -316,6 +317,30 @@ def str_input(x, y, initial_text = '', backlog=None):
return handle_key(textbox, key)
+ def handle_tab_completion(textbox, key): + if key == 9: + current_contents = textbox.gather().strip() + matches = tab_completion(current_contents) + new_input = None + + if len(matches) == 1: + new_input = matches[0] + elif len(matches) > 1: + common_prefix = os.path.commonprefix(matches) + if common_prefix != current_contents: + new_input = common_prefix + + if new_input: + 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)) + + return None + + return handle_history_key(textbox, key) + with CURSES_LOCK: if HALT_ACTIVITY: return None @@ -332,7 +357,9 @@ def str_input(x, y, initial_text = '', backlog=None): curses_subwindow.addstr(0, 0, initial_text[:width - 1])
textbox = curses.textpad.Textbox(curses_subwindow, insert_mode = True) - if backlog is not None: + if tab_completion is not None: + user_input = textbox.edit(lambda key: handle_tab_completion(textbox, key)).strip() + elif 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() diff --git a/nyx/panel/interpreter.py b/nyx/panel/interpreter.py index 8a48b34..e9e004c 100644 --- a/nyx/panel/interpreter.py +++ b/nyx/panel/interpreter.py @@ -17,6 +17,7 @@ from nyx import panel
import stem import stem.connection +import stem.interpreter.autocomplete import stem.interpreter.commands
@@ -79,6 +80,7 @@ class InterpreterPanel(panel.Panel): control_socket = '/var/run/tor/control', password_prompt = True, ) + self.autocompleter = stem.interpreter.autocomplete.Autocompleter(self.controller) self.interpreter = stem.interpreter.commands.ControlInterpretor(self.controller)
def key_handlers(self): @@ -96,7 +98,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:]), '', list(reversed(self._backlog))) + user_input = nyx.curses.str_input(len(PROMPT) + self._x_offset, self.top + len(PROMPT_LINE[-page_height:]), '', list(reversed(self._backlog)), self.autocompleter.matches) user_input, is_done = user_input.strip(), False
if not user_input:
tor-commits@lists.torproject.org