| 18 | |
| 19 | |
| 20 | class Emoji(JupyterMixin): |
| 21 | __slots__ = ["name", "style", "_char", "variant"] |
| 22 | |
| 23 | VARIANTS = {"text": "\ufe0e", "emoji": "\ufe0f"} |
| 24 | |
| 25 | def __init__( |
| 26 | self, |
| 27 | name: str, |
| 28 | style: Union[str, Style] = "none", |
| 29 | variant: Optional[EmojiVariant] = None, |
| 30 | ) -> None: |
| 31 | """A single emoji character. |
| 32 | |
| 33 | Args: |
| 34 | name (str): Name of emoji. |
| 35 | style (Union[str, Style], optional): Optional style. Defaults to None. |
| 36 | |
| 37 | Raises: |
| 38 | NoEmoji: If the emoji doesn't exist. |
| 39 | """ |
| 40 | from ._emoji_codes import EMOJI |
| 41 | |
| 42 | self.name = name |
| 43 | self.style = style |
| 44 | self.variant = variant |
| 45 | try: |
| 46 | self._char = EMOJI[name] |
| 47 | except KeyError: |
| 48 | raise NoEmoji(f"No emoji called {name!r}") |
| 49 | if variant is not None: |
| 50 | self._char += self.VARIANTS.get(variant, "") |
| 51 | |
| 52 | @classmethod |
| 53 | def replace(cls, text: str) -> str: |
| 54 | """Replace emoji markup with corresponding unicode characters. |
| 55 | |
| 56 | Args: |
| 57 | text (str): A string with emojis codes, e.g. "Hello :smiley:!" |
| 58 | |
| 59 | Returns: |
| 60 | str: A string with emoji codes replaces with actual emoji. |
| 61 | """ |
| 62 | return _emoji_replace(text) |
| 63 | |
| 64 | def __repr__(self) -> str: |
| 65 | return f"<emoji {self.name!r}>" |
| 66 | |
| 67 | def __str__(self) -> str: |
| 68 | return self._char |
| 69 | |
| 70 | def __rich_console__( |
| 71 | self, console: "Console", options: "ConsoleOptions" |
| 72 | ) -> "RenderResult": |
| 73 | yield Segment(self._char, console.get_style(self.style)) |
| 74 | |
| 75 | |
| 76 | if __name__ == "__main__": # pragma: no cover |
no outgoing calls