emoji.py 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. import sys
  2. from typing import TYPE_CHECKING, Optional, Union
  3. from .jupyter import JupyterMixin
  4. from .segment import Segment
  5. from .style import Style
  6. from ._emoji_codes import EMOJI
  7. from ._emoji_replace import _emoji_replace
  8. if sys.version_info >= (3, 8):
  9. from typing import Literal
  10. else:
  11. from pip._vendor.typing_extensions import Literal # pragma: no cover
  12. if TYPE_CHECKING:
  13. from .console import Console, ConsoleOptions, RenderResult
  14. EmojiVariant = Literal["emoji", "text"]
  15. class NoEmoji(Exception):
  16. """No emoji by that name."""
  17. class Emoji(JupyterMixin):
  18. __slots__ = ["name", "style", "_char", "variant"]
  19. VARIANTS = {"text": "\uFE0E", "emoji": "\uFE0F"}
  20. def __init__(
  21. self,
  22. name: str,
  23. style: Union[str, Style] = "none",
  24. variant: Optional[EmojiVariant] = None,
  25. ) -> None:
  26. """A single emoji character.
  27. Args:
  28. name (str): Name of emoji.
  29. style (Union[str, Style], optional): Optional style. Defaults to None.
  30. Raises:
  31. NoEmoji: If the emoji doesn't exist.
  32. """
  33. self.name = name
  34. self.style = style
  35. self.variant = variant
  36. try:
  37. self._char = EMOJI[name]
  38. except KeyError:
  39. raise NoEmoji(f"No emoji called {name!r}")
  40. if variant is not None:
  41. self._char += self.VARIANTS.get(variant, "")
  42. @classmethod
  43. def replace(cls, text: str) -> str:
  44. """Replace emoji markup with corresponding unicode characters.
  45. Args:
  46. text (str): A string with emojis codes, e.g. "Hello :smiley:!"
  47. Returns:
  48. str: A string with emoji codes replaces with actual emoji.
  49. """
  50. return _emoji_replace(text)
  51. def __repr__(self) -> str:
  52. return f"<emoji {self.name!r}>"
  53. def __str__(self) -> str:
  54. return self._char
  55. def __rich_console__(
  56. self, console: "Console", options: "ConsoleOptions"
  57. ) -> "RenderResult":
  58. yield Segment(self._char, console.get_style(self.style))
  59. if __name__ == "__main__": # pragma: no cover
  60. import sys
  61. from pip._vendor.rich.columns import Columns
  62. from pip._vendor.rich.console import Console
  63. console = Console(record=True)
  64. columns = Columns(
  65. (f":{name}: {name}" for name in sorted(EMOJI.keys()) if "\u200D" not in name),
  66. column_first=True,
  67. )
  68. console.print(columns)
  69. if len(sys.argv) > 1:
  70. console.save_html(sys.argv[1])