commit 7e931e6047ab7e22ba8e08de58f54e7a687bd28d Author: Sambuddha Basu sambuddhabasu1@gmail.com Date: Sun Jun 19 18:16:41 2016 -0700
Interpreter panel supports GETINFO requests --- nyx/controller.py | 8 +++--- nyx/panel/interpreter.py | 72 ++++++++++++++++++++++++++++++++++++++++++++++++ nyx/panel/interpretor.py | 49 -------------------------------- nyx/tor_interpreter.py | 23 ++++++++++++++++ 4 files changed, 99 insertions(+), 53 deletions(-)
diff --git a/nyx/controller.py b/nyx/controller.py index c167c9a..54316a0 100644 --- a/nyx/controller.py +++ b/nyx/controller.py @@ -20,7 +20,7 @@ import nyx.panel.graph import nyx.panel.header import nyx.panel.log import nyx.panel.torrc -import nyx.panel.interpretor +import nyx.panel.interpreter
import stem
@@ -47,7 +47,7 @@ CONFIG = conf.config_dict('nyx', { 'features.panels.show.connection': True, 'features.panels.show.config': True, 'features.panels.show.torrc': True, - 'features.panels.show.interpretor': True, + 'features.panels.show.interpreter': True, 'features.redrawRate': 5, 'features.refreshRate': 5, 'features.confirmQuit': True, @@ -120,8 +120,8 @@ class Controller(object): if CONFIG['features.panels.show.torrc']: self._page_panels.append([nyx.panel.torrc.TorrcPanel()])
- if CONFIG['features.panels.show.interpretor']: - self._page_panels.append([nyx.panel.interpretor.InterpretorPanel()]) + if CONFIG['features.panels.show.interpreter']: + self._page_panels.append([nyx.panel.interpreter.InterpreterPanel()])
self.quit_signal = False self._page = 0 diff --git a/nyx/panel/interpreter.py b/nyx/panel/interpreter.py new file mode 100644 index 0000000..2769529 --- /dev/null +++ b/nyx/panel/interpreter.py @@ -0,0 +1,72 @@ +""" +Panel providing raw control port access with syntax hilighting, usage +information, tab completion, and other usability features. +""" + +import nyx.controller +import nyx.curses + +from nyx.curses import GREEN, CYAN, BOLD, HIGHLIGHT +from nyx.tor_interpreter import handle_query +from nyx import panel + + +USAGE_INFO = "to use this panel press enter" +PROMPT = ">>> " +PROMPT_LINE = [[(PROMPT, GREEN, BOLD), (USAGE_INFO, CYAN, BOLD)]] + +class InterpreterPanel(panel.Panel): + """ + Renders the interpreter panel with a prompt providing raw control port + access. + """ + + def __init__(self): + panel.Panel.__init__(self, 'interpreter') + + self._is_input_mode = False + + def key_handlers(self): + def _execute_command(): + self._is_input_mode = True + + while self._is_input_mode: + self.redraw(True) + user_input = nyx.curses.str_input(len(PROMPT), self.top + len(PROMPT_LINE)) + user_input, is_done = user_input.strip(), False + + if not user_input: + is_done = True + else: + input_entry, output_entry = handle_query(user_input) + input_entry.insert(0, (PROMPT, GREEN, BOLD)) + PROMPT_LINE.insert(len(PROMPT_LINE) - 1, input_entry) + for line in output_entry: + PROMPT_LINE.insert(len(PROMPT_LINE) - 1, [line]) + + if is_done: + self._is_input_mode = False + self.redraw(True) + + return ( + nyx.panel.KeyHandler('enter', 'execute a command', _execute_command, key_func = lambda key: key.is_selection()), + ) + + def draw(self, width, height): + usage_msg = " (enter "/help" for usage or a blank line to stop)" if self._is_input_mode else "" + self.addstr(0, 0, 'Control Interpreter%s:' % usage_msg, HIGHLIGHT) + + x_offset = 0 + draw_line = 1 + for entry in PROMPT_LINE: + cursor = x_offset + + msg, color, attr = None, None, None + for line in entry: + if len(line) == 2: + self.addstr(draw_line, cursor, line[0], line[1]) + elif len(line) == 3: + self.addstr(draw_line, cursor, line[0], line[1], line[2]) + cursor += len(line[0]) + + draw_line += 1 diff --git a/nyx/panel/interpretor.py b/nyx/panel/interpretor.py deleted file mode 100644 index 995490c..0000000 --- a/nyx/panel/interpretor.py +++ /dev/null @@ -1,49 +0,0 @@ -""" -Panel providing raw control port access with syntax hilighting, usage -information, tab completion, and other usability features. -""" - -import nyx.curses - -from nyx.curses import GREEN, CYAN, BOLD, HIGHLIGHT -from nyx import panel - - -USAGE_INFO = "to use this panel press enter" -PROMPT = ">>> " -PROMPT_LINE = [[(PROMPT, GREEN, BOLD), (USAGE_INFO, CYAN, BOLD)]] - -class InterpretorPanel(panel.Panel): - """ - Renders the current torrc or nyxrc with syntax highlighting in a scrollable - area. - """ - - def __init__(self): - panel.Panel.__init__(self, 'interpretor') - - self._is_input_mode = False - - def key_handlers(self): - def _execute_command(): - self._is_input_mode ^= True - self.redraw(True) - - return ( - nyx.panel.KeyHandler('enter', 'execute a command', _execute_command, key_func = lambda key: key.is_selection()), - ) - - def draw(self, width, height): - usage_msg = " (enter "/help" for usage or a blank line to stop)" if self._is_input_mode else "" - self.addstr(0, 0, 'Control Interpretor%s:' % usage_msg, HIGHLIGHT) - - x_offset = 0 - draw_line = 1 - for entry in PROMPT_LINE: - cursor = x_offset - - for msg, color, attr in entry: - self.addstr(draw_line, cursor, msg, color, attr) - cursor += len(msg) - - draw_line += 1 diff --git a/nyx/tor_interpreter.py b/nyx/tor_interpreter.py new file mode 100644 index 0000000..64d6554 --- /dev/null +++ b/nyx/tor_interpreter.py @@ -0,0 +1,23 @@ +from nyx.curses import GREEN, CYAN, BOLD, HIGHLIGHT +from nyx import tor_controller + + +def handle_query(user_input): + user_input = user_input.strip() + + input_entry, output_entry = [], [] + + if " " in user_input: cmd, arg = user_input.split(" ", 1) + else: cmd, arg = user_input, "" + + cmd = cmd.upper() + input_entry.append((cmd + " ", GREEN, BOLD)) + if arg: + input_entry.append((arg, CYAN, BOLD)) + + if cmd == "GETINFO": + resp = tor_controller().get_info(arg) + for line in resp.split('\n'): + output_entry.append((line, CYAN,)) + + return input_entry, output_entry