Coverage for /run/media/veganeco/water/status600.com/pypi/reptilian_climates/modules_series_2/ships/fields/gardens_pip/rich/emoji.py: 60%

1 import sys

2 from typing import TYPE_CHECKING, Optional, Union

3

4 from .jupyter import JupyterMixin

5 from .segment import Segment

6 from .style import Style

7 from ._emoji_codes import EMOJI

8 from ._emoji_replace import _emoji_replace

9

10 if sys.version_info >= (3, 8):

11 from typing import Literal

12 else:

13 from typing_extensions import Literal # pragma: no cover

14

15

16 if TYPE_CHECKING:

17 from .console import Console, ConsoleOptions, RenderResult

18

19

20 EmojiVariant = Literal["emoji", "text"]

21

22

23 class NoEmoji(Exception):

24 """No emoji by that name."""

25

26

27 class Emoji(JupyterMixin):

28 __slots__ = ["name", "style", "_char", "variant"]

29

30 VARIANTS = {"text": "\uFE0E", "emoji": "\uFE0F"}

31

32 def __init__(

33 self,

34 name: str,

35 style: Union[str, Style] = "none",

36 variant: Optional[EmojiVariant] = None,

37 ) -> None:

38 """A single emoji character.

39

40 Args:

41 name (str): Name of emoji.

42 style (Union[str, Style], optional): Optional style. Defaults to None.

43

44 Raises:

45 NoEmoji: If the emoji doesn't exist.

46 """

47 self.name = name

48 self.style = style

49 self.variant = variant

50 try:

51 self._char = EMOJI[name]

52 except KeyError:

53 raise NoEmoji(f"No emoji called {name!r}")

54 if variant is not None:

55 self._char += self.VARIANTS.get(variant, "")

56

57 @classmethod

58 def replace(cls, text: str) -> str:

59 """Replace emoji markup with corresponding unicode characters.

60

61 Args:

62 text (str): A string with emojis codes, e.g. "Hello :smiley:!"

63

64 Returns:

65 str: A string with emoji codes replaces with actual emoji.

66 """

67 return _emoji_replace(text)

68

69 def __repr__(self) -> str:

70 return f"<emoji {self.name!r}>"

71

72 def __str__(self) -> str:

73 return self._char

74

75 def __rich_console__(

76 self, console: "Console", options: "ConsoleOptions"

77 ) -> "RenderResult":

78 yield Segment(self._char, console.get_style(self.style))

79

80

81 if __name__ == "__main__": # pragma: no cover

82 import sys

83

84 from rich.columns import Columns

85 from rich.console import Console

86

87 console = Console(record=True)

88

89 columns = Columns(

90 (f":{name}: {name}" for name in sorted(EMOJI.keys()) if "\u200D" not in name),

91 column_first=True,

92 )

93

94 console.print(columns)

95 if len(sys.argv) > 1:

96 console.save_html(sys.argv[1])