返回 CodeWhale
jobs.rs
根目录 / crates / tui / src / commands / groups / utility / jobs.rs
1 //! Shell job-center commands.
2
3 use codewhale_command_contract::handler::CommandHandler;
4 use codewhale_command_contract::metadata::{CommandInfo, RegisterCommand};
5
6 use crate::commands::CommandResult;
7 use crate::tui::app::{AppAction, ShellJobAction};
8
9 pub(in crate::commands) const COMMAND_INFO: CommandInfo = CommandInfo {
10 name: "jobs",
11 aliases: &["job", "zuoye"],
12 usage: "/jobs [list|show <id>|poll <id>|wait <id>|stdin <id> <input>|cancel <id|all>]",
13 description_key: "cmd_jobs_description",
14 };
15
16 pub(in crate::commands) struct JobsCmd;
17
18 impl RegisterCommand<CommandResult> for JobsCmd {
19 fn info() -> &'static CommandInfo {
20 &COMMAND_INFO
21 }
22
23 fn handler() -> CommandHandler<CommandResult> {
24 CommandHandler::Pure(jobs)
25 }
26 }
27
28 fn is_cancel_all_token(id: &str) -> bool {
29 id.eq_ignore_ascii_case("all")
30 }
31
32 fn jobs(args: Option<&str>) -> CommandResult {
33 let raw = args.unwrap_or("").trim();
34 if raw.is_empty() || raw.eq_ignore_ascii_case("list") {
35 return CommandResult::action(AppAction::ShellJob(ShellJobAction::List));
36 }
37
38 let mut parts = raw.splitn(3, char::is_whitespace);
39 let action = parts.next().unwrap_or("").to_ascii_lowercase();
40 let id = parts.next().map(str::trim).filter(|s| !s.is_empty());
41 let rest = parts.next().map(str::trim).unwrap_or("");
42
43 match action.as_str() {
44 "list" => CommandResult::action(AppAction::ShellJob(ShellJobAction::List)),
45 "show" | "inspect" | "poll" | "wait" => match id {
46 Some(id) => CommandResult::action(AppAction::ShellJob(ShellJobAction::Show {
47 id: id.to_string(),
48 })),
49 None => CommandResult::error(format!("Usage: /jobs {action} <id>")),
50 },
51 "stdin" | "send" => match id {
52 Some(id) if !rest.is_empty() => {
53 CommandResult::action(AppAction::ShellJob(ShellJobAction::SendStdin {
54 id: id.to_string(),
55 input: rest.to_string(),
56 close: false,
57 }))
58 }
59 _ => CommandResult::error("Usage: /jobs stdin <id> <input>"),
60 },
61 "close-stdin" | "eof" => match id {
62 Some(id) => CommandResult::action(AppAction::ShellJob(ShellJobAction::SendStdin {
63 id: id.to_string(),
64 input: String::new(),
65 close: true,
66 })),
67 None => CommandResult::error("Usage: /jobs close-stdin <id>"),
68 },
69 "cancel" | "kill" | "stop" => match id {
70 Some(id) if is_cancel_all_token(id) => {
71 CommandResult::action(AppAction::ShellJob(ShellJobAction::CancelAll))
72 }
73 Some(id) => CommandResult::action(AppAction::ShellJob(ShellJobAction::Cancel {
74 id: id.to_string(),
75 })),
76 None => CommandResult::error(
77 "Usage: /jobs cancel <id|all> — id is the shell_* shown on the Shells work-strip row",
78 ),
79 },
80 "cancel-all" | "kill-all" | "stop-all" => {
81 CommandResult::action(AppAction::ShellJob(ShellJobAction::CancelAll))
82 }
83 _ => CommandResult::error(
84 "Usage: /jobs [list|show <id>|poll <id>|wait <id>|stdin <id> <input>|close-stdin <id>|cancel <id|all>]",
85 ),
86 }
87 }
88
89 #[cfg(test)]
90 mod tests {
91 use super::*;
92
93 #[test]
94 fn parses_job_actions() {
95 let show = jobs(Some("show shell_abcd"));
96 assert!(matches!(
97 show.action,
98 Some(AppAction::ShellJob(ShellJobAction::Show { id })) if id == "shell_abcd"
99 ));
100
101 let send = jobs(Some("stdin shell_abcd y"));
102 assert!(matches!(
103 send.action,
104 Some(AppAction::ShellJob(ShellJobAction::SendStdin { id, input, close: false }))
105 if id == "shell_abcd" && input == "y"
106 ));
107
108 let cancel_all = jobs(Some("cancel-all"));
109 assert!(matches!(
110 cancel_all.action,
111 Some(AppAction::ShellJob(ShellJobAction::CancelAll))
112 ));
113
114 let cancel_all_spaced = jobs(Some("cancel all"));
115 assert!(matches!(
116 cancel_all_spaced.action,
117 Some(AppAction::ShellJob(ShellJobAction::CancelAll))
118 ));
119
120 let cancel_id = jobs(Some("cancel shell_abcd"));
121 assert!(matches!(
122 cancel_id.action,
123 Some(AppAction::ShellJob(ShellJobAction::Cancel { id })) if id == "shell_abcd"
124 ));
125
126 let bare_cancel = jobs(Some("cancel"));
127 assert!(bare_cancel.action.is_none());
128 assert!(
129 bare_cancel
130 .message
131 .as_deref()
132 .is_some_and(|message| message.contains("shell_*") && message.contains("<id|all>")),
133 "bare cancel must name the work-strip id space, not a task: {:?}",
134 bare_cancel.message
135 );
136 assert!(
137 !bare_cancel
138 .message
139 .as_deref()
140 .is_some_and(|message| message.contains("Task")),
141 "bare cancel must not mention Task: {:?}",
142 bare_cancel.message
143 );
144 }
145
146 #[test]
147 fn handler_is_pure_and_argument_only() {
148 assert!(matches!(JobsCmd::handler(), CommandHandler::Pure(_)));
149 assert_eq!(JobsCmd::info().description_key, "cmd_jobs_description");
150 assert_eq!(JobsCmd::info().aliases, &["job", "zuoye"]);
151 }
152 }
153
153 lines RUST