commit 731cfe8745b8dc631497974888c38b8bdadf9ae7 Author: Damian Johnson atagar@torproject.org Date: Tue Mar 15 09:53:07 2016 -0700
Replace nyx.curses.setup() with start()
No reason not to have this replace curses.wrapper() too. This way we can drop direct curses usage from our starter as well. --- nyx/controller.py | 2 -- nyx/curses.py | 41 +++++++++++++++++++++++++++-------------- nyx/starter.py | 5 +---- 3 files changed, 28 insertions(+), 20 deletions(-)
diff --git a/nyx/controller.py b/nyx/controller.py index e3bb354..7b96134 100644 --- a/nyx/controller.py +++ b/nyx/controller.py @@ -410,8 +410,6 @@ def start_nyx(stdscr): for panel_impl in control.get_daemon_panels(): panel_impl.start()
- nyx.curses.setup(transparent_background = True, cursor = False) - # logs the initialization time
log.info('nyx started (initialization took %0.3f seconds)' % (time.time() - CONFIG['start_time'])) diff --git a/nyx/curses.py b/nyx/curses.py index fa2a77c..a2d0dfa 100644 --- a/nyx/curses.py +++ b/nyx/curses.py @@ -9,7 +9,7 @@ if we want Windows support in the future too. ::
curses_attr - curses encoded text attribute - setup - configures curses settings + start - initializes curses with the given function
is_color_supported - checks if terminal supports color output get_color_override - provides color we override requests with @@ -72,6 +72,8 @@ import stem.util.system
from nyx import msg, log
+CURSES_SCREEN = None + # Text colors and attributes. These are *very* commonly used so including # shorter aliases (so they can be referenced as just GREEN or BOLD).
@@ -152,25 +154,36 @@ def curses_attr(*attributes): return encoded
-def setup(transparent_background = False, cursor = True): +def start(function, transparent_background = False, cursor = True): """ - One time setup operations for configuring curses. + Starts a curses interface, delegating to the given function. The function + should accept a single argument for the curses screen.
+ :param funtion: function to invoke when curses starts :param bool transparent_background: allows background transparency :param bool cursor: makes cursor visible """
- if transparent_background: - try: - curses.use_default_colors() - except curses.error: - pass - - if not cursor: - try: - curses.curs_set(0) - except curses.error: - pass + def _wrapper(stdscr): + global CURSES_SCREEN + + CURSES_SCREEN = stdscr + + if transparent_background: + try: + curses.use_default_colors() + except curses.error: + pass + + if not cursor: + try: + curses.curs_set(0) + except curses.error: + pass + + function(stdscr) + + curses.wrapper(_wrapper)
def is_color_supported(): diff --git a/nyx/starter.py b/nyx/starter.py index 6697e21..09f5057 100644 --- a/nyx/starter.py +++ b/nyx/starter.py @@ -4,9 +4,6 @@ information. This starts the application, parsing arguments and getting a Tor connection. """
-from __future__ import absolute_import - -import curses import locale import logging import os @@ -86,7 +83,7 @@ def main(config): _set_process_name()
try: - curses.wrapper(nyx.controller.start_nyx) + nyx.curses.start(nyx.controller.start_nyx, transparent_background = True, cursor = False) except UnboundLocalError as exc: if os.environ['TERM'] != 'xterm': print(msg('setup.unknown_term', term = os.environ['TERM']))