Textualize / rich

Rich is a Python library for rich text and beautiful formatting in the terminal.
https://rich.readthedocs.io/en/latest/
MIT License
49.09k stars 1.72k forks source link

[REQUEST] Strip emoji shortcodes on emoji=False #3520

Open DinhHuy2010 opened 3 days ago

DinhHuy2010 commented 3 days ago

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.)

github-actions[bot] commented 3 days ago

Thank you for your issue. Give us a little time to review it.

PS. You might want to check the FAQ if you haven't done so already.

This is an automated reply, generated by FAQtory