def suppress_stderr(suppress=True): """ It is used in completion to avoid running prompt-toolkit nestedly """ global SUPPRESS_STDERR OLD_SUPPRESS_STDERR = SUPPRESS_STDERR SUPPRESS_STDERR = suppress try: yield finally: console.flush() SUPPRESS_STDERR = OLD_SUPPRESS_STDERR
def native_read_console(): """ It is used in completion to avoid running prompt-toolkit nestedly """ global CALLING_FROM_PROMPT global SUPPRESS_STDERR CALLING_FROM_PROMPT = True SUPPRESS_STDERR = True try: yield finally: console.flush() CALLING_FROM_PROMPT = False SUPPRESS_STDERR = False
def read_console(message, add_history=1): if CALLING_FROM_PROMPT: # fallback to `input` if `read_console` is called nestedly # c.f. run_coroutine_in_terminal of prompt_toolkit global SUPPRESS_STDERR OLD_SUPPRESS_STDERR = SUPPRESS_STDERR SUPPRESS_STDERR = False app = session.app console.flush() app.output.flush() app._running_in_terminal = True try: with app.input.detach(): with app.input.cooked_mode(): return ask_input(message) finally: SUPPRESS_STDERR = OLD_SUPPRESS_STDERR app._running_in_terminal = False app.renderer.reset() app._request_absolute_cursor_position() app._redraw() session.prompt_text = message activated = False for name in reversed(session.modes): mode = session.modes[name] if mode.activator and mode.activator(session): session.activate_mode(name) activated = True break if not activated: session.activate_mode("unknown") current_mode = session.current_mode if interrupted[0]: interrupted[0] = False elif not TERMINAL_CURSOR_AT_BEGINNING[0] or \ (settings.insert_new_line and current_mode.insert_new_line): session.app.output.write_raw("\n") text = None while text is None: try: text = session.prompt(add_history=add_history) except KeyboardInterrupt: interrupted[0] = True raise except Exception as e: if isinstance(e, EOFError): # todo: confirmation in "r" mode return None else: print("unexpected error was caught.") print( "please report to https://github.com/randy3k/radian for such error." ) print(e) import traceback traceback.print_exc() import os os._exit(1) current_mode = session.current_mode if current_mode.on_post_accept: result = current_mode.on_post_accept(session) if result is not None: return result if settings.insert_new_line and current_mode.insert_new_line: session.app.output.write_raw("\n") text = None return text