def key_binding(filter=True, eager=False, is_global=False, save_before=None, record_in_macro=True): """ Decorator that turn a function into a `_Binding` object. This can be added to a `KeyBindings` object when a key binding is assigned. """ assert save_before is None or callable(save_before) filter = to_filter(filter) eager = to_filter(eager) is_global = to_filter(is_global) save_before = save_before or (lambda event: True) record_in_macro = to_filter(record_in_macro) keys = () def decorator(function): return _Binding(keys, function, filter=filter, eager=eager, is_global=is_global, save_before=save_before, record_in_macro=record_in_macro) return decorator
def __init__(self, style, output, full_screen=False, mouse_support=False, cpr_not_supported_callback=None): assert isinstance(style, BaseStyle) assert isinstance(output, Output) assert callable( cpr_not_supported_callback) or cpr_not_supported_callback is None self.style = style self.output = output self.full_screen = full_screen self.mouse_support = to_filter(mouse_support) self.cpr_not_supported_callback = cpr_not_supported_callback self._in_alternate_screen = False self._mouse_support_enabled = False self._bracketed_paste_enabled = False # Future set when we are waiting for a CPR flag. self._waiting_for_cpr_futures = deque() self.cpr_support = CPR_Support.UNKNOWN # Cache for the style. self._attrs_for_style = None self._last_style_hash = None self._last_color_depth = None self.reset(_scroll=True)
def __init__(self, keys, handler, filter=True, eager=False, is_global=False, save_before=None, record_in_macro=True): assert isinstance(keys, tuple) assert callable(handler) assert callable(save_before) self.keys = keys self.handler = handler self.filter = to_filter(filter) self.eager = to_filter(eager) self.is_global = to_filter(is_global) self.save_before = save_before self.record_in_macro = to_filter(record_in_macro)
def __init__(self, min_rows=3, suggested_max_column_width=30, show_meta=True, extra_filter=True, z_index=10**10): show_meta = to_filter(show_meta) extra_filter = to_filter(extra_filter) # Display filter: show when there are completions but not at the point # we are returning the input. full_filter = has_completions & ~is_done & extra_filter @Condition def any_completion_has_meta(): return any( c.display_meta for c in get_app().current_buffer.complete_state.completions) # Create child windows. completions_window = ConditionalContainer(content=Window( content=MultiColumnCompletionMenuControl( min_rows=min_rows, suggested_max_column_width=suggested_max_column_width), width=Dimension(min=8), height=Dimension(min=1), style='class:completion-menu'), filter=full_filter) meta_window = ConditionalContainer( content=Window(content=_SelectedCompletionMetaControl()), filter=show_meta & full_filter & any_completion_has_meta) # Initialise split. super(MultiColumnCompletionsMenu, self).__init__([completions_window, meta_window], z_index=z_index)
def __init__(self, max_height=None, scroll_offset=0, extra_filter=True, display_arrows=False, z_index=10**10): extra_filter = to_filter(extra_filter) display_arrows = to_filter(display_arrows) super(CompletionsMenu, self).__init__( content=Window( content=CompletionsMenuControl(), width=Dimension(min=8), height=Dimension(min=1, max=max_height), scroll_offsets=ScrollOffsets(top=scroll_offset, bottom=scroll_offset), right_margins=[ScrollbarMargin(display_arrows=display_arrows)], dont_extend_width=True, style='class:completion-menu', z_index=z_index, ), # Show when there are completions but not at the point we are # returning the input. filter=has_completions & ~is_done & extra_filter)
def __init__(self, pygments_lexer_cls, sync_from_start=True, syntax_sync=None): assert syntax_sync is None or isinstance(syntax_sync, SyntaxSync) self.pygments_lexer_cls = pygments_lexer_cls self.sync_from_start = to_filter(sync_from_start) # Instantiate the Pygments lexer. self.pygments_lexer = pygments_lexer_cls(stripnl=False, stripall=False, ensurenl=False) # Create syntax sync instance. self.syntax_sync = syntax_sync or RegexSync.from_pygments_lexer_cls( pygments_lexer_cls)
def __init__(self, prompt='Shell command: ', enable_global_bindings=True): self.prompt = prompt self.enable_global_bindings = to_filter(enable_global_bindings) self.system_buffer = Buffer(name=SYSTEM_BUFFER) self._bindings = self._build_key_bindings() self.buffer_control = BufferControl( buffer=self.system_buffer, lexer=SimpleLexer(style='class:system-toolbar.text'), input_processors=[ BeforeInput(lambda: self.prompt, style='class:system-toolbar') ], key_bindings=self._bindings) self.window = Window(self.buffer_control, height=1, style='class:system-toolbar') self.container = ConditionalContainer(content=self.window, filter=has_focus( self.system_buffer))
def add(self, *keys, **kwargs): """ Decorator for adding a key bindings. :param filter: :class:`~prompt_toolkit.filters.Filter` to determine when this key binding is active. :param eager: :class:`~prompt_toolkit.filters.Filter` or `bool`. When True, ignore potential longer matches when this key binding is hit. E.g. when there is an active eager key binding for Ctrl-X, execute the handler immediately and ignore the key binding for Ctrl-X Ctrl-E of which it is a prefix. :param is_global: When this key bindings is added to a `Container` or `Control`, make it a global (always active) binding. :param save_before: Callable that takes an `Event` and returns True if we should save the current buffer, before handling the event. (That's the default.) :param record_in_macro: Record these key bindings when a macro is being recorded. (True by default.) """ filter = to_filter(kwargs.pop('filter', True)) eager = to_filter(kwargs.pop('eager', False)) is_global = to_filter(kwargs.pop('is_global', False)) save_before = kwargs.pop('save_before', lambda e: True) record_in_macro = to_filter(kwargs.pop('record_in_macro', True)) assert not kwargs assert keys assert callable(save_before) keys = tuple(_check_and_expand_key(k) for k in keys) if isinstance(filter, Never): # When a filter is Never, it will always stay disabled, so in that # case don't bother putting it in the key bindings. It will slow # down every key press otherwise. def decorator(func): return func else: def decorator(func): if isinstance(func, _Binding): # We're adding an existing _Binding object. self.bindings.append( _Binding(keys, func.handler, filter=func.filter & filter, eager=eager | func.eager, is_global=is_global | func.is_global, save_before=func.save_before, record_in_macro=func.record_in_macro)) else: self.bindings.append( _Binding(keys, func, filter=filter, eager=eager, is_global=is_global, save_before=save_before, record_in_macro=record_in_macro)) self._clear_cache() return func return decorator
def __init__(self, margin, filter): assert isinstance(margin, Margin) self.margin = margin self.filter = to_filter(filter)
def enable_suspend(): return to_filter(self.enable_suspend)()
def dynamic(): value = getattr(self, attr_name) return to_filter(value)()
def _true(value): " Test whether `value` is True. In case of a Filter, call it. " return to_filter(value)()
def __init__(self, display_arrows=False): self.display_arrows = to_filter(display_arrows)
def __init__(self, key_bindings, filter=True): assert isinstance(key_bindings, KeyBindingsBase) _Proxy.__init__(self) self.key_bindings = key_bindings self.filter = to_filter(filter)
def __init__(self, relative=False, display_tildes=False): self.relative = to_filter(relative) self.display_tildes = to_filter(display_tildes)
def __init__(self, text='', multiline=True, password=False, lexer=None, completer=None, accept_handler=None, focusable=True, wrap_lines=True, read_only=False, width=None, height=None, dont_extend_height=False, dont_extend_width=False, line_numbers=False, scrollbar=False, style='', search_field=None, preview_search=True, prompt=''): assert isinstance(text, six.text_type) assert search_field is None or isinstance(search_field, SearchToolbar) if search_field is None: search_control = None elif isinstance(search_field, SearchToolbar): search_control = search_field.control self.buffer = Buffer( document=Document(text, 0), multiline=multiline, read_only=read_only, completer=completer, complete_while_typing=True, accept_handler=lambda buff: accept_handler and accept_handler()) self.control = BufferControl( buffer=self.buffer, lexer=lexer, input_processors=[ ConditionalProcessor(processor=PasswordProcessor(), filter=to_filter(password)), BeforeInput(prompt, style='class:text-area.prompt'), ], search_buffer_control=search_control, preview_search=preview_search, focusable=focusable) if multiline: if scrollbar: right_margins = [ScrollbarMargin(display_arrows=True)] else: right_margins = [] if line_numbers: left_margins = [NumberedMargin()] else: left_margins = [] else: wrap_lines = False # Never wrap for single line input. height = D.exact(1) left_margins = [] right_margins = [] style = 'class:text-area ' + style self.window = Window(height=height, width=width, dont_extend_height=dont_extend_height, dont_extend_width=dont_extend_width, content=self.control, style=style, wrap_lines=wrap_lines, left_margins=left_margins, right_margins=right_margins)
def __init__(self, processor, filter): assert isinstance(processor, Processor) self.processor = processor self.filter = to_filter(filter)
def __init__( self, layout=None, style=None, include_default_pygments_style=True, key_bindings=None, clipboard=None, full_screen=False, color_depth=None, mouse_support=False, enable_page_navigation_bindings=None, # Can be None, True or False. paste_mode=False, editing_mode=EditingMode.EMACS, erase_when_done=False, reverse_vi_search_direction=False, min_redraw_interval=None, max_render_postpone_time=0, on_reset=None, on_invalidate=None, before_render=None, after_render=None, # I/O. input=None, output=None): # If `enable_page_navigation_bindings` is not specified, enable it in # case of full screen applications only. This can be overridden by the user. if enable_page_navigation_bindings is None: enable_page_navigation_bindings = Condition( lambda: self.full_screen) paste_mode = to_filter(paste_mode) mouse_support = to_filter(mouse_support) reverse_vi_search_direction = to_filter(reverse_vi_search_direction) enable_page_navigation_bindings = to_filter( enable_page_navigation_bindings) include_default_pygments_style = to_filter( include_default_pygments_style) assert layout is None or isinstance( layout, Layout), 'Got layout: %r' % (layout, ) assert key_bindings is None or isinstance(key_bindings, KeyBindingsBase) assert clipboard is None or isinstance(clipboard, Clipboard) assert isinstance(full_screen, bool) assert (color_depth is None or callable(color_depth) or color_depth in ColorDepth._ALL), 'Got color_depth: %r' % (color_depth, ) assert isinstance(editing_mode, six.string_types) assert style is None or isinstance(style, BaseStyle) assert isinstance(erase_when_done, bool) assert min_redraw_interval is None or isinstance( min_redraw_interval, (float, int)) assert max_render_postpone_time is None or isinstance( max_render_postpone_time, (float, int)) assert on_reset is None or callable(on_reset) assert on_invalidate is None or callable(on_invalidate) assert before_render is None or callable(before_render) assert after_render is None or callable(after_render) assert output is None or isinstance(output, Output) assert input is None or isinstance(input, Input) self.style = style if layout is None: layout = create_dummy_layout() # Key bindings. self.key_bindings = key_bindings self._default_bindings = load_key_bindings() self._page_navigation_bindings = load_page_navigation_bindings() self.layout = layout self.clipboard = clipboard or InMemoryClipboard() self.full_screen = full_screen self._color_depth = color_depth self.mouse_support = mouse_support self.paste_mode = paste_mode self.editing_mode = editing_mode self.erase_when_done = erase_when_done self.reverse_vi_search_direction = reverse_vi_search_direction self.enable_page_navigation_bindings = enable_page_navigation_bindings self.min_redraw_interval = min_redraw_interval self.max_render_postpone_time = max_render_postpone_time # Events. self.on_invalidate = Event(self, on_invalidate) self.on_reset = Event(self, on_reset) self.before_render = Event(self, before_render) self.after_render = Event(self, after_render) # I/O. self.output = output or get_default_output() self.input = input or get_default_input() # List of 'extra' functions to execute before a Application.run. self.pre_run_callables = [] self._is_running = False self.future = None #: Quoted insert. This flag is set if we go into quoted insert mode. self.quoted_insert = False #: Vi state. (For Vi key bindings.) self.vi_state = ViState() self.emacs_state = EmacsState() #: When to flush the input (For flushing escape keys.) This is important #: on terminals that use vt100 input. We can't distinguish the escape #: key from for instance the left-arrow key, if we don't know what follows #: after "\x1b". This little timer will consider "\x1b" to be escape if #: nothing did follow in this time span. #: This seems to work like the `ttimeoutlen` option in Vim. self.ttimeoutlen = .5 # Seconds. #: Like Vim's `timeoutlen` option. This can be `None` or a float. For #: instance, suppose that we have a key binding AB and a second key #: binding A. If the uses presses A and then waits, we don't handle #: this binding yet (unless it was marked 'eager'), because we don't #: know what will follow. This timeout is the maximum amount of time #: that we wait until we call the handlers anyway. Pass `None` to #: disable this timeout. self.timeoutlen = 1.0 #: The `Renderer` instance. # Make sure that the same stdout is used, when a custom renderer has been passed. self._merged_style = self._create_merged_style( include_default_pygments_style) self.renderer = Renderer( self._merged_style, self.output, full_screen=full_screen, mouse_support=mouse_support, cpr_not_supported_callback=self.cpr_not_supported_callback) #: Render counter. This one is increased every time the UI is rendered. #: It can be used as a key for caching certain information during one #: rendering. self.render_counter = 0 # Invalidate flag. When 'True', a repaint has been scheduled. self._invalidated = False self._invalidate_events = [ ] # Collection of 'invalidate' Event objects. self._last_redraw_time = 0 # Unix timestamp of last redraw. Used when # `min_redraw_interval` is given. #: The `InputProcessor` instance. self.key_processor = KeyProcessor(_CombinedRegistry(self)) # If `run_in_terminal` was called. This will point to a `Future` what will be # set at the point when the previous run finishes. self._running_in_terminal = False self._running_in_terminal_f = None # Trigger initialize callback. self.reset()