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