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_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
from __future__ import unicode_literals import sys from lineedit import ModalPromptSession from prompt_toolkit.key_binding.key_bindings import KeyBindings from prompt_toolkit.enums import DEFAULT_BUFFER from prompt_toolkit.filters import has_focus session = ModalPromptSession() default_focused = has_focus(DEFAULT_BUFFER) pkb = KeyBindings() @pkb.add('q', filter=default_focused) def _(event): session.change_mode("q") qkb = KeyBindings() @qkb.add('p', filter=default_focused) def _(event): session.change_mode("p") rkb = KeyBindings() @rkb.add('p', filter=default_focused) def _(event): session.change_mode("p") @rkb.add('q', filter=default_focused) def _(event): session.change_mode("q")
from __future__ import unicode_literals import sys from lineedit import ModalPromptSession session = ModalPromptSession() session.register_mode( "p", message=lambda: "p> ", history_share_with=lambda m: m in ["q"]) session.register_mode( "q", message=lambda: "q> ", history_share_with=lambda m: m in ["p"]) print("Enter [p/q] to change mode:") while True: try: text = session.prompt() session.activate_mode(text) except EOFError: sys.exit(0) except KeyboardInterrupt: pass except Exception: pass
from __future__ import unicode_literals import sys from lineedit import ModalPromptSession session = ModalPromptSession() session.register_mode("p", message=lambda: "p> ") session.register_mode("q", message=lambda: "q> ") print("Enter [p/q] to change mode:") while True: try: text = session.prompt() session.activate_mode(text) except EOFError: sys.exit(0) except KeyboardInterrupt: pass except Exception: pass