ratatui-org / ratatui

Rust library that's all about cooking up terminal user interfaces (TUIs) 👨‍🍳🐀
https://ratatui.rs
MIT License
8.82k stars 263 forks source link

Visual glitch - Null character inside block wrapped component alters borders #1211

Open nick42d opened 5 days ago

nick42d commented 5 days ago

Description

G'day,

This one took me a while to investigate in a project so thought I'd report here in case it helps someone else. When a block wrapped widget contains text with a null character (eg \0), and is styled (e.g colour or bold), it can cause a visual glitch where outter block borders do not align correctly.

To Reproduce

Reproducible example located here: https://github.com/nick42d/ratatuirepro

Expected behavior

Expect inner widget contents not to impact outter block - however note that the cause could be a technical limitation of the terminal that I'm not aware of.

Screenshots

image

Environment

kdheepak commented 5 days ago

Thanks for reporting! oof, that must have been annoying to find.

kdheepak commented 5 days ago

I think the solution for this is that Line or Span should probably strip all null characters?

joshka commented 5 days ago

This is related to the recent unicode-width change in 0.1.13, which makes these characters now be measured as one character wide instead of 0 characters. See https://github.com/unicode-rs/unicode-width/issues/55 (closed as WONTFIX)

Locking unicode-width version =0.1.12 (and reverting to Ratatui 0.26.3) makes the issue in the repro go away. This might be a viable approach if you need an immediate workaround, but it's likely a better idea to strip the null characters (and probably any control characters) until this is fixed.

I think the solution for this is that Line or Span should probably strip all null characters?

I think this is probably more complex than only filtering characters out:

nick42d commented 5 days ago

This is related to the recent unicode-width change in 0.1.13, which makes these characters now be measured as one character wide instead of 0 characters. See https://github.com/unicode-rs/unicode-width/issues/55 (closed as WONTFIX)

Locking unicode-width version =0.1.12 (and reverting to Ratatui 0.26.3) makes the issue in the repro go away. This might be a viable approach if you need an immediate workaround, but it's likely a better idea to strip the null characters (and probably any control characters) until this is fixed.

I think the solution for this is that Line or Span should probably strip all null characters?

I think this is probably more complex than only filtering characters out:

  • it's not just null characters that are affected, it's all the control characters and a bunch of other things that we haven't until now considered
  • we might want to turn off this filtering (it's a perf problem very large string sizes)
  • we definitely want to be able to pass through ANSI escape codes and measure that differently (they'd be in the control characters mentioned above)
  • We have other things that probably need to go with this (e.g. tab characters)

@joshka, that was an interesting read! For now I took the approach to strip the characters, I was doing something incorrect on my part in any case calling a function like string.push(optional_char.unwrap_or_default()). Good luck in finding a solution!