Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
DinhHuy2010 opened this issue Oct 4, 2024 · 1 comment
Open

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

DinhHuy2010 opened this issue Oct 4, 2024 · 1 comment

Comments

@DinhHuy2010
Copy link

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

Copy link

github-actions bot commented Oct 4, 2024

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

@DinhHuy2010 DinhHuy2010 mentioned this issue Oct 8, 2024
9 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant