From 0ff1af3ee45a368d60cd1f5b37f3b23c9f482b5b Mon Sep 17 00:00:00 2001 From: Brikwerk Date: Tue, 7 Sep 2021 16:42:00 -0700 Subject: [PATCH] Improved remote tui error handling --- nxbt/tui.py | 30 ++++++++++++++++++++++++------ 1 file changed, 24 insertions(+), 6 deletions(-) diff --git a/nxbt/tui.py b/nxbt/tui.py index ea0cbe1..f34f8f4 100644 --- a/nxbt/tui.py +++ b/nxbt/tui.py @@ -264,13 +264,28 @@ class InputTUI(): "9": "ZR", } - def __init__(self, reconnect_target=None, debug=False, logfile=False): + def __init__(self, reconnect_target=None, debug=False, logfile=False, force_remote=False): self.reconnect_target = reconnect_target self.term = Terminal() - self.remote_connection = self.detect_remote_connection() + if force_remote: + self.remote_connection = True + else: + self.remote_connection = self.detect_remote_connection() self.controller = ControllerTUI(self.term) + # Check if direct connection will fail + if not self.remote_connection: + try: + from pynput import keyboard + except ImportError as e: + print("Unable to import pynput for direct input.") + print("If you're accessing NXBT over a remote shell, ", end="") + print("please use the 'remote_tui' option instead of 'tui'.") + print("The original pynput import is displayed below:\n") + print(e) + exit(1) + self.debug = debug self.logfile = logfile @@ -391,7 +406,7 @@ class InputTUI(): if len(term._keyboard_buf) > 1: term._keyboard_buf = deque([term._keyboard_buf.pop()]) - inp = term.inkey(1/60) + inp = term.inkey(1/66) pressed_key = None if inp.is_sequence: @@ -426,9 +441,6 @@ class InputTUI(): self.check_for_disconnect(term) def direct_input_loop(self, term): - - # pynput must be imported here since earlier imports - # will cause errors on remote connections from pynput import keyboard self.controller.toggle_auto_keypress_deactivation(False) @@ -610,6 +622,12 @@ class InputTUI(): print(term.bold_black_on_red(term.center(state.title()))) print(term.bold_black_on_red(term.center(""))) + if state == 'crashed': + time.sleep(3) + term.clear() + errors = self.nx.state[self.controller_index]["errors"] + raise ConnectionError(errors) + while True: inp = term.inkey(1/30) if inp == chr(113):