ulyssa / iamb

A Matrix client for Vim addicts
https://iamb.chat
Apache License 2.0
608 stars 45 forks source link

Buggy Unicode Support #293

Open de4db1ff opened 4 months ago

de4db1ff commented 4 months ago

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);
}
simnalamburt commented 1 week ago

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

Reference
simnalamburt commented 6 days ago

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
simnalamburt commented 6 days ago

@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?

ulyssa commented 5 days ago

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

simnalamburt commented 5 days ago

Thanks for the guide :) Fixed it

Reference