pub mod handlers;
pub mod registry;
pub use handlers::{scroll_block_into_view, wrap_next_index, wrap_previous_index};
pub use registry::{
KeyContext, KeyExecutionContext, KeyHandlingContext, KeyResult, ModeAwareRegistry,
};
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum KeyLoopAction {
Continue,
Break,
}
pub fn build_mode_aware_registry(
stream_service: std::sync::Arc<crate::core::chat_stream::ChatStreamService>,
terminal: std::sync::Arc<
tokio::sync::Mutex<ratatui::Terminal<crate::ui::osc_backend::OscBackend<std::io::Stdout>>>,
>,
) -> ModeAwareRegistry {
use handlers::*;
use ratatui::crossterm::event::{KeyCode, KeyModifiers};
use registry::{KeyContext, KeyPattern, ModeAwareBuilder};
ModeAwareBuilder::new()
.register_for_context(
KeyContext::Typing,
KeyPattern::simple(KeyCode::Esc),
Box::new(EscapeHandler),
)
.register_for_context(
KeyContext::BlockSelect,
KeyPattern::simple(KeyCode::Esc),
Box::new(EscapeHandler),
)
.register_for_context(
KeyContext::InPlaceEdit,
KeyPattern::simple(KeyCode::Esc),
Box::new(EscapeHandler),
)
.register_for_context(
KeyContext::FilePrompt,
KeyPattern::simple(KeyCode::Esc),
Box::new(EscapeHandler),
)
.register_for_context(
KeyContext::McpPromptInput,
KeyPattern::simple(KeyCode::Esc),
Box::new(EscapeHandler),
)
.register_for_context(
KeyContext::Typing,
KeyPattern::ctrl(KeyCode::Char('l')),
Box::new(CtrlLHandler),
)
.register_for_context(
KeyContext::Typing,
KeyPattern::ctrl(KeyCode::Char('o')),
Box::new(CtrlOHandler),
)
.register_for_context(
KeyContext::EditSelect,
KeyPattern::ctrl(KeyCode::Char('l')),
Box::new(CtrlLHandler),
)
.register_for_context(
KeyContext::BlockSelect,
KeyPattern::ctrl(KeyCode::Char('l')),
Box::new(CtrlLHandler),
)
.register_for_context(
KeyContext::Typing,
KeyPattern::simple(KeyCode::F(4)),
Box::new(F4Handler),
)
.register_for_context(
KeyContext::InPlaceEdit,
KeyPattern::simple(KeyCode::F(4)),
Box::new(F4Handler),
)
.register_for_context(
KeyContext::InPlaceEdit,
KeyPattern::ctrl(KeyCode::Char('o')),
Box::new(CtrlOHandler),
)
.register_for_context(
KeyContext::EditSelect,
KeyPattern::simple(KeyCode::F(4)),
Box::new(F4Handler),
)
.register_for_context(
KeyContext::BlockSelect,
KeyPattern::simple(KeyCode::F(4)),
Box::new(F4Handler),
)
.register_for_context(
KeyContext::Typing,
KeyPattern::ctrl(KeyCode::Char('d')),
Box::new(CtrlDHandler),
)
.register_for_context(
KeyContext::ToolPrompt,
KeyPattern::ctrl(KeyCode::Char('o')),
Box::new(CtrlOHandler),
)
.register_for_context(
KeyContext::EditSelect,
KeyPattern::ctrl(KeyCode::Char('d')),
Box::new(CtrlDHandler),
)
.register_for_context(
KeyContext::BlockSelect,
KeyPattern::ctrl(KeyCode::Char('d')),
Box::new(CtrlDHandler),
)
.register_for_context(
KeyContext::Typing,
KeyPattern::simple(KeyCode::Home),
Box::new(NavigationHandler),
)
.register_for_context(
KeyContext::InPlaceEdit,
KeyPattern::simple(KeyCode::Home),
Box::new(NavigationHandler),
)
.register_for_context(
KeyContext::FilePrompt,
KeyPattern::simple(KeyCode::Home),
Box::new(NavigationHandler),
)
.register_for_context(
KeyContext::ToolPrompt,
KeyPattern::simple(KeyCode::Home),
Box::new(NavigationHandler),
)
.register_for_context(
KeyContext::Typing,
KeyPattern::simple(KeyCode::End),
Box::new(NavigationHandler),
)
.register_for_context(
KeyContext::InPlaceEdit,
KeyPattern::simple(KeyCode::End),
Box::new(NavigationHandler),
)
.register_for_context(
KeyContext::FilePrompt,
KeyPattern::simple(KeyCode::End),
Box::new(NavigationHandler),
)
.register_for_context(
KeyContext::ToolPrompt,
KeyPattern::simple(KeyCode::End),
Box::new(NavigationHandler),
)
.register_for_context(
KeyContext::Typing,
KeyPattern::simple(KeyCode::PageUp),
Box::new(NavigationHandler),
)
.register_for_context(
KeyContext::InPlaceEdit,
KeyPattern::simple(KeyCode::PageUp),
Box::new(NavigationHandler),
)
.register_for_context(
KeyContext::FilePrompt,
KeyPattern::simple(KeyCode::PageUp),
Box::new(NavigationHandler),
)
.register_for_context(
KeyContext::ToolPrompt,
KeyPattern::simple(KeyCode::PageUp),
Box::new(NavigationHandler),
)
.register_for_context(
KeyContext::Typing,
KeyPattern::simple(KeyCode::PageDown),
Box::new(NavigationHandler),
)
.register_for_context(
KeyContext::InPlaceEdit,
KeyPattern::simple(KeyCode::PageDown),
Box::new(NavigationHandler),
)
.register_for_context(
KeyContext::FilePrompt,
KeyPattern::simple(KeyCode::PageDown),
Box::new(NavigationHandler),
)
.register_for_context(
KeyContext::ToolPrompt,
KeyPattern::simple(KeyCode::PageDown),
Box::new(NavigationHandler),
)
.register_for_context(
KeyContext::Typing,
KeyPattern::simple(KeyCode::Up),
Box::new(ArrowKeyHandler),
)
.register_for_context(
KeyContext::InPlaceEdit,
KeyPattern::simple(KeyCode::Up),
Box::new(ArrowKeyHandler),
)
.register_for_context(
KeyContext::FilePrompt,
KeyPattern::simple(KeyCode::Up),
Box::new(ArrowKeyHandler),
)
.register_for_context(
KeyContext::ToolPrompt,
KeyPattern::simple(KeyCode::Up),
Box::new(ArrowKeyHandler),
)
.register_for_context(
KeyContext::Typing,
KeyPattern::simple(KeyCode::Down),
Box::new(ArrowKeyHandler),
)
.register_for_context(
KeyContext::InPlaceEdit,
KeyPattern::simple(KeyCode::Down),
Box::new(ArrowKeyHandler),
)
.register_for_context(
KeyContext::FilePrompt,
KeyPattern::simple(KeyCode::Down),
Box::new(ArrowKeyHandler),
)
.register_for_context(
KeyContext::ToolPrompt,
KeyPattern::simple(KeyCode::Down),
Box::new(ArrowKeyHandler),
)
.register_for_context(
KeyContext::Typing,
KeyPattern::simple(KeyCode::Left),
Box::new(ArrowKeyHandler),
)
.register_for_context(
KeyContext::InPlaceEdit,
KeyPattern::simple(KeyCode::Left),
Box::new(ArrowKeyHandler),
)
.register_for_context(
KeyContext::FilePrompt,
KeyPattern::simple(KeyCode::Left),
Box::new(ArrowKeyHandler),
)
.register_for_context(
KeyContext::ToolPrompt,
KeyPattern::simple(KeyCode::Left),
Box::new(ArrowKeyHandler),
)
.register_for_context(
KeyContext::Typing,
KeyPattern::simple(KeyCode::Right),
Box::new(ArrowKeyHandler),
)
.register_for_context(
KeyContext::InPlaceEdit,
KeyPattern::simple(KeyCode::Right),
Box::new(ArrowKeyHandler),
)
.register_for_context(
KeyContext::FilePrompt,
KeyPattern::simple(KeyCode::Right),
Box::new(ArrowKeyHandler),
)
.register_for_context(
KeyContext::ToolPrompt,
KeyPattern::simple(KeyCode::Right),
Box::new(ArrowKeyHandler),
)
.register_for_context(
KeyContext::Typing,
KeyPattern::with_modifiers(KeyCode::Up, KeyModifiers::SHIFT),
Box::new(ArrowKeyHandler),
)
.register_for_context(
KeyContext::InPlaceEdit,
KeyPattern::with_modifiers(KeyCode::Up, KeyModifiers::SHIFT),
Box::new(ArrowKeyHandler),
)
.register_for_context(
KeyContext::FilePrompt,
KeyPattern::with_modifiers(KeyCode::Up, KeyModifiers::SHIFT),
Box::new(ArrowKeyHandler),
)
.register_for_context(
KeyContext::ToolPrompt,
KeyPattern::with_modifiers(KeyCode::Up, KeyModifiers::SHIFT),
Box::new(ArrowKeyHandler),
)
.register_for_context(
KeyContext::Typing,
KeyPattern::with_modifiers(KeyCode::Down, KeyModifiers::SHIFT),
Box::new(ArrowKeyHandler),
)
.register_for_context(
KeyContext::InPlaceEdit,
KeyPattern::with_modifiers(KeyCode::Down, KeyModifiers::SHIFT),
Box::new(ArrowKeyHandler),
)
.register_for_context(
KeyContext::FilePrompt,
KeyPattern::with_modifiers(KeyCode::Down, KeyModifiers::SHIFT),
Box::new(ArrowKeyHandler),
)
.register_for_context(
KeyContext::ToolPrompt,
KeyPattern::with_modifiers(KeyCode::Down, KeyModifiers::SHIFT),
Box::new(ArrowKeyHandler),
)
.register_for_context(
KeyContext::Typing,
KeyPattern::with_modifiers(KeyCode::Left, KeyModifiers::SHIFT),
Box::new(ArrowKeyHandler),
)
.register_for_context(
KeyContext::InPlaceEdit,
KeyPattern::with_modifiers(KeyCode::Left, KeyModifiers::SHIFT),
Box::new(ArrowKeyHandler),
)
.register_for_context(
KeyContext::FilePrompt,
KeyPattern::with_modifiers(KeyCode::Left, KeyModifiers::SHIFT),
Box::new(ArrowKeyHandler),
)
.register_for_context(
KeyContext::ToolPrompt,
KeyPattern::with_modifiers(KeyCode::Left, KeyModifiers::SHIFT),
Box::new(ArrowKeyHandler),
)
.register_for_context(
KeyContext::Typing,
KeyPattern::with_modifiers(KeyCode::Right, KeyModifiers::SHIFT),
Box::new(ArrowKeyHandler),
)
.register_for_context(
KeyContext::InPlaceEdit,
KeyPattern::with_modifiers(KeyCode::Right, KeyModifiers::SHIFT),
Box::new(ArrowKeyHandler),
)
.register_for_context(
KeyContext::FilePrompt,
KeyPattern::with_modifiers(KeyCode::Right, KeyModifiers::SHIFT),
Box::new(ArrowKeyHandler),
)
.register_for_context(
KeyContext::ToolPrompt,
KeyPattern::with_modifiers(KeyCode::Right, KeyModifiers::SHIFT),
Box::new(ArrowKeyHandler),
)
.register_for_context(
KeyContext::Typing,
KeyPattern::ctrl(KeyCode::Char('a')),
Box::new(TextEditingHandler),
)
.register_for_context(
KeyContext::Typing,
KeyPattern::ctrl(KeyCode::Char('e')),
Box::new(TextEditingHandler),
)
.register_for_context(
KeyContext::Typing,
KeyPattern::simple(KeyCode::Delete),
Box::new(TextEditingHandler),
)
.register_for_context(
KeyContext::Typing,
KeyPattern::simple(KeyCode::Backspace),
Box::new(TextEditingHandler),
)
.register_for_context(
KeyContext::Typing,
KeyPattern::ctrl(KeyCode::Char('b')),
Box::new(CtrlBHandler),
)
.register_for_context(
KeyContext::BlockSelect,
KeyPattern::ctrl(KeyCode::Char('b')),
Box::new(CtrlBHandler),
)
.register_for_context(
KeyContext::Typing,
KeyPattern::ctrl(KeyCode::Char('p')),
Box::new(CtrlPHandler),
)
.register_for_context(
KeyContext::EditSelect,
KeyPattern::ctrl(KeyCode::Char('p')),
Box::new(CtrlPHandler),
)
.register_for_context(
KeyContext::Typing,
KeyPattern::ctrl(KeyCode::Char('x')),
Box::new(CtrlXHandler),
)
.register_for_context(
KeyContext::EditSelect,
KeyPattern::ctrl(KeyCode::Char('x')),
Box::new(CtrlXHandler),
)
.register_for_context(
KeyContext::Typing,
KeyPattern::ctrl(KeyCode::Char('j')),
Box::new(CtrlJHandler {
stream_service: stream_service.clone(),
}),
)
.register_for_context(
KeyContext::InPlaceEdit,
KeyPattern::ctrl(KeyCode::Char('j')),
Box::new(CtrlJHandler {
stream_service: stream_service.clone(),
}),
)
.register_for_context(
KeyContext::Typing,
KeyPattern::simple(KeyCode::Enter),
Box::new(EnterHandler {
stream_service: stream_service.clone(),
}),
)
.register_for_context(
KeyContext::Typing,
KeyPattern::with_modifiers(KeyCode::Enter, KeyModifiers::ALT),
Box::new(AltEnterHandler {
stream_service: stream_service.clone(),
}),
)
.register_for_context(
KeyContext::FilePrompt,
KeyPattern::simple(KeyCode::Enter),
Box::new(EnterHandler {
stream_service: stream_service.clone(),
}),
)
.register_for_context(
KeyContext::McpPromptInput,
KeyPattern::simple(KeyCode::Enter),
Box::new(EnterHandler {
stream_service: stream_service.clone(),
}),
)
.register_for_context(
KeyContext::FilePrompt,
KeyPattern::with_modifiers(KeyCode::Enter, KeyModifiers::ALT),
Box::new(AltEnterHandler {
stream_service: stream_service.clone(),
}),
)
.register_for_context(
KeyContext::McpPromptInput,
KeyPattern::with_modifiers(KeyCode::Enter, KeyModifiers::ALT),
Box::new(AltEnterHandler {
stream_service: stream_service.clone(),
}),
)
.register_for_context(
KeyContext::InPlaceEdit,
KeyPattern::simple(KeyCode::Enter),
Box::new(EnterHandler {
stream_service: stream_service.clone(),
}),
)
.register_for_context(
KeyContext::InPlaceEdit,
KeyPattern::with_modifiers(KeyCode::Enter, KeyModifiers::ALT),
Box::new(AltEnterHandler {
stream_service: stream_service.clone(),
}),
)
.register_for_context(
KeyContext::Typing,
KeyPattern::ctrl(KeyCode::Char('r')),
Box::new(CtrlRHandler),
)
.register_for_context(
KeyContext::Typing,
KeyPattern::ctrl(KeyCode::Char('n')),
Box::new(CtrlNHandler),
)
.register_for_context(
KeyContext::Typing,
KeyPattern::ctrl(KeyCode::Char('t')),
Box::new(CtrlTHandler {
terminal: terminal.clone(),
}),
)
.register_for_context(
KeyContext::Typing,
KeyPattern::ctrl(KeyCode::Char('c')),
Box::new(CtrlCHandler),
)
.register_for_context(
KeyContext::Picker,
KeyPattern::ctrl(KeyCode::Char('c')),
Box::new(CtrlCHandler),
)
.register_for_context(
KeyContext::EditSelect,
KeyPattern::ctrl(KeyCode::Char('c')),
Box::new(CtrlCHandler),
)
.register_for_context(
KeyContext::BlockSelect,
KeyPattern::ctrl(KeyCode::Char('c')),
Box::new(CtrlCHandler),
)
.register_for_context(
KeyContext::InPlaceEdit,
KeyPattern::ctrl(KeyCode::Char('c')),
Box::new(CtrlCHandler),
)
.register_for_context(
KeyContext::FilePrompt,
KeyPattern::ctrl(KeyCode::Char('c')),
Box::new(CtrlCHandler),
)
.register_for_context(
KeyContext::McpPromptInput,
KeyPattern::ctrl(KeyCode::Char('c')),
Box::new(CtrlCHandler),
)
.register_for_context(
KeyContext::ToolPrompt,
KeyPattern::ctrl(KeyCode::Char('c')),
Box::new(CtrlCHandler),
)
.register_for_context(
KeyContext::EditSelect,
KeyPattern::any(),
Box::new(EditSelectHandler),
)
.register_for_context(
KeyContext::BlockSelect,
KeyPattern::any(),
Box::new(BlockSelectHandler),
)
.register_for_context(
KeyContext::Picker,
KeyPattern::any(),
Box::new(PickerHandler),
)
.register_for_context(
KeyContext::ToolPrompt,
KeyPattern::any(),
Box::new(ToolPromptDecisionHandler),
)
.build()
}