| 1 | //! `/stash` slash command — list / pop parked composer drafts (#440). |
| 2 | //! |
| 3 | //! See `crates/tui/src/composer_stash.rs` for the on-disk format |
| 4 | //! and persistence rules. The slash command is the user-facing |
| 5 | //! surface; Ctrl+G (or Ctrl+S) in the composer is the corresponding push entry |
| 6 | //! point. |
| 7 | |
| 8 | use codewhale_command_contract::facets::CommandComposerContext; |
| 9 | use codewhale_command_contract::handler::{CommandCapabilities, CommandContexts, CommandHandler}; |
| 10 | use codewhale_command_contract::metadata::{CommandInfo, RegisterCommand}; |
| 11 | use codewhale_command_contract::outcome::CommandResult; |
| 12 | |
| 13 | const COMMAND_INFO: CommandInfo = CommandInfo { |
| 14 | name: "stash", |
| 15 | aliases: &["park"], |
| 16 | usage: "/stash [list|pop|clear]", |
| 17 | description_key: "cmd_stash_description", |
| 18 | }; |
| 19 | |
| 20 | pub(super) struct StashCmd; |
| 21 | |
| 22 | impl<A> RegisterCommand<CommandResult<A>> for StashCmd { |
| 23 | fn info() -> &'static CommandInfo { |
| 24 | &COMMAND_INFO |
| 25 | } |
| 26 | fn handler() -> CommandHandler<CommandResult<A>> { |
| 27 | CommandHandler::Contextual { |
| 28 | capabilities: CommandCapabilities::COMPOSER, |
| 29 | handler: stash, |
| 30 | } |
| 31 | } |
| 32 | } |
| 33 | |
| 34 | /// Top-level dispatch for `/stash`. Subcommands: |
| 35 | /// |
| 36 | /// * `/stash` — same as `/stash list`. |
| 37 | /// * `/stash list` — show parked drafts, oldest first. |
| 38 | /// * `/stash pop` — restore the most recently parked draft into |
| 39 | /// the composer; the popped entry is removed from disk. |
| 40 | /// * `/stash clear` — wipe the entire stash file. Reports how many |
| 41 | /// entries were dropped so the user knows what they deleted. |
| 42 | fn stash<A>(mut contexts: CommandContexts<'_>, arg: Option<&str>) -> CommandResult<A> { |
| 43 | let Some(composer) = contexts.take_composer() else { |
| 44 | return CommandResult::error("Command capability unavailable: composer"); |
| 45 | }; |
| 46 | let sub = arg.map(str::trim).unwrap_or("list").to_ascii_lowercase(); |
| 47 | match sub.as_str() { |
| 48 | "" | "list" | "ls" | "show" => list(composer), |
| 49 | "pop" | "restore" => pop(composer), |
| 50 | "clear" | "wipe" | "drop" => clear(composer), |
| 51 | other => CommandResult::error(format!( |
| 52 | "unknown subcommand `{other}`. Try `/stash list`, `/stash pop`, or `/stash clear`." |
| 53 | )), |
| 54 | } |
| 55 | } |
| 56 | |
| 57 | fn list<A>(composer: &dyn CommandComposerContext) -> CommandResult<A> { |
| 58 | let entries = composer.parked_drafts(); |
| 59 | if entries.is_empty() { |
| 60 | return CommandResult::message( |
| 61 | "Stash empty. Press Ctrl+G (or Ctrl+S) in the composer to park the current draft.", |
| 62 | ); |
| 63 | } |
| 64 | let mut out = String::new(); |
| 65 | out.push_str(&format!("{} parked draft(s):\n\n", entries.len())); |
| 66 | for (idx, entry) in entries.iter().enumerate() { |
| 67 | out.push_str(&format_stash_line(idx, &entry.timestamp, &entry.text)); |
| 68 | } |
| 69 | out.push_str("\nUse `/stash pop` to restore the most recent draft."); |
| 70 | CommandResult::message(out) |
| 71 | } |
| 72 | |
| 73 | fn clear<A>(composer: &mut dyn CommandComposerContext) -> CommandResult<A> { |
| 74 | match composer.clear_parked() { |
| 75 | Ok(0) => CommandResult::message("Stash already empty — nothing to clear."), |
| 76 | Ok(n) => CommandResult::message(format!("Cleared {n} parked draft(s) from the stash.")), |
| 77 | Err(err) => CommandResult::error(format!("Failed to clear stash: {err}")), |
| 78 | } |
| 79 | } |
| 80 | |
| 81 | fn pop<A>(composer: &mut dyn CommandComposerContext) -> CommandResult<A> { |
| 82 | match composer.restore_parked() { |
| 83 | Some((text, remaining)) => { |
| 84 | let preview = preview_first_line(&text, 60); |
| 85 | let suffix = match remaining { |
| 86 | 0 => " (stash now empty)".to_string(), |
| 87 | 1 => " (1 more parked)".to_string(), |
| 88 | n => format!(" ({n} more parked)"), |
| 89 | }; |
| 90 | CommandResult::message(format!("Restored stashed draft: {preview}{suffix}")) |
| 91 | } |
| 92 | None => CommandResult::message("Stash empty — nothing to pop."), |
| 93 | } |
| 94 | } |
| 95 | |
| 96 | /// One `/stash list` row. `idx` is the 0-based position; users see 1-based. |
| 97 | fn format_stash_line(idx: usize, ts: &str, text: &str) -> String { |
| 98 | let ts = if ts.is_empty() { "(no ts)" } else { ts }; |
| 99 | let preview = preview_first_line(text, 80); |
| 100 | format!(" {}. [{ts}] {preview}\n", idx + 1) |
| 101 | } |
| 102 | |
| 103 | /// Take a one-line preview of `text`, capped at `max_chars`. |
| 104 | /// Multi-line drafts get a single-line summary so the listing |
| 105 | /// stays scannable. |
| 106 | fn preview_first_line(text: &str, max_chars: usize) -> String { |
| 107 | let head = text.lines().next().unwrap_or("").trim(); |
| 108 | if head.chars().count() <= max_chars { |
| 109 | return head.to_string(); |
| 110 | } |
| 111 | let mut out: String = head.chars().take(max_chars.saturating_sub(1)).collect(); |
| 112 | out.push('…'); |
| 113 | out |
| 114 | } |
| 115 | |
| 116 | #[cfg(test)] |
| 117 | mod tests { |
| 118 | use super::*; |
| 119 | |
| 120 | #[test] |
| 121 | fn stash_list_numbers_entries_from_one() { |
| 122 | assert_eq!( |
| 123 | format_stash_line(0, "2026-09-22T10:00:00Z", "first draft\nmore"), |
| 124 | " 1. [2026-09-22T10:00:00Z] first draft\n" |
| 125 | ); |
| 126 | assert_eq!(format_stash_line(2, "", "third"), " 3. [(no ts)] third\n"); |
| 127 | } |
| 128 | |
| 129 | #[test] |
| 130 | fn preview_first_line_truncates_to_cap() { |
| 131 | let body = "x".repeat(200); |
| 132 | let p = preview_first_line(&body, 10); |
| 133 | assert_eq!(p.chars().count(), 10); |
| 134 | assert!(p.ends_with('…')); |
| 135 | } |
| 136 | |
| 137 | #[test] |
| 138 | fn preview_first_line_keeps_short_input_intact() { |
| 139 | assert_eq!(preview_first_line("short", 50), "short"); |
| 140 | } |
| 141 | |
| 142 | #[test] |
| 143 | fn preview_first_line_only_uses_first_line_of_multiline() { |
| 144 | let body = "first line of the draft\nsecond line that's longer\nthird"; |
| 145 | assert_eq!(preview_first_line(body, 80), "first line of the draft"); |
| 146 | } |
| 147 | |
| 148 | #[test] |
| 149 | fn preview_first_line_handles_empty_input() { |
| 150 | assert_eq!(preview_first_line("", 50), ""); |
| 151 | assert_eq!(preview_first_line(" ", 50), ""); |
| 152 | } |
| 153 | } |
| 154 |