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