| 1 | use super::CommandResult; |
| 2 | use super::groups::core::navigation::portable_handlers; |
| 3 | use super::traits::{Command, ContextualCommand}; |
| 4 | use crate::tui::app::{App, AppAction}; |
| 5 | use codewhale_command_contract::handler::CommandHandler; |
| 6 | use codewhale_command_contract::metadata::{CommandInfo, RegisterCommand}; |
| 7 | use codewhale_command_contract::outcome::{CoreNavigationAction, CoreNavigationCommandResult}; |
| 8 | |
| 9 | struct Registration<const INDEX: usize>; |
| 10 | |
| 11 | impl<const INDEX: usize> RegisterCommand<CommandResult> for Registration<INDEX> { |
| 12 | fn info() -> &'static CommandInfo { |
| 13 | portable_handlers()[INDEX].0 |
| 14 | } |
| 15 | |
| 16 | fn handler() -> CommandHandler<CommandResult> { |
| 17 | match portable_handlers()[INDEX].1 { |
| 18 | CommandHandler::Pure(_) => { |
| 19 | CommandHandler::Pure(|args| match portable_handlers()[INDEX].1 { |
| 20 | CommandHandler::Pure(run) => host_result(run(args)), |
| 21 | _ => CommandResult::error("navigation handler shape changed"), |
| 22 | }) |
| 23 | } |
| 24 | CommandHandler::Contextual { capabilities, .. } => CommandHandler::Contextual { |
| 25 | capabilities, |
| 26 | handler: |contexts, args| match portable_handlers()[INDEX].1 { |
| 27 | CommandHandler::Contextual { handler, .. } => { |
| 28 | host_result(handler(contexts, args)) |
| 29 | } |
| 30 | _ => CommandResult::error("navigation handler shape changed"), |
| 31 | }, |
| 32 | }, |
| 33 | } |
| 34 | } |
| 35 | } |
| 36 | |
| 37 | fn host_result(result: CoreNavigationCommandResult) -> CommandResult { |
| 38 | CommandResult { |
| 39 | message: result.message, |
| 40 | action: result.action.map(|action| match action { |
| 41 | CoreNavigationAction::OpenHotbarSetup => AppAction::OpenHotbarSetup, |
| 42 | CoreNavigationAction::DisableHotbar => AppAction::DisableHotbar, |
| 43 | CoreNavigationAction::RestoreHotbarDefaults => AppAction::RestoreHotbarDefaults, |
| 44 | CoreNavigationAction::OpenLiveTranscript => AppAction::OpenLiveTranscript, |
| 45 | CoreNavigationAction::OpenTurnInspector => AppAction::OpenTurnInspector, |
| 46 | CoreNavigationAction::OpenWorktreeManager => AppAction::OpenWorktreeManager, |
| 47 | CoreNavigationAction::OpenAgentBoard => AppAction::OpenAgentBoard, |
| 48 | CoreNavigationAction::SwitchWorkspace { workspace } => { |
| 49 | AppAction::SwitchWorkspace { workspace } |
| 50 | } |
| 51 | CoreNavigationAction::VoiceCapture => AppAction::VoiceCapture, |
| 52 | }), |
| 53 | is_error: result.is_error, |
| 54 | } |
| 55 | } |
| 56 | |
| 57 | pub(in crate::commands) fn dispatch(app: &mut App, name: &str, arg: Option<&str>) -> CommandResult { |
| 58 | let Some(handler) = super::registry() |
| 59 | .get(name) |
| 60 | .and_then(Command::contextual_handler) |
| 61 | else { |
| 62 | return CommandResult::error(format!("Navigation command unavailable: {name}")); |
| 63 | }; |
| 64 | match handler { |
| 65 | CommandHandler::Pure(run) => run(arg), |
| 66 | CommandHandler::Contextual { |
| 67 | capabilities, |
| 68 | handler, |
| 69 | } => { |
| 70 | let mut bundle = app.command_contexts_with_config(None); |
| 71 | handler(bundle.contexts(capabilities), arg) |
| 72 | } |
| 73 | } |
| 74 | } |
| 75 | |
| 76 | pub(in crate::commands) fn registration<const INDEX: usize>() -> Box<dyn Command> { |
| 77 | Box::new( |
| 78 | ContextualCommand::from_contract::<Registration<INDEX>>().expect("navigation registration"), |
| 79 | ) |
| 80 | } |
| 81 |