def create_output(stdout: Optional[TextIO] = None) -> Output: """ Return an :class:`~prompt_toolkit.output.Output` instance for the command line. :param stdout: The stdout object """ if stdout is None: # By default, render to stdout. If the output is piped somewhere else, # render to stderr. The prompt_toolkit render output is not meant to be # consumed by something other then a terminal, so this is a reasonable # default. if sys.stdout.isatty(): stdout = sys.stdout else: stdout = sys.stderr if is_windows(): from .conemu import ConEmuOutput from .win32 import Win32Output from .windows10 import is_win_vt100_enabled, Windows10_Output if is_win_vt100_enabled(): return cast(Output, Windows10_Output(stdout)) if is_conemu_ansi(): return cast(Output, ConEmuOutput(stdout)) else: return Win32Output(stdout) else: from .vt100 import Vt100_Output return Vt100_Output.from_pty(stdout, term=get_term_environment_variable())
def create_output( stdout: Optional[TextIO] = None, always_prefer_tty: bool = True ) -> Output: """ Return an :class:`~prompt_toolkit.output.Output` instance for the command line. :param stdout: The stdout object :param always_prefer_tty: When set, look for `sys.stderr` if `sys.stdout` is not a TTY. (The prompt_toolkit render output is not meant to be consumed by something other then a terminal, so this is a reasonable default.) """ # Consider TERM and PROMPT_TOOLKIT_COLOR_DEPTH environment variables. # Notice that PROMPT_TOOLKIT_COLOR_DEPTH value is the default that's used # if the Application doesn't override it. term_from_env = get_term_environment_variable() color_depth_from_env = ColorDepth.from_env() if stdout is None: # By default, render to stdout. If the output is piped somewhere else, # render to stderr. stdout = sys.stdout if always_prefer_tty: for io in [sys.stdout, sys.stderr]: if io.isatty(): stdout = io break # If the patch_stdout context manager has been used, then sys.stdout is # replaced by this proxy. For prompt_toolkit applications, we want to use # the real stdout. from prompt_toolkit.patch_stdout import StdoutProxy while isinstance(stdout, StdoutProxy): stdout = stdout.original_stdout if is_windows(): from .conemu import ConEmuOutput from .win32 import Win32Output from .windows10 import Windows10_Output, is_win_vt100_enabled if is_win_vt100_enabled(): return cast( Output, Windows10_Output(stdout, default_color_depth=color_depth_from_env), ) if is_conemu_ansi(): return cast( Output, ConEmuOutput(stdout, default_color_depth=color_depth_from_env) ) else: return Win32Output(stdout, default_color_depth=color_depth_from_env) else: from .vt100 import Vt100_Output return Vt100_Output.from_pty( stdout, term=term_from_env, default_color_depth=color_depth_from_env )
def create_radian_prompt_session(options): history_file = ".radian_history" if options.no_history: history = ModalInMemoryHistory() elif not options.global_history and os.path.exists(history_file): history = ModalFileHistory(os.path.abspath(history_file)) else: history = ModalFileHistory( os.path.join(os.path.expanduser("~"), history_file)) if is_windows(): output = None else: output = CustomVt100Output.from_pty( sys.stdout, term=get_term_environment_variable()) def get_inputhook(): terminal_width = [None] def _(context): while True: if context.input_is_ready(): break try: process_events() except Exception: pass output_width = session.app.output.get_size().columns if output_width and terminal_width[0] != output_width: terminal_width[0] = output_width setoption("width", max(terminal_width[0], 20)) time.sleep(1.0 / 30) return _ session = ModalPromptSession( color_depth=ColorDepth.default(term=os.environ.get("TERM")), history=history, enable_history_search=True, enable_suspend=True, tempfile_suffix=".R", input=CustomVt100Input(sys.stdin) if not is_windows() else None, output=output, inputhook=get_inputhook(), mode_class=RadianMode) load_settings(session) register_modes(session) return session
def create_output(stdout: Optional[TextIO] = None, always_prefer_tty: bool = True) -> Output: """ Return an :class:`~prompt_toolkit.output.Output` instance for the command line. :param stdout: The stdout object :param always_prefer_tty: When set, look for `sys.stderr` if `sys.stdout` is not a TTY. (The prompt_toolkit render output is not meant to be consumed by something other then a terminal, so this is a reasonable default.) """ if stdout is None: # By default, render to stdout. If the output is piped somewhere else, # render to stderr. stdout = sys.stdout if always_prefer_tty: for io in [sys.stdout, sys.stderr]: if io.isatty(): stdout = io break # If the patch_stdout context manager has been used, then sys.stdout is # replaced by this proxy. For prompt_toolkit applications, we want to use # the real stdout. while isinstance(stdout, StdoutProxy): stdout = stdout.original_stdout if is_windows(): from .conemu import ConEmuOutput from .win32 import Win32Output from .windows10 import is_win_vt100_enabled, Windows10_Output if is_win_vt100_enabled(): return cast(Output, Windows10_Output(stdout)) if is_conemu_ansi(): return cast(Output, ConEmuOutput(stdout)) else: return Win32Output(stdout) else: from .vt100 import Vt100_Output return Vt100_Output.from_pty(stdout, term=get_term_environment_variable())
def create_output(stdout=None): """ Return an :class:`~prompt_toolkit.output.Output` instance for the command line. :param stdout: The stdout object """ stdout = stdout or sys.__stdout__ if is_windows(): from .conemu import ConEmuOutput from .win32 import Win32Output from .windows10 import is_win_vt100_enabled, Windows10_Output if is_win_vt100_enabled(): return Windows10_Output(stdout) if is_conemu_ansi(): return ConEmuOutput(stdout) else: return Win32Output(stdout) else: from .vt100 import Vt100_Output return Vt100_Output.from_pty(stdout, term=get_term_environment_variable())
def create_output(stdout=None): """ Return an :class:`~prompt_toolkit.output.Output` instance for the command line. :param stdout: The stdout object """ stdout = stdout or sys.__stdout__ if is_windows(): from .conemu import ConEmuOutput from .win32 import Win32Output from .windows10 import is_win_vt100_enabled, Windows10_Output if is_win_vt100_enabled(): return Windows10_Output(stdout) if is_conemu_ansi(): return ConEmuOutput(stdout) else: return Win32Output(stdout) else: from .vt100 import Vt100_Output return Vt100_Output.from_pty( stdout, term=get_term_environment_variable())
def create_radian_prompt_session(options, settings): history_file = ".radian_history" if options.no_history: history = ModalInMemoryHistory() elif not options.global_history and os.path.exists(history_file): history = ModalFileHistory(os.path.abspath(history_file)) else: history = ModalFileHistory( os.path.join(os.path.expanduser("~"), history_file)) if is_windows(): output = None else: output = CustomVt100Output.from_pty( sys.stdout, term=get_term_environment_variable()) def get_inputhook(): terminal_width = [None] def _(context): while True: if context.input_is_ready(): break try: process_events() except Exception: pass output_width = session.app.output.get_size().columns if output_width and terminal_width[0] != output_width: terminal_width[0] = output_width setoption("width", max(terminal_width[0], 20)) time.sleep(1.0 / 30) return _ def vi_mode_prompt(): if session.editing_mode.lower( ) == "vi" and settings.show_vi_mode_prompt: return settings.vi_mode_prompt.format( str(session.app.vi_state.input_mode)[3:6]) return "" def message(): if hasattr(session.current_mode, "get_message"): return ANSI(vi_mode_prompt() + session.current_mode.get_message()) elif hasattr(session.current_mode, "message"): message = session.current_mode.message if callable(message): return ANSI(vi_mode_prompt() + message()) else: return ANSI(vi_mode_prompt() + message) else: return "" session = ModalPromptSession( message=message, color_depth=ColorDepth.default(term=os.environ.get("TERM")), style=style_from_pygments_cls(get_style_by_name( settings.color_scheme)), editing_mode="VI" if settings.editing_mode in ["vim", "vi"] else "EMACS", history=history, enable_history_search=True, history_search_no_duplicates=settings.history_search_no_duplicates, search_ignore_case=settings.history_search_ignore_case, enable_suspend=True, tempfile_suffix=".R", input=CustomVt100Input(sys.stdin) if not is_windows() else None, output=output, inputhook=get_inputhook(), mode_class=RadianMode) apply_settings(session, settings) def browse_activator(session): message = session.prompt_text if BROWSE_PATTERN.match(message): session.browse_level = BROWSE_PATTERN.match(message).group(1) return True else: return False def browse_on_pre_accept(session): if session.default_buffer.text.strip() in [ "n", "s", "f", "c", "cont", "Q", "where", "help" ]: session.add_history = False def shell_process_text(session): text = session.default_buffer.text shell.run_command(text) input_processors = [] if settings.highlight_matching_bracket: input_processors.append(HighlightMatchingBracketProcessor()) session.register_mode( "r", activator=lambda session: session.prompt_text == settings.prompt, insert_new_line=True, history_share_with="browse", get_message=lambda: settings.prompt, multiline=settings.indent_lines, complete_while_typing=settings.complete_while_typing, lexer=PygmentsLexer(SLexer), completer=RCompleter(timeout=settings.completion_timeout), key_bindings=create_key_bindings(), input_processors=input_processors, prompt_key_bindings=create_r_key_bindings(prase_text_complete)) session.register_mode("shell", on_post_accept=shell_process_text, insert_new_line=True, get_message=lambda: settings.shell_prompt, multiline=settings.indent_lines, complete_while_typing=settings.complete_while_typing, lexer=None, completer=SmartPathCompleter(), prompt_key_bindings=create_shell_key_bindings()) session.register_mode( "browse", activator=browse_activator, # on_pre_accept=browse_on_pre_accept, # disable insert_new_line=True, history_share_with="r", get_message=lambda: settings.browse_prompt.format(session.browse_level ), multiline=settings.indent_lines, complete_while_typing=True, lexer=PygmentsLexer(SLexer), completer=RCompleter(timeout=settings.completion_timeout), input_processors=input_processors, prompt_key_bindings=create_r_key_bindings(prase_text_complete), switchable_from=False, switchable_to=False) session.register_mode("unknown", insert_new_line=False, get_message=lambda: session.prompt_text, complete_while_typing=False, lexer=None, completer=None, prompt_key_bindings=None, switchable_from=False, switchable_to=False, input_processors=[]) return session
def create_output(stdout: Optional[TextIO] = None, always_prefer_tty: bool = False) -> Output: """ Return an :class:`~prompt_toolkit.output.Output` instance for the command line. :param stdout: The stdout object :param always_prefer_tty: When set, look for `sys.stderr` if `sys.stdout` is not a TTY. Useful if `sys.stdout` is redirected to a file, but we still want user input and output on the terminal. By default, this is `False`. If `sys.stdout` is not a terminal (maybe it's redirected to a file), then a `PlainTextOutput` will be returned. That way, tools like `print_formatted_text` will write plain text into that file. """ # Consider TERM, PROMPT_TOOLKIT_BELL, and PROMPT_TOOLKIT_COLOR_DEPTH # environment variables. Notice that PROMPT_TOOLKIT_COLOR_DEPTH value is # the default that's used if the Application doesn't override it. term_from_env = get_term_environment_variable() bell_from_env = get_bell_environment_variable() color_depth_from_env = ColorDepth.from_env() if stdout is None: # By default, render to stdout. If the output is piped somewhere else, # render to stderr. stdout = sys.stdout if always_prefer_tty: for io in [sys.stdout, sys.stderr]: if io is not None and io.isatty(): # (This is `None` when using `pythonw.exe` on Windows.) stdout = io break # If the output is still `None`, use a DummyOutput. # This happens for instance on Windows, when running the application under # `pythonw.exe`. In that case, there won't be a terminal Window, and # stdin/stdout/stderr are `None`. if stdout is None: return DummyOutput() # If the patch_stdout context manager has been used, then sys.stdout is # replaced by this proxy. For prompt_toolkit applications, we want to use # the real stdout. from prompt_toolkit.patch_stdout import StdoutProxy while isinstance(stdout, StdoutProxy): stdout = stdout.original_stdout if is_windows(): from .conemu import ConEmuOutput from .win32 import Win32Output from .windows10 import Windows10_Output, is_win_vt100_enabled if is_win_vt100_enabled(): return cast( Output, Windows10_Output(stdout, default_color_depth=color_depth_from_env), ) if is_conemu_ansi(): return cast( Output, ConEmuOutput(stdout, default_color_depth=color_depth_from_env)) else: return Win32Output(stdout, default_color_depth=color_depth_from_env) else: from .vt100 import Vt100_Output # Stdout is not a TTY? Render as plain text. # This is mostly useful if stdout is redirected to a file, and # `print_formatted_text` is used. if not stdout.isatty(): return PlainTextOutput(stdout) return Vt100_Output.from_pty( stdout, term=term_from_env, default_color_depth=color_depth_from_env, enable_bell=bell_from_env, )