| 1 | //! `/hotbar` command. |
| 2 | |
| 3 | use codewhale_command_contract::handler::CommandHandler; |
| 4 | use codewhale_command_contract::metadata::{CommandInfo, RegisterCommand}; |
| 5 | use codewhale_command_contract::outcome::{ |
| 6 | CoreNavigationAction as Action, CoreNavigationCommandResult as CommandResult, |
| 7 | }; |
| 8 | |
| 9 | pub const COMMAND_INFO: CommandInfo = CommandInfo { |
| 10 | name: "hotbar", |
| 11 | aliases: &["hotkeys"], |
| 12 | usage: "/hotbar", |
| 13 | description_key: "cmd_hotbar_description", |
| 14 | }; |
| 15 | |
| 16 | pub struct HotbarCmd; |
| 17 | |
| 18 | impl RegisterCommand<CommandResult> for HotbarCmd { |
| 19 | fn info() -> &'static CommandInfo { |
| 20 | &COMMAND_INFO |
| 21 | } |
| 22 | |
| 23 | fn handler() -> CommandHandler<CommandResult> { |
| 24 | CommandHandler::Pure(hotbar) |
| 25 | } |
| 26 | } |
| 27 | |
| 28 | fn hotbar(arg: Option<&str>) -> CommandResult { |
| 29 | match arg.map(str::trim).filter(|arg| !arg.is_empty()) { |
| 30 | None | Some("setup" | "edit" | "configure" | "config") => { |
| 31 | CommandResult::action(Action::OpenHotbarSetup) |
| 32 | } |
| 33 | // Hide the Hotbar: persist `hotbar = []` and clear the live slots. |
| 34 | Some("off" | "disable" | "hide") => CommandResult::action(Action::DisableHotbar), |
| 35 | // Restore the default recommended slots (explicit reset). |
| 36 | Some("on" | "reset" | "defaults" | "default") => { |
| 37 | CommandResult::action(Action::RestoreHotbarDefaults) |
| 38 | } |
| 39 | Some("help" | "?") => CommandResult::message( |
| 40 | "Hotbar gives you Alt+1 through Alt+8 shortcuts (Option key on macOS, Alt \ |
| 41 | elsewhere). Use `/hotbar` to customize, `/hotbar off` to hide it \ |
| 42 | (`hotbar = []`), and `/hotbar on` to restore the default slots. \ |
| 43 | Hotbar slots dispatch only when no modal, inline picker, or \ |
| 44 | onboarding surface owns input. Bare 1-8 insert text in the \ |
| 45 | composer. Cmd-number and F-keys are not Hotbar shortcuts unless \ |
| 46 | a future release documents and implements them.", |
| 47 | ), |
| 48 | Some(other) => CommandResult::error(format!( |
| 49 | "Unknown /hotbar target '{other}'. Try `/hotbar`, `/hotbar off`, \ |
| 50 | `/hotbar on`, or `/hotbar help`." |
| 51 | )), |
| 52 | } |
| 53 | } |
| 54 | |
| 55 | #[cfg(test)] |
| 56 | mod tests { |
| 57 | use super::*; |
| 58 | |
| 59 | #[test] |
| 60 | fn hotbar_command_opens_setup_view() { |
| 61 | let result = hotbar(None); |
| 62 | assert_eq!(result.action, Some(Action::OpenHotbarSetup)); |
| 63 | assert!(result.message.is_none()); |
| 64 | } |
| 65 | |
| 66 | #[test] |
| 67 | fn hotbar_setup_alias_opens_setup_view() { |
| 68 | let result = hotbar(Some("setup")); |
| 69 | assert_eq!(result.action, Some(Action::OpenHotbarSetup)); |
| 70 | assert!(result.message.is_none()); |
| 71 | } |
| 72 | |
| 73 | #[test] |
| 74 | fn hotbar_help_arg_explains_customize_and_disable() { |
| 75 | let result = hotbar(Some("help")); |
| 76 | assert!(!result.is_error); |
| 77 | assert!(result.action.is_none()); |
| 78 | let message = result |
| 79 | .message |
| 80 | .as_deref() |
| 81 | .expect("help should return a message"); |
| 82 | assert!( |
| 83 | message.contains("/hotbar") |
| 84 | && message.contains("customize") |
| 85 | && message.contains("Alt+1 through Alt+8"), |
| 86 | "help should point at /hotbar to customize: {message:?}" |
| 87 | ); |
| 88 | assert!( |
| 89 | message.contains("hotbar = []"), |
| 90 | "help should mention the explicit disabled config: {message:?}" |
| 91 | ); |
| 92 | assert!( |
| 93 | message.contains("/hotbar off") && message.contains("/hotbar on"), |
| 94 | "help should mention both disable and restore paths: {message:?}" |
| 95 | ); |
| 96 | assert!(message.contains("Bare 1-8 insert text")); |
| 97 | assert!(message.contains("Cmd-number and F-keys are not Hotbar shortcuts")); |
| 98 | } |
| 99 | |
| 100 | #[test] |
| 101 | fn hotbar_off_and_disable_aliases_return_disable_action() { |
| 102 | for arg in ["off", "disable", "hide"] { |
| 103 | let result = hotbar(Some(arg)); |
| 104 | assert_eq!( |
| 105 | result.action, |
| 106 | Some(Action::DisableHotbar), |
| 107 | "`/hotbar {arg}` should disable the hotbar" |
| 108 | ); |
| 109 | assert!( |
| 110 | result.message.is_none(), |
| 111 | "`/hotbar {arg}` should not also emit a message" |
| 112 | ); |
| 113 | } |
| 114 | } |
| 115 | |
| 116 | #[test] |
| 117 | fn hotbar_on_and_reset_aliases_return_restore_action() { |
| 118 | for arg in ["on", "reset", "defaults", "default"] { |
| 119 | let result = hotbar(Some(arg)); |
| 120 | assert_eq!( |
| 121 | result.action, |
| 122 | Some(Action::RestoreHotbarDefaults), |
| 123 | "`/hotbar {arg}` should restore default hotbar slots" |
| 124 | ); |
| 125 | assert!(result.message.is_none()); |
| 126 | } |
| 127 | } |
| 128 | |
| 129 | #[test] |
| 130 | fn hotbar_unknown_arg_reports_error() { |
| 131 | let result = hotbar(Some("bogus")); |
| 132 | assert!(result.is_error); |
| 133 | assert!(result.action.is_none()); |
| 134 | assert!( |
| 135 | result |
| 136 | .message |
| 137 | .as_deref() |
| 138 | .is_some_and(|message| message.contains("Unknown /hotbar target 'bogus'")) |
| 139 | ); |
| 140 | } |
| 141 | } |
| 142 |