base.py 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. """
  2. The base classes for the styling.
  3. """
  4. from __future__ import annotations
  5. from abc import ABCMeta, abstractmethod, abstractproperty
  6. from typing import Callable, Hashable, NamedTuple
  7. __all__ = [
  8. "Attrs",
  9. "DEFAULT_ATTRS",
  10. "ANSI_COLOR_NAMES",
  11. "ANSI_COLOR_NAMES_ALIASES",
  12. "BaseStyle",
  13. "DummyStyle",
  14. "DynamicStyle",
  15. ]
  16. #: Style attributes.
  17. class Attrs(NamedTuple):
  18. color: str | None
  19. bgcolor: str | None
  20. bold: bool | None
  21. underline: bool | None
  22. strike: bool | None
  23. italic: bool | None
  24. blink: bool | None
  25. reverse: bool | None
  26. hidden: bool | None
  27. """
  28. :param color: Hexadecimal string. E.g. '000000' or Ansi color name: e.g. 'ansiblue'
  29. :param bgcolor: Hexadecimal string. E.g. 'ffffff' or Ansi color name: e.g. 'ansired'
  30. :param bold: Boolean
  31. :param underline: Boolean
  32. :param strike: Boolean
  33. :param italic: Boolean
  34. :param blink: Boolean
  35. :param reverse: Boolean
  36. :param hidden: Boolean
  37. """
  38. #: The default `Attrs`.
  39. DEFAULT_ATTRS = Attrs(
  40. color="",
  41. bgcolor="",
  42. bold=False,
  43. underline=False,
  44. strike=False,
  45. italic=False,
  46. blink=False,
  47. reverse=False,
  48. hidden=False,
  49. )
  50. #: ``Attrs.bgcolor/fgcolor`` can be in either 'ffffff' format, or can be any of
  51. #: the following in case we want to take colors from the 8/16 color palette.
  52. #: Usually, in that case, the terminal application allows to configure the RGB
  53. #: values for these names.
  54. #: ISO 6429 colors
  55. ANSI_COLOR_NAMES = [
  56. "ansidefault",
  57. # Low intensity, dark. (One or two components 0x80, the other 0x00.)
  58. "ansiblack",
  59. "ansired",
  60. "ansigreen",
  61. "ansiyellow",
  62. "ansiblue",
  63. "ansimagenta",
  64. "ansicyan",
  65. "ansigray",
  66. # High intensity, bright. (One or two components 0xff, the other 0x00. Not supported everywhere.)
  67. "ansibrightblack",
  68. "ansibrightred",
  69. "ansibrightgreen",
  70. "ansibrightyellow",
  71. "ansibrightblue",
  72. "ansibrightmagenta",
  73. "ansibrightcyan",
  74. "ansiwhite",
  75. ]
  76. # People don't use the same ANSI color names everywhere. In prompt_toolkit 1.0
  77. # we used some unconventional names (which were contributed like that to
  78. # Pygments). This is fixed now, but we still support the old names.
  79. # The table below maps the old aliases to the current names.
  80. ANSI_COLOR_NAMES_ALIASES: dict[str, str] = {
  81. "ansidarkgray": "ansibrightblack",
  82. "ansiteal": "ansicyan",
  83. "ansiturquoise": "ansibrightcyan",
  84. "ansibrown": "ansiyellow",
  85. "ansipurple": "ansimagenta",
  86. "ansifuchsia": "ansibrightmagenta",
  87. "ansilightgray": "ansigray",
  88. "ansidarkred": "ansired",
  89. "ansidarkgreen": "ansigreen",
  90. "ansidarkblue": "ansiblue",
  91. }
  92. assert set(ANSI_COLOR_NAMES_ALIASES.values()).issubset(set(ANSI_COLOR_NAMES))
  93. assert not (set(ANSI_COLOR_NAMES_ALIASES.keys()) & set(ANSI_COLOR_NAMES))
  94. class BaseStyle(metaclass=ABCMeta):
  95. """
  96. Abstract base class for prompt_toolkit styles.
  97. """
  98. @abstractmethod
  99. def get_attrs_for_style_str(
  100. self, style_str: str, default: Attrs = DEFAULT_ATTRS
  101. ) -> Attrs:
  102. """
  103. Return :class:`.Attrs` for the given style string.
  104. :param style_str: The style string. This can contain inline styling as
  105. well as classnames (e.g. "class:title").
  106. :param default: `Attrs` to be used if no styling was defined.
  107. """
  108. @abstractproperty
  109. def style_rules(self) -> list[tuple[str, str]]:
  110. """
  111. The list of style rules, used to create this style.
  112. (Required for `DynamicStyle` and `_MergedStyle` to work.)
  113. """
  114. return []
  115. @abstractmethod
  116. def invalidation_hash(self) -> Hashable:
  117. """
  118. Invalidation hash for the style. When this changes over time, the
  119. renderer knows that something in the style changed, and that everything
  120. has to be redrawn.
  121. """
  122. class DummyStyle(BaseStyle):
  123. """
  124. A style that doesn't style anything.
  125. """
  126. def get_attrs_for_style_str(
  127. self, style_str: str, default: Attrs = DEFAULT_ATTRS
  128. ) -> Attrs:
  129. return default
  130. def invalidation_hash(self) -> Hashable:
  131. return 1 # Always the same value.
  132. @property
  133. def style_rules(self) -> list[tuple[str, str]]:
  134. return []
  135. class DynamicStyle(BaseStyle):
  136. """
  137. Style class that can dynamically returns an other Style.
  138. :param get_style: Callable that returns a :class:`.Style` instance.
  139. """
  140. def __init__(self, get_style: Callable[[], BaseStyle | None]):
  141. self.get_style = get_style
  142. self._dummy = DummyStyle()
  143. def get_attrs_for_style_str(
  144. self, style_str: str, default: Attrs = DEFAULT_ATTRS
  145. ) -> Attrs:
  146. style = self.get_style() or self._dummy
  147. return style.get_attrs_for_style_str(style_str, default)
  148. def invalidation_hash(self) -> Hashable:
  149. return (self.get_style() or self._dummy).invalidation_hash()
  150. @property
  151. def style_rules(self) -> list[tuple[str, str]]:
  152. return (self.get_style() or self._dummy).style_rules