Xithrius / twitch-tui

Twitch chat in the terminal.
https://xithrius.github.io/twitch-tui/
Apache License 2.0
437 stars 32 forks source link

Fix off by one error in highlight #649

Closed vesdev closed 1 month ago

vesdev commented 1 month ago

before image

after image

it does not account for the newlines properly

shoutout to mostlymaxi's chat for noticing this

vesdev commented 1 month ago

also updated minor version of ahash as 0.8.3 does not compile for me, did not bother separating it into a different commit as its a very small change

vesdev commented 1 month ago

Looking more into it there is something funky with the wrapping. start_index is not correct after the line is built for the first_line_msg

codecov[bot] commented 1 month ago

Codecov Report

All modified and coverable lines are covered by tests :white_check_mark:

Project coverage is 17.87%. Comparing base (31bcc1c) to head (46248bc). Report is 1 commits behind head on main.

Additional details and impacted files ```diff @@ Coverage Diff @@ ## main #649 +/- ## ========================================== + Coverage 17.86% 17.87% +0.01% ========================================== Files 42 42 Lines 5430 5431 +1 ========================================== + Hits 970 971 +1 Misses 4460 4460 ```

:umbrella: View full report in Codecov by Sentry.
:loudspeaker: Have feedback on the report? Share it here.

Nogesma commented 1 month ago

I think I know what the issue is. When finding the indices to highlight, the message contains a newline character, which is then removed when splitting the message into multiple lines. This leads to the start_index not matching the highlight index.

A solution might be to just strip newlines from the message when trying to find the sections to highlight, or maybe just do a +1 to the start index only when the current line contained a newline.

Your solution, if I'm right, would give us an off by one highlight when the message spans more than one line without containing a newline.

Let me know if you need any more help with the PR.

vesdev commented 1 month ago

I think I know what the issue is. When finding the indices to highlight, the message contains a newline character, which is then removed when splitting the message into multiple lines. This leads to the start_index not matching the highlight index.

A solution might be to just strip newlines from the message when trying to find the sections to highlight, or maybe just do a +1 to the start index only when the current line contained a newline.

Your solution, if I'm right, would give us an off by one highlight when the message spans more than one line without containing a newline.

Let me know if you need any more help with the PR.

Looking at textwrap docs, wrap() should not include a trailing newline char. https://docs.rs/textwrap/latest/textwrap/fn.wrap.html

Xithrius commented 1 month ago

Sorry I haven't replied yet, busy with IRL stuff. I'll take a look at this when I can.

Xithrius commented 1 month ago

Tested between branches, this fix looks to work perfectly. Thanks for the PR!

Nogesma commented 1 month ago

I'm afraid this PR breaks the case where the line just overflows onto the next line, without a line break.

This is from current git main branch: image

The full solution needs to check where the newlines are in the message, then increment the start_index at the right time.

I can make a PR that fixes this, unless @vesdev wants to do it?

Edit: Fixed bad image

Xithrius commented 1 month ago

Ah hell I thought I tested that, my bad. Good thing I didn't do a release for it :sweat_smile:

vesdev commented 1 month ago

I'm afraid this PR breaks the case where the line just overflows onto the next line, without a line break.

This is from current git main branch: image

The full solution needs to check where the newlines are in the message, then increment the start_index at the right time.

I can make a PR that fixes this, unless @vesdev wants to do it?

Edit: Fixed bad image

Yea feel free to, the proper fix I think would be using the textwrap output for getting the highlight indices instead of the message payload. Didn't think about break_word 🤔