Pygments Hooks (xonsh.pyghooks)

Hooks for pygments syntax highlighting.

class xonsh.pyghooks.CompoundColorMap(styles, *args, **kwargs)[source]

Looks up color tokens by name, potentially generating the value from the lookup.

class xonsh.pyghooks.XonshConsoleLexer(*args, **kwds)[source]

Xonsh console lexer for pygments.

aliases = ['xonshcon']
filenames: List[str] = []
name = 'Xonsh console lexer'
tokens = {'root': [('^(>>>|\\.\\.\\.) ', Token.Generic.Prompt), ('\\n(>>>|\\.\\.\\.)', Token.Generic.Prompt), ('\\n(?![>.][>.][>.] )([^\\n]*)', Token.Generic.Output), ('\\n(?![>.][>.][>.] )(.*?)$', Token.Generic.Output), ('\\?', Token.Keyword), ('(?<=\\w)!', Token.Keyword), ('\\$\\w+', Token.Name.Variable), ('\\(', Token.Punctuation, 'py_bracket'), ('\\{', Token.Punctuation, 'py_curly_bracket'), 'mode_switch_brackets', ('\\n', Token.Text), ('^(\\s*)([rRuUbB]{,2})("""(?:.|\\n)*?""")', <function bygroups.<locals>.callback>), ("^(\\s*)([rRuUbB]{,2})('''(?:.|\\n)*?''')", <function bygroups.<locals>.callback>), ('\\A#!.+$', Token.Comment.Hashbang), ('#.*$', Token.Comment.Single), ('\\\\\\n', Token.Text), ('\\\\', Token.Text), 'keywords', ('(def)((?:\\s|\\\\\\s)+)', <function bygroups.<locals>.callback>, 'funcname'), ('(class)((?:\\s|\\\\\\s)+)', <function bygroups.<locals>.callback>, 'classname'), ('(from)((?:\\s|\\\\\\s)+)', <function bygroups.<locals>.callback>, 'fromimport'), ('(import)((?:\\s|\\\\\\s)+)', <function bygroups.<locals>.callback>, 'import'), 'expr']}
xonsh.pyghooks.XonshHtmlFormatter[source]

alias of xonsh.pyghooks.XonshHtmlFormatter.<locals>.XonshHtmlFormatterProxy

class xonsh.pyghooks.XonshLexer(*args, **kwds)[source]

Xonsh console lexer for pygments.

get_tokens_unprocessed(text)[source]

Check first command, then call super.get_tokens_unprocessed with root or subproc state

aliases = ['xonsh', 'xsh']
filenames = ['*.xsh', '*xonshrc']
name = 'Xonsh lexer'
tokens = {'backtick_re': [('[\\.\\^\\$\\*\\+\\?\\[\\]\\|]', Token.Literal.String.Regex), ('({[0-9]+}|{[0-9]+,[0-9]+})\\??', Token.Literal.String.Regex), ('\\\\([0-9]+|[AbBdDsSwWZabfnrtuUvx\\\\])', Token.Literal.String.Escape), ('`', Token.Literal.String.Backtick, '#pop'), ('[^`\\.\\^\\$\\*\\+\\?\\[\\]\\|]+', Token.Literal.String.Backtick)], 'mode_switch_brackets': [('(\\$)(\\{)', <function bygroups.<locals>.callback>, 'py_curly_bracket'), ('(@)(\\()', <function bygroups.<locals>.callback>, 'py_bracket'), ('([\\!\\$])(\\()', <function bygroups.<locals>.callback>, ('subproc_bracket', 'subproc_start')), ('(@\\$)(\\()', <function bygroups.<locals>.callback>, ('subproc_bracket', 'subproc_start')), ('([\\!\\$])(\\[)', <function bygroups.<locals>.callback>, ('subproc_square_bracket', 'subproc_start')), ('(g?)(`)', <function bygroups.<locals>.callback>, 'backtick_re')], 'py_bracket': [('\\)', Token.Punctuation, '#pop'), 'root'], 'py_curly_bracket': [('\\}', Token.Punctuation, '#pop'), 'root'], 'root': [('\\?', Token.Keyword), ('(?<=\\w)!', Token.Keyword), ('\\$\\w+', Token.Name.Variable), ('\\(', Token.Punctuation, 'py_bracket'), ('\\{', Token.Punctuation, 'py_curly_bracket'), 'mode_switch_brackets', inherit], 'subproc': ['mode_switch_brackets', ('&&|\\|\\|', Token.Operator, 'subproc_start'), ('"(\\\\\\\\|\\\\[0-7]+|\\\\.|[^"\\\\])*"', Token.Literal.String.Double), ("'(\\\\\\\\|\\\\[0-7]+|\\\\.|[^'\\\\])*'", Token.Literal.String.Single), ('(?<=\\w|\\s)!', Token.Keyword, 'subproc_macro'), ('^!', Token.Keyword, 'subproc_macro'), (';', Token.Punctuation, 'subproc_start'), ('&|=', Token.Punctuation), ('\\|', Token.Punctuation, 'subproc_start'), ('\\s+', Token.Text), ('[^=\\s\\[\\]{}()$"\\\'`<&|;]+', <function subproc_arg_callback>), ('<', Token.Text), ('\\$\\w+', Token.Name.Variable)], 'subproc_bracket': [('\\)', Token.Punctuation, '#pop'), 'subproc'], 'subproc_macro': [('(\\s*)([^\\n]+)', <function bygroups.<locals>.callback>), ('', Token.Text.Whitespace, '#pop')], 'subproc_square_bracket': [('\\]', Token.Punctuation, '#pop'), 'subproc'], 'subproc_start': [('\\s+', Token.Text.Whitespace), ('[^=\\s\\[\\]{}()$"\\\'`<&|;!]+(?=\\s|$|\\)|\\]|\\}|!)', <function subproc_cmd_callback>, '#pop'), ('', Token.Text.Whitespace, '#pop')]}
class xonsh.pyghooks.XonshStyle(style_name='default')[source]

A xonsh pygments style that will dispatch to the correct color map by using a ChainMap. The style_name property may be used to reset the current style.

Parameters
style_namestr, optional

The style name to initialize with.

enhance_colors_for_cmd_exe()[source]

Enhance colors when using cmd.exe on windows. When using the default style all blue and dark red colors are changed to CYAN and intense red.

property style_name
xonsh.pyghooks.XonshTerminal256Formatter[source]

alias of xonsh.pyghooks.XonshTerminal256Formatter.<locals>.XonshTerminal256FormatterProxy

xonsh.pyghooks.code_by_name(name, styles)[source]

Converts a token name into a pygments-style color code.

Parameters
namestr

Color token name.

stylesMapping

Mapping for looking up non-hex colors

Returns
codestr

Pygments style color code.

xonsh.pyghooks.color_by_name(name, fg=None, bg=None)[source]

Converts a color name to a color token, foreground name, and background name. Will take into consideration current foreground and background colors, if provided.

Parameters
namestr

Color name.

fgstr, optional

Foreground color name.

bgstr, optional

Background color name.

Returns
tokToken

Pygments Token.Color subclass

fgstr or None

New computed foreground color name.

bgstr or None

New computed background color name.

xonsh.pyghooks.color_file(file_path: str, path_stat: os.stat_result) → Tuple[pygments.token._TokenType, str][source]
Determine color to use for file approximately as ls –color would,

given lstat() results and its path.

Parameters
file_path:

relative path of file (as user typed it).

path_stat:

lstat() results for file_path.

Returns
color token, color_key

Notes

  • implementation follows one authority: https://github.com/coreutils/coreutils/blob/master/src/ls.c#L4879

  • except:

    1. does not return ‘mi’. That’s the color ls uses to show the (missing) target of a symlink (in ls -l, not ls).

    2. in dircolors, setting type code to ‘0 or ‘00’ bypasses that test and proceeds to others. In our implementation, setting code to ‘00’ paints the file with no color. This is arguably a bug.

xonsh.pyghooks.color_name_to_pygments_code(name, styles)[source]

Converts a xonsh color name to a pygments color code.

xonsh.pyghooks.color_token_by_name(xc: tuple, styles=None) → pygments.token._TokenType[source]

Returns (color) token corresponding to Xonsh color tuple, side effect: defines token is defined in styles

xonsh.pyghooks.iskeyword()

x.__contains__(y) <==> y in x.

xonsh.pyghooks.make_pygments_style(palette)[source]

Makes a pygments style based on a color palette.

xonsh.pyghooks.on_lscolors_change(key, oldvalue, newvalue, **kwargs)[source]

if LS_COLORS updated, update file_color_tokens and corresponding color token in style

xonsh.pyghooks.partial_color_tokenize(template)[source]

Tokenizes a template string containing colors. Will return a list of tuples mapping the token to the string which has that color. These sub-strings maybe templates themselves.

xonsh.pyghooks.pygments_style_by_name(name)[source]

Gets or makes a pygments color style by its name.

xonsh.pyghooks.register_custom_pygments_style(name, styles, highlight_color=None, background_color=None, base='default')[source]

Register custom style.

Parameters
namestr

Style name.

stylesdict

Token -> style mapping.

highlight_colorstr

Hightlight color.

background_colorstr

Background color.

basestr, optional

Base style to use as default.

Returns
styleThe pygments.Style subclass created
xonsh.pyghooks.subproc_arg_callback(_, match)[source]

Check if match contains valid path

xonsh.pyghooks.subproc_cmd_callback(_, match)[source]

Yield Builtin token if match contains valid command, otherwise fallback to fallback lexer.

xonsh.pyghooks.xonsh_style_proxy(styler)[source]

Factory for a proxy class to a xonsh style.

xonsh.pyghooks.file_color_tokens = {}

Parallel to LS_COLORS, keyed by dircolors keys, but value is a Color token. Initialized by XonshStyle.