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

[BUG] rich.text - Text.highlight_words() : highlights letters, not words #3184

Closed
2 tasks
bwprog opened this issue Nov 3, 2023 · 3 comments
Closed
2 tasks

Comments

@bwprog
Copy link

bwprog commented Nov 3, 2023

Describe the bug

Using the Text class, I try to highlight a specific word in the text using the highlight_words() function. The word does get highlighted, however all other letters that match any letter in the highlight word within the Text object get highlighted as well.

richtextbug

The fact both the function and keyword use "words" and not "letters" indicates to me this is a bug. I looked at the source code and I think I see the problem.

This line of the code treats keyword "words" as an iterable which it is, as a string.

re_words = "|".join(re.escape(word) for word in words)

It ends up splitting the keyword string like this:

'j|s|o|n'

I think it's just missing split on the end like this. That changes the iterable to a list of words.

re_words = "|".join(re.escape(word) for word in words.split(" "))

I made the change on my local text.py and get what I think should be the correct output. Works on 1 and 2 words with a space separating. I don't know if this would break any edge cases.

richtextfix

With that, the keyword string becomes this:

'json'

Or with a second word, 'json output', like this:

'json|output'

Platform

Click to expand

What platform (Win/Linux/Mac) are you running on? What terminal software are you using?

Linux OpenSuse Tumbleweed (2023-11-02) / Python 3.11.5 / Konsole 23.08.2 / rich 13.6.0

richdiag1 richdiag2
Copy link

github-actions bot commented Nov 3, 2023

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

@darrenburns
Copy link
Member

The signature for highlight_words says that it takes an argument of type words: Iterable[str].

This means you need to pass your words as, for example, a list:

text.highlight_words(["one", "two", "three"], style="blue")

In your original example, you should do:

j.highlight_words(["json"], style="blue")

Closing this issue - but feel free to let me know if you're still having trouble.

@darrenburns darrenburns closed this as not planned Won't fix, can't repro, duplicate, stale Nov 7, 2023
Copy link

github-actions bot commented Nov 7, 2023

I hope we solved your problem.

If you like using Rich, you might also enjoy Textual

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

2 participants