66 lines
2.9 KiB
Rust
66 lines
2.9 KiB
Rust
//! Key code <-> wire-serializable `KeyAction` conversion functions.
|
|||
|
|
//!
|
||
|
|
//! Map `crossterm::event::KeyCode` to and from the IPC protocol's `KeyAction`
|
||
|
|
//! enum. Both directions are total (every `KeyAction` has a `KeyCode`), but
|
||
|
|
//! `key_code_to_action` returns `None` for key codes with no IPC equivalent
|
||
|
|
//! (e.g. media keys), which are silently dropped by the caller.
|
||
|
|
//!
|
||
|
|
//! Flow: daemon receives `KeyAction` over IPC → `key_action_to_code` →
|
||
|
|
//! reconstructs `crossterm::KeyEvent` → feeds into `controller::input::handle_key`.
|
||
|
|
//! The inverse (`key_code_to_action`) is used by the attach-mode client to
|
||
|
|
//! serialise a local terminal key press before sending it over the socket.
|
||
|
|
|
||
|
|
use crossterm::event::KeyCode;
|
||
|
|
use zesdex_infrastructure::ipc::protocol::KeyAction;
|
||
|
|
|
||
|
|
/// Map a `crossterm` key code to the wire-serializable `KeyAction`, for
|
||
|
|
/// sending key input from an attached client to the daemon.
|
||
|
|
///
|
||
|
|
/// Return: `None` for key codes with no `KeyAction` equivalent (e.g.
|
||
|
|
/// media keys), which are silently dropped.
|
||
|
|
pub fn key_code_to_action(code: KeyCode) -> Option<KeyAction> {
|
||
|
|
tracing::debug!("converting key code to action: {:?}", code);
|
||
|
|
match code {
|
||
|
|
KeyCode::Char(c) => Some(KeyAction::Char(c)),
|
||
|
|
KeyCode::Enter => Some(KeyAction::Enter),
|
||
|
|
KeyCode::Esc => Some(KeyAction::Escape),
|
||
|
|
KeyCode::Backspace => Some(KeyAction::Backspace),
|
||
|
|
KeyCode::Delete => Some(KeyAction::Delete),
|
||
|
|
KeyCode::Tab => Some(KeyAction::Tab),
|
||
|
|
KeyCode::Up => Some(KeyAction::Up),
|
||
|
|
KeyCode::Down => Some(KeyAction::Down),
|
||
|
|
KeyCode::Left => Some(KeyAction::Left),
|
||
|
|
KeyCode::Right => Some(KeyAction::Right),
|
||
|
|
KeyCode::Home => Some(KeyAction::Home),
|
||
|
|
KeyCode::End => Some(KeyAction::End),
|
||
|
|
KeyCode::PageUp => Some(KeyAction::PageUp),
|
||
|
|
KeyCode::PageDown => Some(KeyAction::PageDown),
|
||
|
|
KeyCode::F(n) => Some(KeyAction::Function(n)),
|
||
|
|
_ => None,
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
/// Inverse of `key_code_to_action`: reconstruct a `crossterm::KeyCode`
|
||
|
|
/// from a `KeyAction` received over IPC, for replaying it into the
|
||
|
|
/// daemon's normal key-handling path.
|
||
|
|
pub fn key_action_to_code(action: &KeyAction) -> KeyCode {
|
||
|
|
tracing::debug!("converting key action to code: {:?}", action);
|
||
|
|
match action {
|
||
|
|
KeyAction::Char(c) => KeyCode::Char(*c),
|
||
|
|
KeyAction::Enter => KeyCode::Enter,
|
||
|
|
KeyAction::Escape => KeyCode::Esc,
|
||
|
|
KeyAction::Backspace => KeyCode::Backspace,
|
||
|
|
KeyAction::Delete => KeyCode::Delete,
|
||
|
|
KeyAction::Tab => KeyCode::Tab,
|
||
|
|
KeyAction::Up => KeyCode::Up,
|
||
|
|
KeyAction::Down => KeyCode::Down,
|
||
|
|
KeyAction::Left => KeyCode::Left,
|
||
|
|
KeyAction::Right => KeyCode::Right,
|
||
|
|
KeyAction::Home => KeyCode::Home,
|
||
|
|
KeyAction::End => KeyCode::End,
|
||
|
|
KeyAction::PageUp => KeyCode::PageUp,
|
||
|
|
KeyAction::PageDown => KeyCode::PageDown,
|
||
|
|
KeyAction::Function(n) => KeyCode::F(*n),
|
||
|
|
}
|
||
|
|
}
|