Coverage for /pythoncovmergedfiles/medio/medio/usr/local/lib/python3.11/site-packages/rich/_emoji_replace.py: 72%
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 re
2from typing import Callable, Match, Optional
4_ReStringMatch = Match[str] # regex match object
5_ReSubCallable = Callable[[_ReStringMatch], str] # Callable invoked by re.sub
6_EmojiSubMethod = Callable[[_ReSubCallable, str], str] # Sub method of a compiled re
9def _emoji_replace(
10 text: str,
11 default_variant: Optional[str] = None,
12 _emoji_sub: _EmojiSubMethod = re.compile(r"(:(\S*?)(?:(?:\-)(emoji|text))?:)").sub,
13) -> str:
14 """Replace emoji code in text."""
15 from ._emoji_codes import EMOJI
17 get_emoji = EMOJI.__getitem__
18 variants = {"text": "\ufe0e", "emoji": "\ufe0f"}
19 get_variant = variants.get
20 default_variant_code = variants.get(default_variant, "") if default_variant else ""
22 def do_replace(match: Match[str]) -> str:
23 emoji_code, emoji_name, variant = match.groups()
24 try:
25 return get_emoji(emoji_name.lower()) + get_variant(
26 variant, default_variant_code
27 )
28 except KeyError:
29 return emoji_code
31 return _emoji_sub(do_replace, text)