def __init__(self): self.text_control = FormattedTextControl(text=HTML('')) self.window = Window(content=self.text_control, always_hide_cursor=True, align=WindowAlign.LEFT) super(HelpWindow, self).__init__(content=self.window, filter=has_focus(self.window))
def process_options(self) -> None: LOGGER.debug('processing %s options', len(self.get_options())) self.marks = [] def _get_linecount(_o: Option) -> int: return len(self.header_filter(_o).split('\n')) self.options_headers_linecount = list( map(_get_linecount, self.get_options())) self.max_entry_height = max(self.options_headers_linecount) LOGGER.debug('processing headers') self.options_headers = [] for _opt in self.get_options(): prestring = self.header_filter(_opt) + '\n' try: htmlobject = HTML(prestring).formatted_text except Exception as e: LOGGER.error('Error processing html for \n %s \n %s', prestring, e) htmlobject = [('fg:red', prestring)] self.options_headers += [htmlobject] LOGGER.debug('got %s headers', len(self.options_headers)) LOGGER.debug('processing matchers') self.options_matchers = list(map(self.match_filter, self.get_options())) self.indices = list(range(len(self.get_options()))) LOGGER.debug('got %s matchers', len(self.options_matchers))
def process_options(self): logger.debug('processing {0} options'.format(len(self.options))) self.marks = [] self.options_headers_linecount = [ len(self.header_filter(o).split('\n')) for o in self.options ] self.max_entry_height = max(self.options_headers_linecount) logger.debug('processing headers') self.options_headers = [] for o in self.options: prestring = self.header_filter(o) + '\n' try: htmlobject = HTML(prestring).formatted_text except: logger.error( 'Error processing html for \n {0}'.format(prestring) ) htmlobject = [('fg:red', prestring)] self.options_headers += [htmlobject] logger.debug('got {0} headers'.format(len(self.options_headers))) logger.debug('processing matchers') self.options_matchers = [self.match_filter(o) for o in self.options] self.indices = range(len(self.options)) logger.debug('got {0} matchers'.format(len(self.options_matchers)))
def set_help_text(self): """Generate the list of key bindings from key dictionary information """ help_text = "" # Add each key and its help string from corresponding key dictionary to the help string keys_info = user_keys.get_keys_info() for key in keys_info: help_text += ( "<ansired>{key[key]}</ansired>: {key[help]}\n".format( key=keys_info[key])) # Get HTML formatted text self.help_window.text = HTML(help_text)
def _get_completions(self, document): previous_text, fragment = last_word(document.text_before_cursor) if not settings.autocomplete: return previous_text = previous_text.rstrip() open_complete = previous_text and previous_text[-1] in '.{[' if not fragment and not open_complete: return assert open_complete or is_name(fragment[-1]), fragment try: all_vars = dict(autocomplete(self.state, previous_text)) except: if settings.debug: raise all_vars = {} # all_vars.update(KEYWORDS) assert all(isinstance(v, tuple) for v in all_vars.values()) all_vars = list(all_vars.items()) all_vars.sort(key=lambda item: (item[1][0], item[0])) for k, (_rank, v) in all_vars: if k.startswith(fragment): a, b = k[:len(fragment)], k[len(fragment):] if v is None: t = "<keyword>" else: try: t = v.type except AttributeError: t = type(v) yield Completion( b, start_position=0, display=HTML( '<b>%s</b>%s<ansibrightblack> : %s</ansibrightblack>' % (a, b, html_escape(t))), style='bg:ansigray fg:black', selected_style="fg:black bg:ansibrightyellow", )
def _get_completions(self, document): context, fragment = last_word(document.text_before_cursor) if not settings.autocomplete: return if context: open_complete = context.rstrip()[-1] in '.{[' else: open_complete = False if not fragment and not open_complete: return assert open_complete or is_name(fragment[-1]), fragment try: all_vars = dict(autocomplete(self.state, context)) except: if settings.debug: raise all_vars = {} # all_vars.update(KEYWORDS) assert all(isinstance(v, tuple) for v in all_vars.values()) all_vars = list(all_vars.items()) all_vars.sort(key=lambda item: (item[1][0], item[0])) for k, (_rank, v) in all_vars: if k.startswith(fragment): a, b = k[:len(fragment)], k[len(fragment):] if v is None: t = "<keyword>" else: try: t = v.type except AttributeError: t = type(v) yield Completion( b, start_position=0, display=HTML('<b>%s</b>%s: <blue>%s</blue>' % (a, b, html_escape(t))), )
def __init__(self, options: Sequence[Option], default_index: int = 0, header_filter: Callable[[Option], str] = str, match_filter: Callable[[Option], str] = str): self.info_window = InfoWindow() self.help_window = HelpWindow() self.message_toolbar = MessageToolbar(style="class:message_toolbar") self.error_toolbar = MessageToolbar(style="class:error_toolbar") self.status_line = MessageToolbar(style="class:status_line") self.status_line_format = config.getstring('status_line_format', section="tui") self.options_list = OptionsList( options, default_index, header_filter=header_filter, match_filter=match_filter, custom_filter=~has_focus( self.help_window)) # type: OptionsList[Option] self.options_list.search_buffer.on_text_changed += self.update commands, commands_kb = get_commands(self) self.command_line_prompt = CommandLinePrompt(commands=commands) kb = merge_key_bindings([create_keybindings(self), commands_kb]) _root_container = HSplit([ HSplit([ Window(content=BufferControl( input_processors=[BeforeInput('> ')], buffer=self.options_list.search_buffer)), self.options_list, self.info_window, ]), self.help_window, self.error_toolbar, self.message_toolbar, self.status_line, self.command_line_prompt.window, ]) help_text = "" # type: str keys_info = get_keys_info() for k in keys_info: help_text += ("<ansired>{k[key]}</ansired>: {k[help]}\n".format( k=keys_info[k])) self.help_window.text = HTML(help_text) self.layout = Layout(_root_container) super(Picker, self).__init__( input=None, output=None, editing_mode=EditingMode.EMACS if config.get( 'editmode', section='tui') == 'emacs' else EditingMode.VI, layout=self.layout, style=Style.from_dict({ 'options_list.selected_margin': config.get('options_list.selected_margin_style', section='tui'), 'options_list.unselected_margin': config.get('options_list.unselected_margin_style', section='tui'), 'error_toolbar': config.get('error_toolbar_style', section='tui'), 'message_toolbar': config.get('message_toolbar_style', section='tui'), 'status_line': config.get('status_line_style', section='tui'), }), key_bindings=kb, include_default_pygments_style=False, full_screen=True, enable_page_navigation_bindings=True) self.update()