token.py 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. """
  2. The Token class, interchangeable with ``pygments.token``.
  3. A `Token` has some semantics for a piece of text that is given a style through
  4. a :class:`~prompt_toolkit.styles.Style` class. A pygments lexer for instance,
  5. returns a list of (Token, text) tuples. Each fragment of text has a token
  6. assigned, which when combined with a style sheet, will determine the fine
  7. style.
  8. """
  9. # If we don't need any lexers or style classes from Pygments, we don't want
  10. # Pygments to be installed for only the following 10 lines of code. So, there
  11. # is some duplication, but this should stay compatible with Pygments.
  12. __all__ = (
  13. 'Token',
  14. 'ZeroWidthEscape',
  15. )
  16. class _TokenType(tuple):
  17. def __getattr__(self, val):
  18. if not val or not val[0].isupper():
  19. return tuple.__getattribute__(self, val)
  20. new = _TokenType(self + (val,))
  21. setattr(self, val, new)
  22. return new
  23. def __repr__(self):
  24. return 'Token' + (self and '.' or '') + '.'.join(self)
  25. # Prefer the Token class from Pygments. If Pygments is not installed, use our
  26. # minimalistic Token class.
  27. try:
  28. from pygments.token import Token
  29. except ImportError:
  30. Token = _TokenType()
  31. # Built-in tokens:
  32. #: `ZeroWidthEscape` can be used for raw VT escape sequences that don't
  33. #: cause the cursor position to move. (E.g. FinalTerm's escape sequences
  34. #: for shell integration.)
  35. ZeroWidthEscape = Token.ZeroWidthEscape