예제 #1
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
예제 #2
0
파일: inspectors.py 프로젝트: zbrdge/xonsh
    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
예제 #3
0
파일: shell.py 프로젝트: aweltsch/xonsh
 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
예제 #4
0
파일: shell.py 프로젝트: AndreaCrotti/xonsh
 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
예제 #5
0
파일: shell.py 프로젝트: AndreaCrotti/xonsh
 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)
예제 #6
0
파일: shell.py 프로젝트: aweltsch/xonsh
 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)
예제 #7
0
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
예제 #8
0
 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
예제 #9
0
파일: shell.py 프로젝트: CJ-Wright/xonsh
 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
예제 #10
0
파일: tracer.py 프로젝트: asmeurer/xonsh
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
예제 #11
0
파일: shell.py 프로젝트: aweltsch/xonsh
 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
예제 #12
0
파일: shell.py 프로젝트: nicfit/xonsh
 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
예제 #13
0
파일: shell.py 프로젝트: AndreaCrotti/xonsh
 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
예제 #14
0
파일: shell.py 프로젝트: nicfit/xonsh
 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
예제 #15
0
파일: shell.py 프로젝트: aweltsch/xonsh
 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
예제 #16
0
 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
예제 #17
0
 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)
예제 #18
0
파일: shell.py 프로젝트: AndreaCrotti/xonsh
 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)