pygments.py 780 B

1234567891011121314151617181920212223242526272829303132
  1. from __future__ import annotations
  2. from typing import TYPE_CHECKING
  3. from prompt_toolkit.styles.pygments import pygments_token_to_classname
  4. from .base import StyleAndTextTuples
  5. if TYPE_CHECKING:
  6. from pygments.token import Token
  7. __all__ = [
  8. "PygmentsTokens",
  9. ]
  10. class PygmentsTokens:
  11. """
  12. Turn a pygments token list into a list of prompt_toolkit text fragments
  13. (``(style_str, text)`` tuples).
  14. """
  15. def __init__(self, token_list: list[tuple[Token, str]]) -> None:
  16. self.token_list = token_list
  17. def __pt_formatted_text__(self) -> StyleAndTextTuples:
  18. result: StyleAndTextTuples = []
  19. for token, text in self.token_list:
  20. result.append(("class:" + pygments_token_to_classname(token), text))
  21. return result