Beispiel #1
0
def tracer_format_line(fname,
                       lineno,
                       line,
                       color=True,
                       lexer=None,
                       formatter=None):
    """Formats a trace line suitable for printing."""
    fname = min(fname,
                prompt._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)
    if tokens[-1][1] == "\n":
        del tokens[-1]
    elif tokens[-1][1].endswith("\n"):
        tokens[-1] = (tokens[-1][0], tokens[-1][1].rstrip())
    return tokens
Beispiel #2
0
    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
Beispiel #3
0
    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
Beispiel #4
0
def tracer_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