Skip to content

Commit

Permalink
refactor converting termion key event into Input
Browse files Browse the repository at this point in the history
  • Loading branch information
rhysd committed Aug 2, 2024
1 parent 0e32eed commit 3937eca
Showing 1 changed file with 23 additions and 58 deletions.
81 changes: 23 additions & 58 deletions src/input/termion.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,71 +15,36 @@ impl From<Event> for Input {
impl From<KeyEvent> for Input {
/// Convert [`termion::event::Key`] into [`Input`].
fn from(key: KeyEvent) -> Self {
let mut ctrl = false;
let mut alt = false;
let mut shift = false;
let (ctrl, alt, shift) = match key {
KeyEvent::Ctrl(_)
| KeyEvent::CtrlUp
| KeyEvent::CtrlRight
| KeyEvent::CtrlDown
| KeyEvent::CtrlLeft => (true, false, false),
KeyEvent::Alt(_)
| KeyEvent::AltUp
| KeyEvent::AltRight
| KeyEvent::AltDown
| KeyEvent::AltLeft => (false, true, false),
KeyEvent::ShiftUp
| KeyEvent::ShiftRight
| KeyEvent::ShiftDown
| KeyEvent::ShiftLeft => (false, false, true),
_ => (false, false, false),
};

let key = match key {
KeyEvent::Char('\n' | '\r') => Key::Enter,
KeyEvent::Char(c) => Key::Char(c),
KeyEvent::Ctrl(c) => {
ctrl = true;
Key::Char(c)
}
KeyEvent::Alt(c) => {
alt = true;
Key::Char(c)
}
KeyEvent::Char(c) | KeyEvent::Ctrl(c) | KeyEvent::Alt(c) => Key::Char(c),
KeyEvent::Backspace => Key::Backspace,
KeyEvent::Left => Key::Left,
KeyEvent::ShiftLeft => {
shift = true;
Key::Left
}
KeyEvent::AltLeft => {
alt = true;
Key::Left
}
KeyEvent::CtrlLeft => {
ctrl = true;
KeyEvent::Left | KeyEvent::CtrlLeft | KeyEvent::AltLeft | KeyEvent::ShiftLeft => {
Key::Left
}
KeyEvent::Right => Key::Right,
KeyEvent::ShiftRight => {
shift = true;
Key::Right
}
KeyEvent::AltRight => {
alt = true;
KeyEvent::Right | KeyEvent::CtrlRight | KeyEvent::AltRight | KeyEvent::ShiftRight => {
Key::Right
}
KeyEvent::CtrlRight => {
ctrl = true;
Key::Right
}
KeyEvent::Up => Key::Up,
KeyEvent::ShiftUp => {
shift = true;
Key::Up
}
KeyEvent::AltUp => {
alt = true;
Key::Up
}
KeyEvent::CtrlUp => {
ctrl = true;
Key::Up
}
KeyEvent::Down => Key::Down,
KeyEvent::ShiftDown => {
shift = true;
Key::Down
}
KeyEvent::AltDown => {
alt = true;
Key::Down
}
KeyEvent::CtrlDown => {
ctrl = true;
KeyEvent::Up | KeyEvent::CtrlUp | KeyEvent::AltUp | KeyEvent::ShiftUp => Key::Up,
KeyEvent::Down | KeyEvent::CtrlDown | KeyEvent::AltDown | KeyEvent::ShiftDown => {
Key::Down
}
KeyEvent::Home => Key::Home,
Expand Down

0 comments on commit 3937eca

Please sign in to comment.