[tor-commits] [nyx/master] Perform cbreak in key_input()

atagar at torproject.org atagar at torproject.org
Tue Mar 15 17:05:10 UTC 2016


commit b552c4ef7f8a1a64378652c785cef0d8e04c93b0
Author: Damian Johnson <atagar at torproject.org>
Date:   Tue Mar 15 08:58:29 2016 -0700

    Perform cbreak in key_input()
    
    Our cbreak and halfdelay modes are used to determine if we time out requests
    for user input...
    
      https://www.ibm.com/support/knowledgecenter/ssw_aix_53/com.ibm.aix.genprogc/doc/genprogc/understanding_terminals_wcurses.htm%23a55c21c8e
    
    As such we might as well have this live in the one spot we call the curses
    getch() method. This was only scattered around due to old copy-pasted code.
---
 nyx/controller.py  | 12 +++++++++---
 nyx/menu/menu.py   |  5 -----
 nyx/panel/graph.py |  2 --
 nyx/popups.py      | 20 +++-----------------
 4 files changed, 12 insertions(+), 27 deletions(-)

diff --git a/nyx/controller.py b/nyx/controller.py
index 54508aa..e3bb354 100644
--- a/nyx/controller.py
+++ b/nyx/controller.py
@@ -146,11 +146,18 @@ class Controller:
 
     return self._screen
 
-  def key_input(self):
+  def key_input(self, input_timeout = None):
     """
     Gets keystroke from the user.
+
+    :param int input_timeout: duration in seconds to wait for user input
     """
 
+    if input_timeout:
+      curses.halfdelay(input_timeout * 10)
+    else:
+      curses.cbreak()  # wait indefinitely for key presses (no timeout)
+
     return nyx.curses.KeyInput(self.get_screen().getch())
 
   def get_page_count(self):
@@ -431,8 +438,7 @@ def start_nyx(stdscr):
     if override_key:
       key, override_key = override_key, None
     else:
-      curses.halfdelay(CONFIG['features.redrawRate'] * 10)
-      key = nyx.curses.KeyInput(stdscr.getch())
+      key = control.key_input(CONFIG['features.redrawRate'])
 
     if key.match('right'):
       control.next_page()
diff --git a/nyx/menu/menu.py b/nyx/menu/menu.py
index 1deab02..7d49b3e 100644
--- a/nyx/menu/menu.py
+++ b/nyx/menu/menu.py
@@ -2,10 +2,6 @@
 Display logic for presenting the menu.
 """
 
-from __future__ import absolute_import
-
-import curses
-
 import nyx.curses
 import nyx.popups
 import nyx.controller
@@ -123,7 +119,6 @@ def show_menu():
 
         popup.win.refresh()
 
-        curses.cbreak()
         cursor.handle_key(control.key_input())
 
         # redraws the rest of the interface if we're rendering on it again
diff --git a/nyx/panel/graph.py b/nyx/panel/graph.py
index 818769c..2b3714e 100644
--- a/nyx/panel/graph.py
+++ b/nyx/panel/graph.py
@@ -13,7 +13,6 @@ Downloaded (0.0 B/sec):           Uploaded (0.0 B/sec):
 
 import collections
 import copy
-import curses
 import time
 
 import nyx.controller
@@ -481,7 +480,6 @@ class GraphPanel(nyx.panel.Panel):
         while True:
           msg = 'press the down/up to resize the graph, and enter when done'
           control.set_msg(msg, BOLD, True)
-          curses.cbreak()  # TODO: can we drop this?
           key = control.key_input()
 
           if key.match('down'):
diff --git a/nyx/popups.py b/nyx/popups.py
index 70db69a..e6df000 100644
--- a/nyx/popups.py
+++ b/nyx/popups.py
@@ -2,10 +2,7 @@
 Functions for displaying popups in the interface.
 """
 
-from __future__ import absolute_import
-
 import math
-import curses
 import operator
 
 import nyx.controller
@@ -99,14 +96,14 @@ def input_prompt(msg, initial_value = ''):
     return user_input
 
 
-def show_msg(msg, max_wait = -1, attr = HIGHLIGHT):
+def show_msg(msg, max_wait = None, attr = HIGHLIGHT):
   """
   Displays a single line message on the control line for a set time. Pressing
   any key will end the message. This returns the key pressed.
 
   Arguments:
     msg     - message to be displayed to the user
-    max_wait - time to show the message, indefinite if -1
+    max_wait - time to show the message, indefinite if None
     attr    - attributes with which to draw the message
   """
 
@@ -114,12 +111,7 @@ def show_msg(msg, max_wait = -1, attr = HIGHLIGHT):
     control = nyx.controller.get_controller()
     control.set_msg(msg, attr, True)
 
-    if max_wait == -1:
-      curses.cbreak()
-    else:
-      curses.halfdelay(max_wait * 10)
-
-    key_press = control.key_input()
+    key_press = control.key_input(max_wait)
     control.set_msg()
     return key_press
 
@@ -184,7 +176,6 @@ def show_help_popup():
         popup.addstr(7, 2, 'Press any key...')
 
       popup.win.refresh()
-      curses.cbreak()
       exit_key = control.key_input()
 
   if not exit_key.is_selection() and not exit_key.is_scroll() and \
@@ -212,7 +203,6 @@ def show_about_popup():
       popup.addstr(7, 2, 'Press any key...')
       popup.win.refresh()
 
-      curses.cbreak()
       control.key_input()
 
 
@@ -260,7 +250,6 @@ def show_count_dialog(title, counts):
     popup.addstr(0, 0, title, HIGHLIGHT)
     popup.win.refresh()
 
-    curses.cbreak()
     nyx.controller.get_controller().key_input()
 
 
@@ -288,7 +277,6 @@ def show_sort_dialog(title, options, old_selection, option_colors):
     if popup:
       new_selections = []  # new ordering
       cursor_location = 0     # index of highlighted option
-      curses.cbreak()         # wait indefinitely for key presses (no timeout)
 
       selection_options = list(options)
       selection_options.append('Cancel')
@@ -403,8 +391,6 @@ def show_menu(title, options, old_selection):
     top_panel.set_title_visible(False)
     top_panel.redraw(True)
 
-    curses.cbreak()   # wait indefinitely for key presses (no timeout)
-
     while True:
       popup.win.erase()
       popup.win.box()





More information about the tor-commits mailing list