iamb icon indicating copy to clipboard operation
iamb copied to clipboard

Buggy Unicode Support

Open de4db1ff opened this issue 1 year ago • 5 comments

The cursor does not align correctly with user input when typing certain Unicode characters, such as Chinese characters or emojis. This issue appears to be caused by the presence of certain Unicode characters in the buffer, which results in get_term_cursor() returning an incorrect value.

main.rs: 321
if let Some((cx, cy)) = sstate.get_term_cursor() {
                if let Some(c) = cursor {
                    let style = Style::default().fg(Color::Green);
                    let span = Span::styled(c.to_string(), style);
                    let para = Paragraph::new(span);
                    let inner = Rect::new(cx, cy, 1, 1);
                    f.render_widget(para, inner)
                }
                f.set_cursor(cx, cy);
}

de4db1ff avatar May 26 '24 14:05 de4db1ff

~~I've just confirmed that this is not a bug on the iamb side. It’s actually a bug in ratatui.~~

Reference
  • https://github.com/simnalamburt/ratatui-practice
  • https://docs.rs/unicode-width/

simnalamburt avatar Sep 26 '24 17:09 simnalamburt

Sorry for the confusion. I've just confirmed that it’s actually a bug on the iamb side. I was able to create a small PoC and patch for it. Diff: (40d92a2)

@@ -28,7 +28,7 @@ impl State {
     }

     fn cursor(&self) -> u16 {
-        self.idx_chars as u16
+        self.input[..self.idx_bytes()].width_cjk() as u16
     }
 }

Before:

After:

Reference
  • https://github.com/simnalamburt/ratatui-practice

simnalamburt avatar Sep 28 '24 18:09 simnalamburt

@ulyssa I’m willing to fix this issue on my own, but I’m not familiar with the iamb codebase. Could you give me a rough hint on where I should start?

simnalamburt avatar Sep 28 '24 18:09 simnalamburt

@simnalamburt thank you for looking into this! You will want to update how coff is calculated here and here to use the unicode-width crate.

ulyssa avatar Sep 29 '24 19:09 ulyssa

Thanks for the guide :) Fixed it

Reference
  • https://github.com/ulyssa/modalkit/pull/151
  • https://github.com/ulyssa/iamb/pull/358

simnalamburt avatar Sep 29 '24 20:09 simnalamburt

@ulyssa I'm afraid that this issue has not fixed yet since iamb is still using modalkit v0.0.20. See https://github.com/ulyssa/iamb/pull/358 for the fix. (Otherwise you can release modalkit v0.0.21)

simnalamburt avatar Nov 27 '24 06:11 simnalamburt