[tor-commits] [nyx/master] Make str_input() helpers private

atagar at torproject.org atagar at torproject.org
Sun Jul 31 23:32:40 UTC 2016


commit f7fb291641fbbcb1048ebcba59660d326df09be3
Author: Damian Johnson <atagar at torproject.org>
Date:   Mon Jul 18 09:24:56 2016 -0700

    Make str_input() helpers private
    
    They're internal helpers that are only used within this file. They shouldn't be
    public.
---
 nyx/controller.py |   2 +-
 nyx/curses.py     | 112 +++++++++++++++++++++++++++---------------------------
 test/subwindow.py |  30 +++++++--------
 3 files changed, 72 insertions(+), 72 deletions(-)

diff --git a/nyx/controller.py b/nyx/controller.py
index 54316a0..d02dbeb 100644
--- a/nyx/controller.py
+++ b/nyx/controller.py
@@ -18,9 +18,9 @@ import nyx.panel.config
 import nyx.panel.connection
 import nyx.panel.graph
 import nyx.panel.header
+import nyx.panel.interpreter
 import nyx.panel.log
 import nyx.panel.torrc
-import nyx.panel.interpreter
 
 import stem
 
diff --git a/nyx/curses.py b/nyx/curses.py
index 2bd9c06..3767cd3 100644
--- a/nyx/curses.py
+++ b/nyx/curses.py
@@ -14,9 +14,6 @@ if we want Windows support in the future too.
   start - initializes curses with the given function
   raw_screen - provides direct access to the curses screen
   key_input - get keypress by user
-  str_input_handle_key - helper function to display content in text field
-  str_input_handle_history_key - handle up/down arrow key in text field
-  str_input_handle_tab_completion - handle tab completion in text field
   str_input - text field where user can input a string
   curses_attr - curses encoded text attribute
   screen_size - provides the dimensions of our screen
@@ -250,7 +247,55 @@ def key_input(input_timeout = None):
   return KeyInput(CURSES_SCREEN.getch())
 
 
-def str_input_handle_key(textbox, key):
+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
+  this terminates and provides back **None**.
+
+  This blanks any content within the space that the input field is rendered
+  (otherwise stray characters would be interpreted as part of the initial
+  input).
+
+  :param int x: horizontal location
+  :param int y: vertical location
+  :param str initial_text: initial input of the field
+
+  :returns: **str** with the user input or **None** if the prompt is canceled
+  """
+
+  with CURSES_LOCK:
+    if HALT_ACTIVITY:
+      return None
+
+    try:
+      curses.curs_set(1)  # show cursor
+    except curses.error:
+      pass
+
+    width = screen_size().width - x
+
+    curses_subwindow = CURSES_SCREEN.subwin(1, width, y, x)
+    curses_subwindow.erase()
+    curses_subwindow.addstr(0, 0, initial_text[:width - 1])
+
+    textbox = curses.textpad.Textbox(curses_subwindow, insert_mode = True)
+    if tab_completion is not None:
+      user_input = textbox.edit(lambda key: _handle_tab_completion(textbox, key, backlog, tab_completion)).strip()
+    elif backlog is not None:
+      user_input = textbox.edit(lambda key: _handle_history_key(textbox, key, backlog)).strip()
+    else:
+      user_input = textbox.edit(lambda key: _handle_key(textbox, key)).strip()
+
+    try:
+      curses.curs_set(0)  # hide cursor
+    except curses.error:
+      pass
+
+    return None if textbox.lastcmd == curses.ascii.BEL else user_input
+
+
+def _handle_key(textbox, key):
   """
   Outputs the entered key onto the textbox.
 
@@ -259,6 +304,7 @@ def str_input_handle_key(textbox, key):
 
   :returns: **str** with the user input or **None** if the prompt is canceled
   """
+
   y, x = textbox.win.getyx()
 
   if key == 27:
@@ -282,7 +328,7 @@ def str_input_handle_key(textbox, key):
     return key
 
 
-def str_input_handle_history_key(textbox, key, backlog):
+def _handle_history_key(textbox, key, backlog):
   """
   Handles history validation. When the up/down arrow keys are pressed,
   the relative previous/next commands are shown.
@@ -294,7 +340,9 @@ def str_input_handle_history_key(textbox, key, backlog):
   :returns: **None** if up/down arrow key is pressed or calls function
     to write key to the textbox
   """
+
   global HISTORY_DICT
+
   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
@@ -322,10 +370,10 @@ def str_input_handle_history_key(textbox, key, backlog):
     HISTORY_DICT['selection_index'] = new_selection
     return None
 
-  return str_input_handle_key(textbox, key)
+  return _handle_key(textbox, key)
 
 
-def str_input_handle_tab_completion(textbox, key, backlog, tab_completion):
+def _handle_tab_completion(textbox, key, backlog, tab_completion):
   """
   Handles tab completion. If the tab key is pressed, the current textbox
   contents are checked for probable commands.
@@ -361,55 +409,7 @@ def str_input_handle_tab_completion(textbox, key, backlog, tab_completion):
 
     return None
 
-  return str_input_handle_history_key(textbox, key, backlog)
-
-
-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
-  this terminates and provides back **None**.
-
-  This blanks any content within the space that the input field is rendered
-  (otherwise stray characters would be interpreted as part of the initial
-  input).
-
-  :param int x: horizontal location
-  :param int y: vertical location
-  :param str initial_text: initial input of the field
-
-  :returns: **str** with the user input or **None** if the prompt is canceled
-  """
-
-  with CURSES_LOCK:
-    if HALT_ACTIVITY:
-      return None
-
-    try:
-      curses.curs_set(1)  # show cursor
-    except curses.error:
-      pass
-
-    width = screen_size().width - x
-
-    curses_subwindow = CURSES_SCREEN.subwin(1, width, y, x)
-    curses_subwindow.erase()
-    curses_subwindow.addstr(0, 0, initial_text[:width - 1])
-
-    textbox = curses.textpad.Textbox(curses_subwindow, insert_mode = True)
-    if tab_completion is not None:
-      user_input = textbox.edit(lambda key: str_input_handle_tab_completion(textbox, key, backlog, tab_completion)).strip()
-    elif backlog is not None:
-      user_input = textbox.edit(lambda key: str_input_handle_history_key(textbox, key, backlog)).strip()
-    else:
-      user_input = textbox.edit(lambda key: str_input_handle_key(textbox, key)).strip()
-
-    try:
-      curses.curs_set(0)  # hide cursor
-    except curses.error:
-      pass
-
-    return None if textbox.lastcmd == curses.ascii.BEL else user_input
+  return _handle_history_key(textbox, key, backlog)
 
 
 def curses_attr(*attributes):
diff --git a/test/subwindow.py b/test/subwindow.py
index 0f6f6a6..3f58278 100644
--- a/test/subwindow.py
+++ b/test/subwindow.py
@@ -123,18 +123,18 @@ class TestCurses(unittest.TestCase):
 
     self.assertEqual(EXPECTED_SCROLLBAR_BOTTOM, test.render(_draw).content.strip())
 
-  def test_str_input_handle_key(self):
+  def test_handle_key(self):
     dimensions = (40, 80)
 
     textbox = Mock()
     textbox.win.getyx.return_value = dimensions
-    self.assertEqual(curses.ascii.BEL, nyx.curses.str_input_handle_key(textbox, 27))
+    self.assertEqual(curses.ascii.BEL, nyx.curses._handle_key(textbox, 27))
 
     textbox = Mock()
     textbox.win.getyx.return_value = dimensions
     textbox.win.move = Mock()
     expected_call = call(dimensions[0], 0)
-    nyx.curses.str_input_handle_key(textbox, curses.KEY_HOME)
+    nyx.curses._handle_key(textbox, curses.KEY_HOME)
     self.assertTrue(textbox.win.move.called)
     self.assertEquals(expected_call, textbox.win.move.call_args)
 
@@ -143,34 +143,34 @@ class TestCurses(unittest.TestCase):
     textbox.gather.return_value = 'Sample Text'
     textbox.win.move = Mock()
     expected_call = call(*dimensions)
-    nyx.curses.str_input_handle_key(textbox, curses.KEY_RIGHT)
+    nyx.curses._handle_key(textbox, curses.KEY_RIGHT)
     self.assertTrue(textbox.win.move.called)
     self.assertEquals(expected_call, textbox.win.move.call_args)
 
     textbox = Mock()
     textbox.win.getyx.return_value = dimensions
-    self.assertEqual(curses.ascii.BEL, nyx.curses.str_input_handle_key(textbox, 410))
+    self.assertEqual(curses.ascii.BEL, nyx.curses._handle_key(textbox, 410))
 
     textbox = Mock()
     textbox.win.getyx.return_value = dimensions
     key_pressed = ord('a')
-    self.assertEqual(key_pressed, nyx.curses.str_input_handle_key(textbox, key_pressed))
+    self.assertEqual(key_pressed, nyx.curses._handle_key(textbox, key_pressed))
 
-  @patch('nyx.curses.str_input_handle_key')
-  def test_str_input_handle_history_key(self, mock_str_input_handle_key):
+  @patch('nyx.curses._handle_key')
+  def test_handle_history_key(self, mock_handle_key):
     backlog = ['GETINFO version']
     dimensions = (40, 80)
 
     textbox = Mock()
     textbox.win.getyx.return_value = dimensions
-    self.assertIsNone(nyx.curses.str_input_handle_history_key(textbox, curses.KEY_UP, []))
+    self.assertIsNone(nyx.curses._handle_history_key(textbox, curses.KEY_UP, []))
 
     textbox = Mock()
     textbox.win.getyx.return_value = dimensions
     textbox.win.getmaxyx.return_value = dimensions
     textbox.win.addstr = Mock()
     textbox.win.move = Mock()
-    nyx.curses.str_input_handle_history_key(textbox, curses.KEY_UP, backlog)
+    nyx.curses._handle_history_key(textbox, curses.KEY_UP, backlog)
     self.assertTrue(textbox.win.clear.called)
     expected_addstr_call = call(dimensions[0], 0, backlog[0])
     self.assertEqual(expected_addstr_call, textbox.win.addstr.call_args)
@@ -178,11 +178,11 @@ class TestCurses(unittest.TestCase):
     self.assertEqual(expected_move_call, textbox.win.move.call_args)
 
     textbox = Mock()
-    nyx.curses.str_input_handle_history_key(textbox, curses.KEY_LEFT, [])
-    self.assertTrue(mock_str_input_handle_key.called)
+    nyx.curses._handle_history_key(textbox, curses.KEY_LEFT, [])
+    self.assertTrue(mock_handle_key.called)
 
-  @patch('nyx.curses.str_input_handle_history_key')
-  def test_str_input_handle_tab_completion(self, mock_str_input_handle_history_key):
+  @patch('nyx.curses._handle_history_key')
+  def test_handle_tab_completion(self, mock_handle_history_key):
     dimensions = (40, 80)
     tab_completion_content = 'GETINFO version'
 
@@ -193,7 +193,7 @@ class TestCurses(unittest.TestCase):
     textbox.win.move = Mock()
     tab_completion = Mock()
     tab_completion.return_value = [tab_completion_content]
-    nyx.curses.str_input_handle_tab_completion(textbox, 9, [], tab_completion)
+    nyx.curses._handle_tab_completion(textbox, 9, [], tab_completion)
     self.assertTrue(textbox.win.clear.called)
     expected_addstr_call = call(dimensions[0], 0, tab_completion_content)
     self.assertEqual(expected_addstr_call, textbox.win.addstr.call_args)





More information about the tor-commits mailing list