kkawakam / rustyline

Readline Implementation in Rust
https://crates.io/crates/rustyline/
MIT License
1.54k stars 176 forks source link

Line highlighting does not begin until backspace is pressed #768

Open zesterer opened 6 months ago

zesterer commented 6 months ago

I'm implementing some simple syntax highlighting based on example.rs. Syntax highlighting only seems to occur once backspace is pressed, and not after each keystroke (i.e: the input line appears unhighlighted until I press backspace at the end of the line, triggering a highlight).

At first I thought this was an issue in my own code, but I confirmed this to not be the case by placing if line.len() > 0 { panic!(); } within the body of my Highlighter::highlight function (the > 0 check is just to prevent a panic happening once at startup). As suspected, rustyline allows me to input whatever characters I want, only panicking if backspace is pressed.

zesterer commented 6 months ago

Actually, my mistake: it seems that the default implementation of the highlight_char method returns false in all cases, so I needed to override this. I do think this is unexpected behaviour though: if not overriding it leads to incorrect behaviour, it should either (a) have a default implementation that does produce correct behaviour (return true) or (b) not have a default implementation.

I'll keep the issue open since this still seems like an unexpected default behaviour.

gwenn commented 6 months ago

As explain in highlight_char, rustyline must refresh the whole buffer at each key stroke if set to true. You can can try to input a very large multiline input buffer and do some random change or just move the cursor. I expect you to see some slowness...

zesterer commented 6 months ago

I definitely understand that argument, but that only demonstrates that providing a default implementation of those methods is problematic: it took me quite a while to figure out that there was a method I was, in fact, supposed to override in order to produce the correct behaviour.

gwenn commented 6 months ago

I hope that one day rustyline will have the logic to redraw only the dirty cells / lines like textmode and change the default behaviour of highlight_char.