Coverage for /pythoncovmergedfiles/medio/medio/usr/local/lib/python3.11/site-packages/rich/emoji.py: 55%
Shortcuts on this page
r m x toggle line displays
j k next/prev highlighted chunk
0 (zero) top of page
1 (one) first highlighted chunk
Shortcuts on this page
r m x toggle line displays
j k next/prev highlighted chunk
0 (zero) top of page
1 (one) first highlighted chunk
1import sys
2from typing import TYPE_CHECKING, Literal, Optional, Union
4from ._emoji_replace import _emoji_replace
5from .jupyter import JupyterMixin
6from .segment import Segment
7from .style import Style
9if TYPE_CHECKING:
10 from .console import Console, ConsoleOptions, RenderResult
13EmojiVariant = Literal["emoji", "text"]
16class NoEmoji(Exception):
17 """No emoji by that name."""
20class Emoji(JupyterMixin):
21 __slots__ = ["name", "style", "_char", "variant"]
23 VARIANTS = {"text": "\ufe0e", "emoji": "\ufe0f"}
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.
33 Args:
34 name (str): Name of emoji.
35 style (Union[str, Style], optional): Optional style. Defaults to None.
37 Raises:
38 NoEmoji: If the emoji doesn't exist.
39 """
40 from ._emoji_codes import EMOJI
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, "")
52 @classmethod
53 def replace(cls, text: str) -> str:
54 """Replace emoji markup with corresponding unicode characters.
56 Args:
57 text (str): A string with emojis codes, e.g. "Hello :smiley:!"
59 Returns:
60 str: A string with emoji codes replaces with actual emoji.
61 """
62 return _emoji_replace(text)
64 def __repr__(self) -> str:
65 return f"<emoji {self.name!r}>"
67 def __str__(self) -> str:
68 return self._char
70 def __rich_console__(
71 self, console: "Console", options: "ConsoleOptions"
72 ) -> "RenderResult":
73 yield Segment(self._char, console.get_style(self.style))
76if __name__ == "__main__": # pragma: no cover
77 import sys
79 from rich.columns import Columns
80 from rich.console import Console
82 console = Console(record=True)
84 from ._emoji_codes import EMOJI
86 columns = Columns(
87 (f":{name}: {name}" for name in sorted(EMOJI.keys()) if "\u200d" not in name),
88 column_first=True,
89 )
91 console.print(columns)
92 if len(sys.argv) > 1:
93 console.save_html(sys.argv[1])