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 = []
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']}

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

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

Xonsh console lexer for pygments.


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.

style_namestr, optional

The style name to initialize with.


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

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.


Color token name.


Mapping for looking up non-hex colors


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.


Color name.

fgstr, optional

Foreground color name.

bgstr, optional

Background color name.


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) -> (Token.Color, <class 'str'>)[source]
Determine color to use for file approximately as ls –color would,

given lstat() results and its path.


relative path of file (as user typed it).


lstat() results for file_path.

color token, color_key


  • implementation follows one authority:

  • 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) → Token.Color[source]

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


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


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


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.


Gets or makes a pygments color style by its name.

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.


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.