[tor-commits] [nyx/master] Interpreter panel limited to two text attr
atagar at torproject.org
atagar at torproject.org
Sun Jul 31 23:32:41 UTC 2016
commit b6f25bcbd988a4c97562a2c41c533e894b75a389
Author: Damian Johnson <atagar at torproject.org>
Date: Tue Jul 26 09:45:27 2016 -0700
Interpreter panel limited to two text attr
Why did we have this hardcoded size check? In practice it worked (we rarely
have more than one color and one attribute), but the limitation is pointless.
Changing our draw entries to be (text, (attr...)) tuples as we do everywhere
else. This is still far from nice code but getting there bit by bit...
---
nyx/panel/interpreter.py | 43 ++++++++++++++++---------------------------
test/panel/interpreter.py | 19 ++++++++++---------
2 files changed, 26 insertions(+), 36 deletions(-)
diff --git a/nyx/panel/interpreter.py b/nyx/panel/interpreter.py
index 1dcb0e8..137ab9b 100644
--- a/nyx/panel/interpreter.py
+++ b/nyx/panel/interpreter.py
@@ -19,13 +19,12 @@ import stem.interpreter.autocomplete
import stem.interpreter.commands
-USAGE_INFO = 'to use this panel press enter'
-PROMPT = '>>> '
BACKLOG_LIMIT = 100
+PROMPT = [('>>> ', (GREEN, BOLD)), ('to use this panel press enter', (CYAN, BOLD))]
def format_input(user_input):
- output = [(PROMPT, GREEN, BOLD)]
+ output = [('>>> ', (GREEN, BOLD))]
if ' ' in user_input:
cmd, arg = user_input.split(' ', 1)
@@ -33,11 +32,11 @@ def format_input(user_input):
cmd, arg = user_input, ''
if cmd.startswith('/'):
- output.append((user_input, MAGENTA, BOLD))
+ output.append((user_input, (MAGENTA, BOLD)))
else:
- output.append((cmd + ' ', GREEN, BOLD))
+ output.append((cmd + ' ', (GREEN, BOLD)))
if arg:
- output.append((arg, CYAN, BOLD))
+ output.append((arg, (CYAN, BOLD)))
return output
@@ -60,7 +59,7 @@ class InterpreterPanel(panel.Panel):
controller = tor_controller()
self.autocompleter = stem.interpreter.autocomplete.Autocompleter(controller)
self.interpreter = stem.interpreter.commands.ControlInterpretor(controller)
- self.prompt_line = [[(PROMPT, GREEN, BOLD), (USAGE_INFO, CYAN, BOLD)]]
+ self._lines = []
def key_handlers(self):
def _scroll(key):
@@ -77,7 +76,7 @@ class InterpreterPanel(panel.Panel):
self.redraw(True)
_scroll(nyx.curses.KeyInput(curses.KEY_END))
page_height = self.get_height() - 1
- user_input = nyx.curses.str_input(len(PROMPT) + self._x_offset, self.get_top() + len(self.prompt_line[-page_height:]), '', list(reversed(self._backlog)), self.autocompleter.matches)
+ user_input = nyx.curses.str_input(4 + self._x_offset, self.get_top() + max(len(self._lines[-page_height:]), 1), '', list(reversed(self._backlog)), self.autocompleter.matches)
user_input, is_done = user_input.strip(), False
if not user_input:
@@ -104,16 +103,12 @@ class InterpreterPanel(panel.Panel):
sys.stderr = old_stderr
response = '\x1b[31;1m' + new_stderr.getvalue()
sys.stderr = old_stderr
+
if response:
- self.prompt_line.insert(len(self.prompt_line) - 1, format_input(user_input))
+ self._lines.append(format_input(user_input))
for line in response.split('\n'):
- new_line = []
-
- for text, attr in nyx.curses.asci_to_curses(line):
- new_line.append([text] + list(attr))
-
- self.prompt_line.insert(len(self.prompt_line) - 1, new_line)
+ self._lines.append([(text, attr) for text, attr in nyx.curses.asci_to_curses(line)])
except stem.SocketClosed:
is_done = True
@@ -134,18 +129,12 @@ class InterpreterPanel(panel.Panel):
subwindow.scrollbar(1, scroll, self._last_content_height - 1)
y = 1 - scroll
- for entry in self.prompt_line:
- cursor = self._x_offset
-
- for line in entry:
- if len(line) == 2:
- subwindow.addstr(cursor, y, line[0], line[1])
- elif len(line) == 3:
- subwindow.addstr(cursor, y, line[0], line[1], line[2])
- try:
- cursor += len(line[0])
- except:
- pass
+
+ for line in self._lines + [PROMPT]:
+ x = self._x_offset
+
+ for text, attr in line:
+ x = subwindow.addstr(x, y, text, *attr)
y += 1
diff --git a/test/panel/interpreter.py b/test/panel/interpreter.py
index df42980..d036e32 100644
--- a/test/panel/interpreter.py
+++ b/test/panel/interpreter.py
@@ -24,6 +24,7 @@ EXPECTED_MULTILINE_PANEL = """
Control Interpreter:
>>> GETINFO version
250-version=0.2.4.27 (git-412e3f7dc9c6c01a)
+>>> to use this panel press enter
""".strip()
EXPECTED_SCROLLBAR_PANEL = ' |>>> to use this panel press enter'
@@ -34,21 +35,21 @@ class TestInterpreter(unittest.TestCase):
user_input = 'getinfo'
output = nyx.panel.interpreter.format_input(user_input)
self.assertEqual(2, len(output))
- self.assertEqual(('>>> ', 'Green', 'Bold'), output[0])
- self.assertEqual(('getinfo ', 'Green', 'Bold'), output[1])
+ self.assertEqual(('>>> ', ('Green', 'Bold')), output[0])
+ self.assertEqual(('getinfo ', ('Green', 'Bold')), output[1])
user_input = 'getinfo version'
output = nyx.panel.interpreter.format_input(user_input)
self.assertEqual(3, len(output))
- self.assertEqual(('>>> ', 'Green', 'Bold'), output[0])
- self.assertEqual(('getinfo ', 'Green', 'Bold'), output[1])
- self.assertEqual(('version', 'Cyan', 'Bold'), output[2])
+ self.assertEqual(('>>> ', ('Green', 'Bold')), output[0])
+ self.assertEqual(('getinfo ', ('Green', 'Bold')), output[1])
+ self.assertEqual(('version', ('Cyan', 'Bold')), output[2])
user_input = '/help'
output = nyx.panel.interpreter.format_input(user_input)
self.assertEqual(2, len(output))
- self.assertEqual(('>>> ', 'Green', 'Bold'), output[0])
- self.assertEqual(('/help', 'Magenta', 'Bold'), output[1])
+ self.assertEqual(('>>> ', ('Green', 'Bold')), output[0])
+ self.assertEqual(('/help', ('Magenta', 'Bold')), output[1])
@patch('nyx.panel.interpreter.tor_controller')
def test_rendering_panel(self, tor_controller_mock):
@@ -63,8 +64,8 @@ class TestInterpreter(unittest.TestCase):
def test_rendering_multiline_panel(self, tor_controller_mock):
tor_controller_mock()._handle_event = lambda event: None
panel = nyx.panel.interpreter.InterpreterPanel()
- panel.prompt_line = [[('>>> ', 'Green', 'Bold'), ('GETINFO', 'Green', 'Bold'), (' version', 'Cyan')]]
- panel.prompt_line.append([('250-version=0.2.4.27 (git-412e3f7dc9c6c01a)', 'Blue')])
+ panel._lines = [[('>>> ', ('Green', 'Bold')), ('GETINFO', ('Green', 'Bold')), (' version', ('Cyan',))]]
+ panel._lines.append([('250-version=0.2.4.27 (git-412e3f7dc9c6c01a)', ('Blue',))])
self.assertEqual(EXPECTED_MULTILINE_PANEL, test.render(panel._draw).content)
@patch('nyx.panel.interpreter.tor_controller')
More information about the tor-commits
mailing list