def run_standalone(self, color_depth): """ Run pymux standalone, rather than using a client/server architecture. This is mainly useful for debugging. """ self._runs_standalone = True self._start_auto_refresh_thread() client_state = self.add_client(input=create_input(), output=create_output(stdout=sys.stdout), color_depth=color_depth, connection=None) client_state.app.run()
def run_standalone(self, color_depth): """ Run pymux standalone, rather than using a client/server architecture. This is mainly useful for debugging. """ self._runs_standalone = True self._start_auto_refresh_thread() client_state = self.add_client( input=create_input(), output=create_output(stdout=sys.stdout), color_depth=color_depth, connection=None) client_state.app.run()
def run(): content = [] for line in iter(sys.stdin.readline, ""): content.append([line.strip(), False]) chk = CheckboxList(content) root_container = chk layout = Layout(root_container) kb = KeyBindings() @kb.add('q') def exit_(event): event.app.exit() app = Application(input=create_input(sys.stdout), key_bindings=kb, layout=layout, full_screen=True) app.run()
def create_app_session_from_tty() -> Generator[AppSession, None, None]: """ Create `AppSession` that always prefers the TTY input/output. Even if `sys.stdin` and `sys.stdout` are connected to input/output pipes, this will still use the terminal for interaction (because `sys.stderr` is still connected to the terminal). Usage:: from prompt_toolkit.shortcuts import prompt with create_app_session_from_tty(): prompt('>') """ from prompt_toolkit.input.defaults import create_input from prompt_toolkit.output.defaults import create_output input = create_input(always_prefer_tty=True) output = create_output(always_prefer_tty=True) with create_app_session(input=input, output=output) as app_session: yield app_session
def __init__( self, get_globals=None, get_locals=None, history_filename=None, vi_mode=False, input=None, output=None, color_depth=None, # For internal use. extra_key_bindings=None, _completer=None, _validator=None, _lexer=None, _extra_buffer_processors=None, _extra_layout_body=None, _extra_toolbars=None, _input_buffer_height=None): self.get_globals = get_globals or (lambda: {}) self.get_locals = get_locals or self.get_globals self._completer = _completer or PythonCompleter( self.get_globals, self.get_locals) self._validator = _validator or PythonValidator( self.get_compiler_flags) self._lexer = _lexer or PygmentsLexer(PythonLexer) if history_filename: self.history = ThreadedHistory(FileHistory(history_filename)) else: self.history = InMemoryHistory() self._input_buffer_height = _input_buffer_height self._extra_layout_body = _extra_layout_body or [] self._extra_toolbars = _extra_toolbars or [] self._extra_buffer_processors = _extra_buffer_processors or [] self.extra_key_bindings = extra_key_bindings or KeyBindings() # Settings. self.show_signature = False self.show_docstring = False self.show_meta_enter_message = True self.completion_visualisation = CompletionVisualisation.MULTI_COLUMN self.completion_menu_scroll_offset = 1 self.show_line_numbers = False self.show_status_bar = True self.wrap_lines = True self.complete_while_typing = True self.paste_mode = False # When True, don't insert whitespace after newline. self.confirm_exit = True # Ask for confirmation when Control-D is pressed. self.accept_input_on_enter = 2 # Accept when pressing Enter 'n' times. # 'None' means that meta-enter is always required. self.enable_open_in_editor = True self.enable_system_bindings = True self.enable_input_validation = True self.enable_auto_suggest = False self.enable_mouse_support = False self.enable_history_search = False # When True, like readline, going # back in history will filter the # history on the records starting # with the current input. self.enable_syntax_highlighting = True self.highlight_matching_parenthesis = False self.show_sidebar = False # Currently show the sidebar. self.show_sidebar_help = True # When the sidebar is visible, also show the help text. self.show_exit_confirmation = False # Currently show 'Do you really want to exit?' self.terminal_title = None # The title to be displayed in the terminal. (None or string.) self.exit_message = 'Do you really want to exit?' self.insert_blank_line_after_output = True # (For the REPL.) # The buffers. self.default_buffer = self._create_buffer() self.search_buffer = Buffer() self.docstring_buffer = Buffer(read_only=True) # Tokens to be shown at the prompt. self.prompt_style = 'classic' # The currently active style. self.all_prompt_styles = { # Styles selectable from the menu. 'ipython': IPythonPrompt(self), 'classic': ClassicPrompt(), } self.get_input_prompt = lambda: \ self.all_prompt_styles[self.prompt_style].in_prompt() self.get_output_prompt = lambda: \ self.all_prompt_styles[self.prompt_style].out_prompt() #: Load styles. self.code_styles = get_all_code_styles() self.ui_styles = get_all_ui_styles() self._current_code_style_name = 'default' self._current_ui_style_name = 'default' if is_windows(): self._current_code_style_name = 'win32' self._current_style = self._generate_style() # Options to be configurable from the sidebar. self.options = self._create_options() self.selected_option_index = 0 #: Incremeting integer counting the current statement. self.current_statement_index = 1 # Code signatures. (This is set asynchronously after a timeout.) self.signatures = [] # Boolean indicating whether we have a signatures thread running. # (Never run more than one at the same time.) self._get_signatures_thread_running = False self.output = output or create_output() self.input = input or create_input(sys.stdin) self.app = self._create_application(color_depth) if vi_mode: self.app.editing_mode = EditingMode.VI
def default_input(self) -> 'Input': if self._default_input is None: from prompt_toolkit.input.defaults import create_input self._default_input = create_input() return self._default_input
def __init__(self, vi_mode=False, style=None, search_text=None, titlebar_tokens=None): assert isinstance(vi_mode, bool) assert style is None or isinstance(style, Style) self.sources = [] self.current_source_index = 0 # Index in `self.sources`. self.highlight_search = True self.in_colon_mode = False self.message = None self.displaying_help = False self.search_text = search_text self.display_titlebar = bool(titlebar_tokens) self.titlebar_tokens = titlebar_tokens or [] self._dummy_source = DummySource() # When this is True, always make sure that the cursor goes to the # bottom of the visible content. This is similar to 'tail -f'. self.forward_forever = False # Status information for all sources. Source -> _SourceInfo. # (Remember this info as long as the Source object exists.) self.source_info = weakref.WeakKeyDictionary() # Create prompt_toolkit stuff. def open_file(buff): # Open file. self.open_file(buff.text) # Focus main buffer again. buff.reset() # Buffer for the 'Examine:' input. self.examine_buffer = Buffer(name='EXAMINE', completer=PathCompleter(expanduser=True), accept_handler=open_file, multiline=False) # Search buffer. self.search_buffer = Buffer(multiline=False) self.layout = PagerLayout(self) bindings = create_key_bindings(self) self.application = Application( input=create_input(sys.stdout), layout=Layout(container=self.layout.container), enable_page_navigation_bindings=True, key_bindings=bindings, style=style or Style.from_dict(ui_style), mouse_support=True, after_render=self._after_render, full_screen=True) # Hide message when a key is pressed. def key_pressed(_): self.message = None self.application.key_processor.before_key_press += key_pressed if vi_mode: self.application.editing_mode = EditingMode.VI
def input(self) -> "Input": if self._input is None: from prompt_toolkit.input.defaults import create_input self._input = create_input() return self._input
def input(self) -> 'Input': if self._input is None: from prompt_toolkit.input.defaults import create_input self._input = create_input() return self._input
def __init__(self, vi_mode=False, style=None, search_text=None, titlebar_tokens=None): assert isinstance(vi_mode, bool) assert style is None or isinstance(style, Style) self.sources = [] self.current_source_index = 0 # Index in `self.sources`. self.highlight_search = True self.in_colon_mode = False self.message = None self.displaying_help = False self.search_text = search_text self.display_titlebar = bool(titlebar_tokens) self.titlebar_tokens = titlebar_tokens or [] self._dummy_source = DummySource() # When this is True, always make sure that the cursor goes to the # bottom of the visible content. This is similar to 'tail -f'. self.forward_forever = False # Status information for all sources. Source -> _SourceInfo. # (Remember this info as long as the Source object exists.) self.source_info = weakref.WeakKeyDictionary() # Create prompt_toolkit stuff. def open_file(buff): # Open file. self.open_file(buff.text) # Focus main buffer again. buff.reset() # Buffer for the 'Examine:' input. self.examine_buffer = Buffer( name='EXAMINE', completer=PathCompleter(expanduser=True), accept_handler=open_file, multiline=False) # Search buffer. self.search_buffer = Buffer(multiline=False) self.layout = PagerLayout(self) bindings = create_key_bindings(self) self.application = Application( input=create_input(sys.stdout), layout=Layout(container=self.layout.container), enable_page_navigation_bindings=True, key_bindings=bindings, style=style or Style.from_dict(ui_style), mouse_support=True, after_render=self._after_render, full_screen=True) # Hide message when a key is pressed. def key_pressed(_): self.message = None self.application.key_processor.before_key_press += key_pressed if vi_mode: self.application.editing_mode = EditingMode.VI
def __init__(self, get_globals=None, get_locals=None, history_filename=None, vi_mode=False, input=None, output=None, color_depth=None, # For internal use. extra_key_bindings=None, _completer=None, _validator=None, _lexer=None, _extra_buffer_processors=None, _extra_layout_body=None, _extra_toolbars=None, _input_buffer_height=None): self.get_globals = get_globals or (lambda: {}) self.get_locals = get_locals or self.get_globals self._completer = _completer or PythonCompleter(self.get_globals, self.get_locals) self._validator = _validator or PythonValidator(self.get_compiler_flags) self._lexer = _lexer or PygmentsLexer(PythonLexer) if history_filename: self.history = ThreadedHistory(FileHistory(history_filename)) else: self.history = InMemoryHistory() self._input_buffer_height = _input_buffer_height self._extra_layout_body = _extra_layout_body or [] self._extra_toolbars = _extra_toolbars or [] self._extra_buffer_processors = _extra_buffer_processors or [] self.extra_key_bindings = extra_key_bindings or KeyBindings() # Settings. self.show_signature = False self.show_docstring = False self.show_meta_enter_message = True self.completion_visualisation = CompletionVisualisation.MULTI_COLUMN self.completion_menu_scroll_offset = 1 self.show_line_numbers = False self.show_status_bar = True self.wrap_lines = True self.complete_while_typing = True self.paste_mode = False # When True, don't insert whitespace after newline. self.confirm_exit = True # Ask for confirmation when Control-D is pressed. self.accept_input_on_enter = 2 # Accept when pressing Enter 'n' times. # 'None' means that meta-enter is always required. self.enable_open_in_editor = True self.enable_system_bindings = True self.enable_input_validation = True self.enable_auto_suggest = False self.enable_mouse_support = False self.enable_history_search = False # When True, like readline, going # back in history will filter the # history on the records starting # with the current input. self.enable_syntax_highlighting = True self.swap_light_and_dark = False self.highlight_matching_parenthesis = False self.show_sidebar = False # Currently show the sidebar. self.show_sidebar_help = True # When the sidebar is visible, also show the help text. self.show_exit_confirmation = False # Currently show 'Do you really want to exit?' self.terminal_title = None # The title to be displayed in the terminal. (None or string.) self.exit_message = 'Do you really want to exit?' self.insert_blank_line_after_output = True # (For the REPL.) # The buffers. self.default_buffer = self._create_buffer() self.search_buffer = Buffer() self.docstring_buffer = Buffer(read_only=True) # Tokens to be shown at the prompt. self.prompt_style = 'classic' # The currently active style. self.all_prompt_styles = { # Styles selectable from the menu. 'ipython': IPythonPrompt(self), 'classic': ClassicPrompt(), } self.get_input_prompt = lambda: \ self.all_prompt_styles[self.prompt_style].in_prompt() self.get_output_prompt = lambda: \ self.all_prompt_styles[self.prompt_style].out_prompt() #: Load styles. self.code_styles = get_all_code_styles() self.ui_styles = get_all_ui_styles() self._current_code_style_name = 'default' self._current_ui_style_name = 'default' if is_windows(): self._current_code_style_name = 'win32' self._current_style = self._generate_style() self.color_depth = color_depth or ColorDepth.default() self.max_brightness = 1.0 self.min_brightness = 0.0 # Options to be configurable from the sidebar. self.options = self._create_options() self.selected_option_index = 0 #: Incremeting integer counting the current statement. self.current_statement_index = 1 # Code signatures. (This is set asynchronously after a timeout.) self.signatures = [] # Boolean indicating whether we have a signatures thread running. # (Never run more than one at the same time.) self._get_signatures_thread_running = False self.output = output or create_output() self.input = input or create_input(sys.stdin) self.style_transformation = merge_style_transformations([ ConditionalStyleTransformation( SwapLightAndDarkStyleTransformation(), filter=Condition(lambda: self.swap_light_and_dark)), AdjustBrightnessStyleTransformation( lambda: self.min_brightness, lambda: self.max_brightness), ]) self.ptpython_layout = PtPythonLayout( self, lexer=DynamicLexer( lambda: self._lexer if self.enable_syntax_highlighting else SimpleLexer()), input_buffer_height=self._input_buffer_height, extra_buffer_processors=self._extra_buffer_processors, extra_body=self._extra_layout_body, extra_toolbars=self._extra_toolbars) self.app = self._create_application() if vi_mode: self.app.editing_mode = EditingMode.VI
def __init__( self, *, vi_mode: bool = False, style: Optional[Style] = None, search_text: Optional[str] = None, titlebar_tokens=None, input: Optional[Input] = None, output: Optional[Output] = None, ) -> None: self.sources: List[Source] = [] self.current_source_index = 0 # Index in `self.sources`. self.highlight_search = True self.in_colon_mode = False self.message: Optional[str] = None self.displaying_help = False self.search_text = search_text self.display_titlebar = bool(titlebar_tokens) self.titlebar_tokens = titlebar_tokens or [] self._dummy_source = DummySource() # When this is True, always make sure that the cursor goes to the # bottom of the visible content. This is similar to 'tail -f'. self.forward_forever = False # Status information for all sources. Source -> SourceInfo. # (Remember this info as long as the Source object exists.) self.source_info: weakref.WeakKeyDictionary[ Source, SourceInfo] = weakref.WeakKeyDictionary() # Create prompt_toolkit stuff. def open_file(buff: Buffer) -> bool: # Open file. self.open_file(buff.text) return False # Buffer for the 'Examine:' input. self.examine_buffer = Buffer( name="EXAMINE", completer=PathCompleter(expanduser=True), accept_handler=open_file, multiline=False, ) # Search buffer. self.search_buffer = Buffer(multiline=False) self.layout = PagerLayout(self) # Input/output. if input is None: # By default, use the stdout device for input. # (This makes it possible to pipe data to stdin, but still read key # strokes from the TTY). input = create_input(sys.stdout) bindings = create_key_bindings(self) self.application: Application[None] = Application( input=input, output=output, layout=Layout(container=self.layout.container), enable_page_navigation_bindings=True, key_bindings=bindings, style=style or Style.from_dict(ui_style), mouse_support=True, after_render=self._after_render, full_screen=True, ) # Hide message when a key is pressed. def key_pressed(_) -> None: self.message = None self.application.key_processor.before_key_press += key_pressed if vi_mode: self.application.editing_mode = EditingMode.VI