commit bbf5199938d7f0b9beb7fb2bd2091fc9b11fcb52 Author: Damian Johnson atagar@torproject.org Date: Sun Mar 13 19:16:52 2016 -0700
Merge KeyInput into nyx.curses
Simple class that clearly belongs in nyx.curses (not the panel). --- nyx/controller.py | 4 ++-- nyx/curses.py | 63 ++++++++++++++++++++++++++++++++++++++++++++++++- nyx/panel/__init__.py | 56 ------------------------------------------- nyx/panel/connection.py | 4 ++-- nyx/popups.py | 2 +- 5 files changed, 67 insertions(+), 62 deletions(-)
diff --git a/nyx/controller.py b/nyx/controller.py index c3b9146..9a75f60 100644 --- a/nyx/controller.py +++ b/nyx/controller.py @@ -151,7 +151,7 @@ class Controller: Gets keystroke from the user. """
- return nyx.panel.KeyInput(self.get_screen().getch()) + return nyx.curses.KeyInput(self.get_screen().getch())
def get_page_count(self): """ @@ -444,7 +444,7 @@ def start_nyx(stdscr): key, override_key = override_key, None else: curses.halfdelay(CONFIG['features.redrawRate'] * 10) - key = nyx.panel.KeyInput(stdscr.getch()) + key = nyx.curses.KeyInput(stdscr.getch())
if key.match('right'): control.next_page() diff --git a/nyx/curses.py b/nyx/curses.py index 4e32fd9..18da7f2 100644 --- a/nyx/curses.py +++ b/nyx/curses.py @@ -17,6 +17,11 @@ if we want Windows support in the future too. disable_acs - renders replacements for ACS characters is_wide_characters_supported - checks if curses supports wide character
+ KeyInput - user keyboard input + |- match - checks if this matches the given inputs + |- is_scroll - true if key is used for scrolling + +- is_selection - true if key should trigger selection + Scroller - scrolls content with keyboard navigation |- location - present scroll location +- handle_key - moves scroll based on user input @@ -96,6 +101,20 @@ CURSES_ATTRIBUTES = { DEFAULT_COLOR_ATTR = dict([(color, 0) for color in Color]) COLOR_ATTR = None
+SCROLL_KEYS = (curses.KEY_UP, curses.KEY_DOWN, curses.KEY_PPAGE, curses.KEY_NPAGE, curses.KEY_HOME, curses.KEY_END) + +SPECIAL_KEYS = { + 'up': curses.KEY_UP, + 'down': curses.KEY_DOWN, + 'left': curses.KEY_LEFT, + 'right': curses.KEY_RIGHT, + 'home': curses.KEY_HOME, + 'end': curses.KEY_END, + 'page_up': curses.KEY_PPAGE, + 'page_down': curses.KEY_NPAGE, + 'esc': 27, +} +
def conf_handler(key, value): if key == 'features.colorOverride': @@ -261,6 +280,48 @@ def is_wide_characters_supported(): return False
+class KeyInput(object): + """ + Keyboard input by the user. + """ + + def __init__(self, key): + self._key = key # pressed key as an integer + + def match(self, *keys): + """ + Checks if we have a case insensitive match with the given key. Beside + characters, this also recognizes: up, down, left, right, home, end, + page_up, page_down, and esc. + """ + + for key in keys: + if key in SPECIAL_KEYS: + if self._key == SPECIAL_KEYS[key]: + return True + elif len(key) == 1: + if self._key in (ord(key.lower()), ord(key.upper())): + return True + else: + raise ValueError("%s wasn't among our recognized key codes" % key) + + return False + + def is_scroll(self): + """ + True if the key is used for scrolling, false otherwise. + """ + + return self._key in SCROLL_KEYS + + def is_selection(self): + """ + True if the key matches the enter or space keys. + """ + + return self._key in (curses.KEY_ENTER, 10, ord(' ')) + + class Scroller(object): """ Simple scroller that provides keyboard navigation of content. @@ -296,7 +357,7 @@ class Scroller(object): * page up / page down - scrolls by the page_height * home / end - moves to the top or bottom
- :param nyx.util.panel.KeyInput key: pressed key + :param nyx.curses.KeyInput key: pressed key :param int content_height: height of the content being renered :param int page_height: height visible on the page
diff --git a/nyx/panel/__init__.py b/nyx/panel/__init__.py index 0bbde97..0394d55 100644 --- a/nyx/panel/__init__.py +++ b/nyx/panel/__init__.py @@ -20,20 +20,6 @@ from stem.util import conf, str_tools
CURSES_LOCK = RLock()
-SCROLL_KEYS = (curses.KEY_UP, curses.KEY_DOWN, curses.KEY_PPAGE, curses.KEY_NPAGE, curses.KEY_HOME, curses.KEY_END) - -SPECIAL_KEYS = { - 'up': curses.KEY_UP, - 'down': curses.KEY_DOWN, - 'left': curses.KEY_LEFT, - 'right': curses.KEY_RIGHT, - 'home': curses.KEY_HOME, - 'end': curses.KEY_END, - 'page_up': curses.KEY_PPAGE, - 'page_down': curses.KEY_NPAGE, - 'esc': 27, -} - PASS = -1
__all__ = [ @@ -806,45 +792,3 @@ class Panel(object): self.addch(top, left + width - 1, curses.ACS_URCORNER, *attributes) self.addch(top + height - 1, left, curses.ACS_LLCORNER, *attributes) self.addch(top + height - 1, left + width - 1, curses.ACS_LRCORNER, *attributes) - - -class KeyInput(object): - """ - Keyboard input by the user. - """ - - def __init__(self, key): - self._key = key # pressed key as an integer - - def match(self, *keys): - """ - Checks if we have a case insensitive match with the given key. Beside - characters, this also recognizes: up, down, left, right, home, end, - page_up, page_down, and esc. - """ - - for key in keys: - if key in SPECIAL_KEYS: - if self._key == SPECIAL_KEYS[key]: - return True - elif len(key) == 1: - if self._key in (ord(key.lower()), ord(key.upper())): - return True - else: - raise ValueError("%s wasn't among our recognized key codes" % key) - - return False - - def is_scroll(self): - """ - True if the key is used for scrolling, false otherwise. - """ - - return self._key in SCROLL_KEYS - - def is_selection(self): - """ - True if the key matches the enter or space keys. - """ - - return self._key in (curses.KEY_ENTER, 10, ord(' ')) diff --git a/nyx/panel/connection.py b/nyx/panel/connection.py index 1f6d820..7e19a02 100644 --- a/nyx/panel/connection.py +++ b/nyx/panel/connection.py @@ -364,9 +364,9 @@ class ConnectionPanel(nyx.panel.Panel, threading.Thread): if not key or key.is_selection() or key.match('d'): break # closes popup elif key.match('left'): - self.handle_key(nyx.panel.KeyInput(curses.KEY_UP)) + self.handle_key(nyx.curses.KeyInput(curses.KEY_UP)) elif key.match('right'): - self.handle_key(nyx.panel.KeyInput(curses.KEY_DOWN)) + self.handle_key(nyx.curses.KeyInput(curses.KEY_DOWN))
self.set_title_visible(True) self.redraw(True) diff --git a/nyx/popups.py b/nyx/popups.py index d594409..70db69a 100644 --- a/nyx/popups.py +++ b/nyx/popups.py @@ -445,7 +445,7 @@ def show_descriptor_popup(fingerprint, color, max_width, is_close_key): :param function is_close_key: method to indicate if a key should close the dialog or not
- :returns: :class:`~nyx.panel.KeyInput` for the keyboard input that + :returns: :class:`~nyx.curses.KeyInput` for the keyboard input that closed the dialog """