kkawakam / rustyline

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

Handle Ctrl-C differently depending on content #794

Closed xeruf closed 1 month ago

xeruf commented 1 month ago

When I receive an Interrupted Err, I do not get any information about the current line. So I wrote a ConditionalEventHandler for Ctrl-C - but none of the parameters seems to provide any information about the content in the line so far. And what the parameter positive means is entirely unclear to me.

This is what I have, but the event is always the Ctrl-C event so Interrupt is never triggered:

struct CtrlCHandler;
impl ConditionalEventHandler for CtrlCHandler {
    fn handle(&self, evt: &Event, n: RepeatCount, positive: bool, ctx: &EventContext) -> Option<Cmd> {
        Some(if evt.get(0).is_some() {
            Cmd::Kill(Movement::WholeLine)
        } else {
            Cmd::Interrupt
        })
    }
}
gwenn commented 1 month ago

n: RepeatCount, positive: bool are used in emacs mode: https://github.com/kkawakam/rustyline/blob/db7209d9db5032a5ee566a2230565867d4c705af/src/keymap.rs#L523 but are irrelevant here.

You can find information about the current line here: https://docs.rs/rustyline/latest/rustyline/struct.EventContext.html#method.line

There is an example here: https://github.com/kkawakam/rustyline/blob/db7209d9db5032a5ee566a2230565867d4c705af/examples/custom_key_bindings.rs#L74-L83