def _format_fields_tokens(self, fields, title_width=0): """Formats a list of fields for display using color tokens from pygments. Parameters ---------- fields : list A list of 2-tuples: (field_title, field_content) title_width : int How many characters to pad titles to. Default to longest title. """ out = [] if title_width == 0: title_width = max(len(title) + 2 for title, _ in fields) for title, content in fields: title_len = len(title) title = "{BOLD_RED}" + title + ":{NO_COLOR}" if not isinstance(content, str) or len(content.splitlines()) > 1: title += "\n" else: title += " ".ljust(title_width - title_len) out += pyghooks.partial_color_tokenize(title) if isinstance(content, str): out[-1] = (out[-1][0], out[-1][1] + content + "\n") else: out += content out[-1] = (out[-1][0], out[-1][1] + "\n") out[-1] = (out[-1][0], out[-1][1] + "\n") return out
def _format_fields_tokens(self, fields, title_width=0): """Formats a list of fields for display using color tokens from pygments. Parameters ---------- fields : list A list of 2-tuples: (field_title, field_content) title_width : int How many characters to pad titles to. Default to longest title. """ out = [] if title_width == 0: title_width = max(len(title) + 2 for title, _ in fields) for title, content in fields: title_len = len(title) title = '{BOLD_RED}' + title + ':{NO_COLOR}' if not isinstance(content, str) or len(content.splitlines()) > 1: title += '\n' else: title += " ".ljust(title_width - title_len) out += pyghooks.partial_color_tokenize(title) if isinstance(content, str): out[-1] = (out[-1][0], out[-1][1] + content + '\n') else: out += content out[-1] = (out[-1][0], out[-1][1] + '\n') out[-1] = (out[-1][0], out[-1][1] + '\n') return out
def prompt_tokens(self, cli): """Returns a list of (token, str) tuples for the current prompt.""" p = builtins.__xonsh_env__.get('PROMPT') try: p = self.prompt_formatter(p) except Exception: # pylint: disable=broad-except print_exception() toks = partial_color_tokenize(p) self.settitle() return toks
def prompt_tokens(self, cli): """Returns a list of (token, str) tuples for the current prompt.""" p = builtins.__xonsh_env__.get('PROMPT') try: p = partial_format_prompt(p) except Exception: # pylint: disable=broad-except print_exception() toks = partial_color_tokenize(p) self.settitle() return toks
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 format_line(fname, lineno, line, color=True, lexer=None, formatter=None): """Formats a trace line suitable for printing.""" fname = min(fname, replace_home(fname), os.path.relpath(fname), key=len) if not color: return COLORLESS_LINE.format(fname=fname, lineno=lineno, line=line) cline = COLOR_LINE.format(fname=fname, lineno=lineno) if not HAVE_PYGMENTS: return cline + line # OK, so we have pygments tokens = pyghooks.partial_color_tokenize(cline) lexer = lexer or pyghooks.XonshLexer() tokens += pygments.lex(line, lexer=lexer) return tokens
def rprompt_tokens(self, cli): """Returns a list of (token, str) tuples for the current right prompt. """ p = builtins.__xonsh_env__.get('RIGHT_PROMPT') if len(p) == 0: return [] try: p = partial_format_prompt(p) except Exception: # pylint: disable=broad-except print_exception() toks = partial_color_tokenize(p) return toks
def format_line(fname, lineno, line, color=True, lexer=None, formatter=None): """Formats a trace line suitable for printing.""" fname = min(fname, replace_home(fname), os.path.relpath(fname), key=len) if not color: return COLORLESS_LINE.format(fname=fname, lineno=lineno, line=line) cline = COLOR_LINE.format(fname=fname, lineno=lineno) if not HAS_PYGMENTS: return cline + line # OK, so we have pygments tokens = pyghooks.partial_color_tokenize(cline) lexer = lexer or pyghooks.XonshLexer() tokens += pygments.lex(line, lexer=lexer) return tokens
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 rprompt_tokens(self, cli): """Returns a list of (token, str) tuples for the current right prompt. """ p = builtins.__xonsh_env__.get('RIGHT_PROMPT') # partial_format_prompt does handle empty strings properly, # but this avoids descending into it in the common case of # $RIGHT_PROMPT == ''. if isinstance(p, str) and len(p) == 0: return [] try: p = partial_format_prompt(p) except Exception: # pylint: disable=broad-except print_exception() toks = partial_color_tokenize(p) return toks
def bottom_toolbar_tokens(self, cli): """Returns a list of (token, str) tuples for the current bottom toolbar. """ p = builtins.__xonsh_env__.get('BOTTOM_TOOLBAR') # self.prompt_formatter does handle empty strings properly, # but this avoids descending into it in the common case of # $TOOLBAR == ''. if isinstance(p, str) and len(p) == 0: return [] try: p = self.prompt_formatter(p) except Exception: # pylint: disable=broad-except print_exception() toks = partial_color_tokenize(p) return toks
def format_color(self, string, **kwargs): """Formats a color string using Pygments. This, therefore, returns a list of (Token, str) tuples. """ return partial_color_tokenize(string)