def singleline(self, store_in_history=True, auto_suggest=None, enable_history_search=True, multiline=True, **kwargs): """Reads a single line of input from the shell. The store_in_history kwarg flags whether the input should be stored in PTK's in-memory history. """ env = builtins.__xonsh_env__ mouse_support = env.get('MOUSE_SUPPORT') if store_in_history: history = self.history else: history = None enable_history_search = False auto_suggest = auto_suggest if env.get('AUTO_SUGGEST') else None completions_display = env.get('COMPLETIONS_DISPLAY') multicolumn = (completions_display == 'multi') self.styler.style_name = env.get('XONSH_COLOR_STYLE') completer = None if completions_display == 'none' else self.pt_completer if not env.get('UPDATE_PROMPT_ON_KEYPRESS'): prompt_tokens_cached = self.prompt_tokens(None) get_prompt_tokens = lambda cli: prompt_tokens_cached rprompt_tokens_cached = self.rprompt_tokens(None) get_rprompt_tokens = lambda cli: rprompt_tokens_cached bottom_toolbar_tokens_cached = self.bottom_toolbar_tokens(None) get_bottom_toolbar_tokens = lambda cli: bottom_toolbar_tokens_cached else: get_prompt_tokens = self.prompt_tokens get_rprompt_tokens = self.rprompt_tokens get_bottom_toolbar_tokens = self.bottom_toolbar_tokens with self.prompter: prompt_args = { 'mouse_support': mouse_support, 'auto_suggest': auto_suggest, 'get_prompt_tokens': get_prompt_tokens, 'get_rprompt_tokens': get_rprompt_tokens, 'get_bottom_toolbar_tokens': get_bottom_toolbar_tokens, 'style': PygmentsStyle(xonsh_style_proxy(self.styler)), 'completer': completer, 'multiline': multiline, 'get_continuation_tokens': self.continuation_tokens, 'history': history, 'enable_history_search': enable_history_search, 'reserve_space_for_menu': 0, 'key_bindings_registry': self.key_bindings_manager.registry, 'display_completions_in_columns': multicolumn, } if builtins.__xonsh_env__.get('COLOR_INPUT'): prompt_args['lexer'] = PygmentsLexer(XonshLexer) line = self.prompter.prompt(**prompt_args) return line
def print_color(self, string, end='\n', **kwargs): """Prints a color string using prompt-toolkit color management.""" env = builtins.__xonsh_env__ self.styler.style_name = env.get('XONSH_COLOR_STYLE') if isinstance(string, str): tokens = partial_color_tokenize(string + end) else: # assume this is a list of (Token, str) tuples and just print tokens = string proxy_style = PygmentsStyle(xonsh_style_proxy(self.styler)) print_tokens(tokens, style=proxy_style)
def print_color(self, string, hide=False, **kwargs): if isinstance(string, str): s = self.format_color(string, hide=hide) else: # assume this is a list of (Token, str) tuples and format it env = builtins.__xonsh_env__ self.styler.style_name = env.get('XONSH_COLOR_STYLE') style_proxy = pyghooks.xonsh_style_proxy(self.styler) formatter = Terminal256Formatter(style=style_proxy) s = pygments.format(string, formatter).rstrip() print(s, **kwargs)
def format_color(self, string, hide=False, force_string=False, **kwargs): """Formats a color string using Pygments. This, therefore, returns a list of (Token, str) tuples. If force_string is set to true, though, this will return a color fomtatted string. """ tokens = partial_color_tokenize(string) if force_string: env = builtins.__xonsh_env__ self.styler.style_name = env.get('XONSH_COLOR_STYLE') proxy_style = xonsh_style_proxy(self.styler) formatter = XonshTerminal256Formatter(style=proxy_style) s = pygments.format(tokens, formatter) return s else: return tokens
def singleline(self, store_in_history=True, auto_suggest=None, enable_history_search=True, multiline=True, **kwargs): """Reads a single line of input from the shell. The store_in_history kwarg flags whether the input should be stored in PTK's in-memory history. """ env = builtins.__xonsh_env__ mouse_support = env.get('MOUSE_SUPPORT') if store_in_history: history = self.history else: history = None enable_history_search = False auto_suggest = auto_suggest if env.get('AUTO_SUGGEST') else None completions_display = env.get('COMPLETIONS_DISPLAY') multicolumn = (completions_display == 'multi') self.styler.style_name = env.get('XONSH_COLOR_STYLE') completer = None if completions_display == 'none' else self.pt_completer prompt_tokens = self.prompt_tokens(None) get_prompt_tokens = lambda cli: prompt_tokens rprompt_tokens = self.rprompt_tokens(None) get_rprompt_tokens = lambda cli: rprompt_tokens with self.prompter: line = self.prompter.prompt( mouse_support=mouse_support, auto_suggest=auto_suggest, get_prompt_tokens=get_prompt_tokens, get_rprompt_tokens=get_rprompt_tokens, style=PygmentsStyle(xonsh_style_proxy(self.styler)), completer=completer, lexer=PygmentsLexer(XonshLexer), multiline=multiline, get_continuation_tokens=self.continuation_tokens, history=history, enable_history_search=enable_history_search, reserve_space_for_menu=0, key_bindings_registry=self.key_bindings_manager.registry, display_completions_in_columns=multicolumn) return line
def html_format(s, style="default"): buf = io.StringIO() proxy_style = xonsh_style_proxy(XonshStyle(style)) # make sure we have a foreground color fgcolor = proxy_style._styles[Token.Text][0] if not fgcolor: fgcolor = invert_color(proxy_style.background_color[1:].strip("#")) # need to generate stream before creating formatter so that all tokens actually exist if isinstance(s, str): token_stream = partial_color_tokenize(s) else: token_stream = s formatter = XonshHtmlFormatter( wrapcode=True, noclasses=True, style=proxy_style, prestyles="margin: 0em; padding: 0.5em 0.1em; color: #" + fgcolor, cssstyles="border-style: solid; border-radius: 5px", ) formatter.format(token_stream, buf) return buf.getvalue()