Skip to content

Commit

Permalink
feat: handle repeat key events using crossterm backend
Browse files Browse the repository at this point in the history
  • Loading branch information
cxreiff authored and sayanarijit committed Nov 27, 2024
1 parent a0eca90 commit 75b14d5
Showing 1 changed file with 52 additions and 36 deletions.
88 changes: 52 additions & 36 deletions src/backend/crossterm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,43 +19,45 @@ pub fn to_input_request(evt: &CrosstermEvent) -> Option<InputRequest> {
modifiers,
kind,
state: _,
}) if *kind == KeyEventKind::Press => match (*code, *modifiers) {
(Backspace, KeyModifiers::NONE) | (Char('h'), KeyModifiers::CONTROL) => {
Some(DeletePrevChar)
}) if *kind == KeyEventKind::Press || *kind == KeyEventKind::Repeat => {
match (*code, *modifiers) {
(Backspace, KeyModifiers::NONE) | (Char('h'), KeyModifiers::CONTROL) => {
Some(DeletePrevChar)
}
(Delete, KeyModifiers::NONE) => Some(DeleteNextChar),
(Tab, KeyModifiers::NONE) => None,
(Left, KeyModifiers::NONE) | (Char('b'), KeyModifiers::CONTROL) => {
Some(GoToPrevChar)
}
(Left, KeyModifiers::CONTROL) | (Char('b'), KeyModifiers::META) => {
Some(GoToPrevWord)
}
(Right, KeyModifiers::NONE) | (Char('f'), KeyModifiers::CONTROL) => {
Some(GoToNextChar)
}
(Right, KeyModifiers::CONTROL) | (Char('f'), KeyModifiers::META) => {
Some(GoToNextWord)
}
(Char('u'), KeyModifiers::CONTROL) => Some(DeleteLine),

(Char('w'), KeyModifiers::CONTROL)
| (Char('d'), KeyModifiers::META)
| (Backspace, KeyModifiers::META)
| (Backspace, KeyModifiers::ALT) => Some(DeletePrevWord),

(Delete, KeyModifiers::CONTROL) => Some(DeleteNextWord),
(Char('k'), KeyModifiers::CONTROL) => Some(DeleteTillEnd),
(Char('a'), KeyModifiers::CONTROL) | (Home, KeyModifiers::NONE) => {
Some(GoToStart)
}
(Char('e'), KeyModifiers::CONTROL) | (End, KeyModifiers::NONE) => {
Some(GoToEnd)
}
(Char(c), KeyModifiers::NONE) => Some(InsertChar(c)),
(Char(c), KeyModifiers::SHIFT) => Some(InsertChar(c)),
(_, _) => None,
}
(Delete, KeyModifiers::NONE) => Some(DeleteNextChar),
(Tab, KeyModifiers::NONE) => None,
(Left, KeyModifiers::NONE) | (Char('b'), KeyModifiers::CONTROL) => {
Some(GoToPrevChar)
}
(Left, KeyModifiers::CONTROL) | (Char('b'), KeyModifiers::META) => {
Some(GoToPrevWord)
}
(Right, KeyModifiers::NONE) | (Char('f'), KeyModifiers::CONTROL) => {
Some(GoToNextChar)
}
(Right, KeyModifiers::CONTROL) | (Char('f'), KeyModifiers::META) => {
Some(GoToNextWord)
}
(Char('u'), KeyModifiers::CONTROL) => Some(DeleteLine),

(Char('w'), KeyModifiers::CONTROL)
| (Char('d'), KeyModifiers::META)
| (Backspace, KeyModifiers::META)
| (Backspace, KeyModifiers::ALT) => Some(DeletePrevWord),

(Delete, KeyModifiers::CONTROL) => Some(DeleteNextWord),
(Char('k'), KeyModifiers::CONTROL) => Some(DeleteTillEnd),
(Char('a'), KeyModifiers::CONTROL) | (Home, KeyModifiers::NONE) => {
Some(GoToStart)
}
(Char('e'), KeyModifiers::CONTROL) | (End, KeyModifiers::NONE) => {
Some(GoToEnd)
}
(Char(c), KeyModifiers::NONE) => Some(InsertChar(c)),
(Char(c), KeyModifiers::SHIFT) => Some(InsertChar(c)),
(_, _) => None,
},
}
_ => None,
}
}
Expand Down Expand Up @@ -135,4 +137,18 @@ mod tests {

assert!(req.is_none());
}

#[test]
fn handle_repeat() {
let evt = CrosstermEvent::Key(KeyEvent {
code: KeyCode::Char('a'),
modifiers: KeyModifiers::NONE,
kind: KeyEventKind::Repeat,
state: KeyEventState::NONE,
});

let req = to_input_request(&evt);

assert_eq!(req, Some(InputRequest::InsertChar('a')));
}
}

0 comments on commit 75b14d5

Please sign in to comment.