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

1 from typing import Callable, Match, Optional

2 import re

3

4 from ._emoji_codes import EMOJI

5

6

7 _ReStringMatch = Match[str] # regex match object

8 _ReSubCallable = Callable[[_ReStringMatch], str] # Callable invoked by re.sub

9 _EmojiSubMethod = Callable[[_ReSubCallable, str], str] # Sub method of a compiled re

10

11

12 def _emoji_replace(

13 text: str,

14 default_variant: Optional[str] = None,

15 _emoji_sub: _EmojiSubMethod = re.compile(r"(:(\S*?)(?:(?:\-)(emoji|text))?:)").sub,

16) -> str:

17 """Replace emoji code in text."""

18 get_emoji = EMOJI.__getitem__

19 variants = {"text": "\uFE0E", "emoji": "\uFE0F"}

20 get_variant = variants.get

21 default_variant_code = variants.get(default_variant, "") if default_variant else ""

22

23 def do_replace(match: Match[str]) -> str:

24 emoji_code, emoji_name, variant = match.groups()

25 try:

26 return get_emoji(emoji_name.lower()) + get_variant(

27 variant, default_variant_code

28 )

29 except KeyError:

30 return emoji_code

31

32 return _emoji_sub(do_replace, text)