def print_formatted_text(output, formatted_text, style, style_transformation=None, color_depth=None): """ Print a list of (style_str, text) tuples in the given style to the output. """ assert isinstance(output, Output) assert isinstance(style, BaseStyle) assert style_transformation is None or isinstance(style_transformation, StyleTransformation) assert color_depth is None or color_depth in ColorDepth._ALL fragments = to_formatted_text(formatted_text) style_transformation = style_transformation or DummyStyleTransformation() color_depth = color_depth or ColorDepth.default() # Reset first. output.reset_attributes() output.enable_autowrap() # Print all (style_str, text) tuples. attrs_for_style_string = _StyleStringToAttrsCache( style.get_attrs_for_style_str, style_transformation) for style_str, text in fragments: attrs = attrs_for_style_string[style_str] if attrs: output.set_attributes(attrs, color_depth) else: output.reset_attributes() # Eliminate carriage returns text = text.replace('\r', '') # Assume that the output is raw, and insert a carriage return before # every newline. (Also important when the front-end is a telnet client.) output.write(text.replace('\n', '\r\n')) # Reset again. output.reset_attributes() output.flush()
def print_formatted_text( output: Output, formatted_text: AnyFormattedText, style: BaseStyle, style_transformation: Optional[StyleTransformation] = None, color_depth: Optional[ColorDepth] = None, ) -> None: """ Print a list of (style_str, text) tuples in the given style to the output. """ fragments = to_formatted_text(formatted_text) style_transformation = style_transformation or DummyStyleTransformation() color_depth = color_depth or output.get_default_color_depth() # Reset first. output.reset_attributes() output.enable_autowrap() last_attrs: Optional[Attrs] = None # Print all (style_str, text) tuples. attrs_for_style_string = _StyleStringToAttrsCache( style.get_attrs_for_style_str, style_transformation) for style_str, text, *_ in fragments: attrs = attrs_for_style_string[style_str] # Set style attributes if something changed. if attrs != last_attrs: if attrs: output.set_attributes(attrs, color_depth) else: output.reset_attributes() last_attrs = attrs # Eliminate carriage returns text = text.replace("\r", "") # Assume that the output is raw, and insert a carriage return before # every newline. (Also important when the front-end is a telnet client.) output.write(text.replace("\n", "\r\n")) # Reset again. output.reset_attributes() output.flush()
def print_formatted_text( output: Output, formatted_text: AnyFormattedText, style: BaseStyle, style_transformation: Optional[StyleTransformation] = None, color_depth: Optional[ColorDepth] = None) -> None: """ Print a list of (style_str, text) tuples in the given style to the output. """ fragments = to_formatted_text(formatted_text) style_transformation = style_transformation or DummyStyleTransformation() color_depth = color_depth or ColorDepth.default() # Reset first. output.reset_attributes() output.enable_autowrap() # Print all (style_str, text) tuples. attrs_for_style_string = _StyleStringToAttrsCache( style.get_attrs_for_style_str, style_transformation) for style_str, text, *_ in fragments: attrs = attrs_for_style_string[style_str] if attrs: output.set_attributes(attrs, color_depth) else: output.reset_attributes() # Assume that the output is raw, and insert a carriage return before # every newline. (Also important when the front-end is a telnet client.) assert '\r' not in text output.write(text.replace('\n', '\r\n')) # Reset again. output.reset_attributes() output.flush()
def __init__( self, layout=None, style=None, include_default_pygments_style=True, style_transformation=None, 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 style_transformation is None or isinstance( style_transformation, StyleTransformation) 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) if layout is None: layout = create_dummy_layout() if style_transformation is None: style_transformation = DummyStyleTransformation() self.style = style self.style_transformation = style_transformation # 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()
def __init__( self, layout: Optional[Layout] = None, style: Optional[BaseStyle] = None, include_default_pygments_style: FilterOrBool = True, style_transformation: Optional[StyleTransformation] = None, key_bindings: Optional[KeyBindingsBase] = None, clipboard: Optional[Clipboard] = None, full_screen: bool = False, color_depth: Union[ ColorDepth, Callable[[], Union[ColorDepth, None]], None ] = None, mouse_support: FilterOrBool = False, enable_page_navigation_bindings: Optional[ FilterOrBool ] = None, # Can be None, True or False. paste_mode: FilterOrBool = False, editing_mode: EditingMode = EditingMode.EMACS, erase_when_done: bool = False, reverse_vi_search_direction: FilterOrBool = False, min_redraw_interval: Union[float, int, None] = None, max_render_postpone_time: Union[float, int, None] = 0.01, refresh_interval: Optional[float] = None, terminal_size_polling_interval: Optional[float] = 0.5, on_reset: Optional[ApplicationEventHandler] = None, on_invalidate: Optional[ApplicationEventHandler] = None, before_render: Optional[ApplicationEventHandler] = None, after_render: Optional[ApplicationEventHandler] = None, # I/O. input: Optional[Input] = None, output: Optional[Output] = None, ) -> 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) if layout is None: layout = create_dummy_layout() if style_transformation is None: style_transformation = DummyStyleTransformation() self.style = style self.style_transformation = style_transformation # 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: bool = 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 self.refresh_interval = refresh_interval self.terminal_size_polling_interval = terminal_size_polling_interval # 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. session = get_app_session() self.output = output or session.output self.input = input or session.input # List of 'extra' functions to execute before a Application.run. self.pre_run_callables: List[Callable[[], None]] = [] self._is_running = False self.future: Optional[Future[_AppResult]] = None self.loop: Optional[AbstractEventLoop] = None self.context: Optional[contextvars.Context] = 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 = 0.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: List[ Event[object] ] = [] # Collection of 'invalidate' Event objects. self._last_redraw_time = 0.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: Optional[Future[None]] = None # Trigger initialize callback. self.reset()