def __init__(self, registry=None, enable_vi_mode=Never(), enable_system_bindings=Never(), enable_search=Always(), enable_open_in_editor=Never(), enable_all=Always()): # Accept both Filters and booleans as input. enable_vi_mode = to_cli_filter(enable_vi_mode) enable_system_bindings = to_cli_filter(enable_system_bindings) enable_open_in_editor = to_cli_filter(enable_open_in_editor) enable_all = to_cli_filter(enable_all) # Create registry. assert registry is None or isinstance(registry, Registry) self.registry = registry or Registry() # Emacs mode filter is the opposite of Vi mode. enable_emacs_mode = ~enable_vi_mode # Vi state. (Object to keep track of in which Vi mode we are.) self.vi_state = ViState() # Load basic bindings. load_basic_bindings(self.registry, enable_all) load_basic_system_bindings(self.registry, enable_system_bindings & enable_all) # Load emacs bindings. load_emacs_bindings(self.registry, enable_emacs_mode & enable_all) load_emacs_open_in_editor_bindings( self.registry, enable_emacs_mode & enable_open_in_editor & enable_all) load_emacs_search_bindings( self.registry, enable_emacs_mode & enable_search & enable_all) load_emacs_system_bindings( self.registry, enable_emacs_mode & enable_system_bindings & enable_all) # Load Vi bindings. load_vi_bindings(self.registry, self.vi_state, enable_visual_key=~enable_open_in_editor, filter=enable_vi_mode & enable_all) load_vi_open_in_editor_bindings( self.registry, self.vi_state, enable_vi_mode & enable_open_in_editor & enable_all) load_vi_search_bindings(self.registry, self.vi_state, enable_vi_mode & enable_search & enable_all) load_vi_system_bindings( self.registry, self.vi_state, enable_vi_mode & enable_system_bindings & enable_all)
def __init__(self, content, width=None, height=None, get_width=None, get_height=None, filter=Always(), dont_extend_width=False, dont_extend_height=False, scroll_offset=0, allow_scroll_beyond_bottom=Never()): assert isinstance(content, UIControl) assert width is None or isinstance(width, LayoutDimension) assert height is None or isinstance(height, LayoutDimension) assert get_width is None or callable(get_width) assert get_height is None or callable(get_height) assert width is None or get_width is None assert height is None or get_height is None assert isinstance(filter, CLIFilter) assert isinstance(scroll_offset, Integer) assert isinstance(allow_scroll_beyond_bottom, CLIFilter) self.content = content self.filter = filter self.dont_extend_width = dont_extend_width self.dont_extend_height = dont_extend_height self.scroll_offset = scroll_offset self.allow_scroll_beyond_bottom = allow_scroll_beyond_bottom self._width = get_width or (lambda cli: width) self._height = get_height or (lambda cli: height) self.reset()
def update_multiline(): multiline = self.toggle_multiline() if multiline: event.cli.current_buffer.is_multiline = Always() else: event.cli.current_buffer.is_multiline = Never() print('Set multiline', multiline and 'ON' or 'off')
def create_cli(eventloop, message='', multiline=False, is_password=False, vi_mode=False, lexer=None, enable_system_prompt=False, enable_open_in_editor=False, validator=None, completer=None, style=None, history=None, get_prompt_tokens=None, get_bottom_toolbar_tokens=None, extra_input_processors=None, key_bindings_registry=None, output=None, on_abort=AbortAction.RAISE_EXCEPTION, on_exit=AbortAction.RAISE_EXCEPTION, on_accept=AcceptAction.RETURN_DOCUMENT): """ Create a `CommandLineInterface` instance. """ assert isinstance(eventloop, EventLoop) # Create history instance. if history is None: history = History() # Use default registry from KeyBindingManager if none was given. if key_bindings_registry is None: key_bindings_registry = KeyBindingManager( enable_vi_mode=vi_mode, enable_system_prompt=enable_system_prompt, enable_open_in_editor=enable_open_in_editor).registry # Create interface. return CommandLineInterface( eventloop=eventloop, layout=create_default_layout( message=message, lexer=lexer, is_password=is_password, reserve_space_for_menu=(completer is not None), get_prompt_tokens=get_prompt_tokens, get_bottom_toolbar_tokens=get_bottom_toolbar_tokens, extra_input_processors=extra_input_processors), buffer=Buffer( is_multiline=(Always() if multiline else Never()), history=history, validator=validator, completer=completer, ), key_bindings_registry=key_bindings_registry, style=style, output=output, on_abort=on_abort, on_exit=on_exit)
def __init__(self): super(SystemToolbarControl, self).__init__( lexer=BashLexer, buffer_name=SYSTEM_BUFFER, show_line_numbers=Never(), input_processors=[ BeforeInput.static('Shell command: ', Token.Toolbar.System.Prefix) ], )
def __init__(self, registry=None, enable_vi_mode=Never(), enable_system_prompt=Never(), enable_search=Always(), enable_open_in_editor=Never()): assert registry is None or isinstance(registry, Registry) assert isinstance(enable_vi_mode, CLIFilter) assert isinstance(enable_system_prompt, CLIFilter) assert isinstance(enable_open_in_editor, CLIFilter) self.registry = registry or Registry() # Emacs mode filter is the opposite of Vi mode. enable_emacs_mode = ~enable_vi_mode # Vi state. (Object to keep track of in which Vi mode we are.) self.vi_state = ViState() # Load emacs bindings. load_emacs_bindings(self.registry, enable_emacs_mode) load_emacs_open_in_editor_bindings( self.registry, enable_emacs_mode & enable_open_in_editor) load_emacs_search_bindings(self.registry, enable_emacs_mode & enable_search) load_emacs_system_bindings(self.registry, enable_emacs_mode & enable_system_prompt) # Load Vi bindings. load_vi_bindings(self.registry, self.vi_state, enable_vi_mode) load_vi_open_in_editor_bindings(self.registry, self.vi_state, enable_vi_mode & enable_open_in_editor) load_vi_search_bindings(self.registry, self.vi_state, enable_vi_mode & enable_search) load_vi_system_bindings(self.registry, self.vi_state, enable_vi_mode & enable_system_prompt)
def __init__(self, input_processors=None, lexer=None, show_line_numbers=Never(), preview_search=Never(), buffer_name=DEFAULT_BUFFER, default_token=Token, menu_position=None): assert input_processors is None or all( isinstance(i, Processor) for i in input_processors) assert isinstance(show_line_numbers, CLIFilter) assert isinstance(preview_search, CLIFilter) assert menu_position is None or callable(menu_position) self.input_processors = input_processors or [] self.show_line_numbers = show_line_numbers self.preview_search = preview_search self.buffer_name = buffer_name self.default_token = default_token self.menu_position = menu_position if lexer: self.lexer = lexer(stripnl=False, stripall=False, ensurenl=False) else: self.lexer = None #: LRU cache for the lexer. #: Often, due to cursor movement, undo/redo and window resizing #: operations, it happens that a short time, the same document has to be #: lexed. This is a faily easy way to cache such an expensive operation. self._token_lru_cache = _SimpleLRUCache(maxsize=8) #: Keep a similar cache for rendered screens. (when we scroll up/down #: through the screen, or when we change another buffer, we don't want #: to recreate the same screen again.) self._screen_lru_cache = _SimpleLRUCache(maxsize=8)
def test_never(): assert not Never()()
def test_invert(): assert not (~Always())() assert (~Never()()) c = ~Condition(lambda: False) assert c()
def __init__(self, preview_search=Never()): self.preview_search = to_cli_filter(preview_search)
def test_invert(self): self.assertFalse((~Always())()) self.assertTrue((~Never()())) c = ~Condition(lambda: False) self.assertTrue(c())
def test_never(self): self.assertFalse(Never()())
def __init__(self, preview_search=Never()): assert isinstance(preview_search, CLIFilter) self.preview_search = preview_search