rich
rich copied to clipboard
[REQUEST] Strip emoji shortcodes on emoji=False
Have you checked the issues for a similar suggestions? Yes
How would you improve Rich?
Propose on Console to strip emoji (strip shortcodes before rendering) if emoji is False. Also add an options to if the user want to strip emoji or keep the emoji.
Here this the code inherit from Console that strip emoji if emoji is False:
import re
import typing
import rich
import rich.emoji
import rich.live
import rich.segment
from rich.style import Style
import rich.text
# regex from https://regex101.com/r/iE9uV0/1
EMOJI_SHORTCODE_REGEX = re.compile(r"(\:(\w|\+|\-)+\:)(?=|[\!\.\?]|$)", re.IGNORECASE)
class MyConsole(rich.console.Console):
def render_str(
self,
text: str,
*,
style: str | Style = "",
justify: None
| typing.Literal["default"]
| typing.Literal["left"]
| typing.Literal["center"]
| typing.Literal["right"]
| typing.Literal["full"] = None,
overflow: None
| typing.Literal["fold"]
| typing.Literal["crop"]
| typing.Literal["ellipsis"]
| typing.Literal["ignore"] = None,
emoji: bool | None = None,
markup: bool | None = None,
highlight: bool | None = None,
highlighter: typing.Callable[[str | rich.text.Text], rich.text.Text]
| None = None,
) -> rich.text.Text:
emoji_enabled = emoji or (emoji is None and self._emoji)
if not emoji_enabled:
text = EMOJI_SHORTCODE_REGEX.sub("", text)
return super().render_str(
text,
style=style,
justify=justify,
overflow=overflow,
emoji=emoji,
markup=markup,
highlight=highlight,
highlighter=highlighter,
)
console = MyConsole()
print(console.render_str("Hi guys!:sparkles:"))
This should print if emoji=True: Hi guys! ✨ emoji=False: Hi guys!
What problem does it solve for you?
I think that keeping the emoji shortcodes make the text printing ugly, Remove it will be a good idea (of course, keep the originial way.)