| 1 | use super::CommandResult; |
| 2 | use codewhale_command_contract::facets::{ |
| 3 | CommandPresentationContext, CommandSessionControlContext, |
| 4 | }; |
| 5 | use codewhale_command_contract::handler::{CommandContexts, CommandHandler}; |
| 6 | use codewhale_command_contract::metadata::{ |
| 7 | CommandInfo as ContractInfo, RegisterCommand as ContractRegisterCommand, |
| 8 | }; |
| 9 | |
| 10 | pub(in crate::commands) const COMPOSER_COLORS: [&str; 8] = [ |
| 11 | "red", "orange", "amber", "green", "cyan", "blue", "purple", "pink", |
| 12 | ]; |
| 13 | |
| 14 | pub(in crate::commands) struct ColorCmd; |
| 15 | |
| 16 | pub(in crate::commands) const CONTRACT_INFO: ContractInfo = ContractInfo { |
| 17 | name: "color", |
| 18 | aliases: &["colour"], |
| 19 | usage: "/color [red|orange|amber|green|cyan|blue|purple|pink|off]", |
| 20 | description_key: "cmd_color_description", |
| 21 | }; |
| 22 | |
| 23 | impl ContractRegisterCommand<CommandResult> for ColorCmd { |
| 24 | fn info() -> &'static ContractInfo { |
| 25 | &CONTRACT_INFO |
| 26 | } |
| 27 | fn handler() -> CommandHandler<CommandResult> { |
| 28 | CommandHandler::Contextual { |
| 29 | capabilities: codewhale_command_contract::handler::CommandCapabilities::SESSION_CONTROL |
| 30 | .union(codewhale_command_contract::handler::CommandCapabilities::PRESENTATION), |
| 31 | handler: color_contextual, |
| 32 | } |
| 33 | } |
| 34 | } |
| 35 | |
| 36 | pub(in crate::commands) fn color_contextual( |
| 37 | contexts: CommandContexts<'_>, |
| 38 | arg: Option<&str>, |
| 39 | ) -> CommandResult { |
| 40 | let mut parts = contexts.into_parts(); |
| 41 | let Some(control) = parts.control.as_deref_mut() else { |
| 42 | return CommandResult::error("Command capability unavailable: session_control".to_string()); |
| 43 | }; |
| 44 | let Some(presentation) = parts.presentation.as_deref_mut() else { |
| 45 | return CommandResult::error("Command capability unavailable: presentation".to_string()); |
| 46 | }; |
| 47 | color_portable(control, presentation, arg) |
| 48 | } |
| 49 | |
| 50 | pub(in crate::commands) fn color_portable( |
| 51 | control: &mut dyn CommandSessionControlContext, |
| 52 | presentation: &dyn CommandPresentationContext, |
| 53 | arg: Option<&str>, |
| 54 | ) -> CommandResult { |
| 55 | let requested = arg |
| 56 | .map(str::trim) |
| 57 | .filter(|arg| !arg.is_empty()) |
| 58 | .map(str::to_ascii_lowercase); |
| 59 | let next = match requested.as_deref() { |
| 60 | None => { |
| 61 | let current = control.composer_color(); |
| 62 | let position = current |
| 63 | .as_deref() |
| 64 | .and_then(|name| COMPOSER_COLORS.iter().position(|color| *color == name)); |
| 65 | Some(COMPOSER_COLORS[position.map_or(0, |index| (index + 1) % COMPOSER_COLORS.len())]) |
| 66 | } |
| 67 | Some("off" | "clear" | "none" | "default") => None, |
| 68 | Some(name) => match COMPOSER_COLORS.iter().find(|color| **color == name) { |
| 69 | Some(color) => Some(*color), |
| 70 | None => { |
| 71 | return CommandResult::error( |
| 72 | presentation |
| 73 | .translate( |
| 74 | "cmd_color_unknown", |
| 75 | &[("colors", &COMPOSER_COLORS.join(", "))], |
| 76 | ) |
| 77 | .unwrap_or_else(std::convert::identity), |
| 78 | ); |
| 79 | } |
| 80 | }, |
| 81 | }; |
| 82 | let message = match next { |
| 83 | Some(color) => presentation.translate("cmd_color_set", &[("color", color)]), |
| 84 | None => presentation.translate("cmd_color_reset", &[]), |
| 85 | }; |
| 86 | let message = match message { |
| 87 | Ok(message) => message, |
| 88 | Err(error) => return CommandResult::error(error), |
| 89 | }; |
| 90 | match control.set_composer_color(next.map(str::to_string)) { |
| 91 | Ok(()) => CommandResult::message(message), |
| 92 | Err(error) => CommandResult::error(error), |
| 93 | } |
| 94 | } |
| 95 |