返回 CodeWhale
workspace.rs
根目录 / crates / tui / src / commands / groups / core / workspace.rs
1 //! `/workspace` command.
2
3 use codewhale_command_contract::facets::{CommandPresentationContext, CommandWorkspaceContext};
4 use codewhale_command_contract::handler::{CommandCapabilities, CommandContexts, CommandHandler};
5 use codewhale_command_contract::metadata::{CommandInfo, RegisterCommand};
6 use codewhale_command_contract::navigation::WorkspaceTarget;
7 use codewhale_command_contract::outcome::{
8 CoreNavigationAction as Action, CoreNavigationCommandResult as CommandResult,
9 };
10 use std::path::PathBuf;
11
12 pub const COMMAND_INFO: CommandInfo = CommandInfo {
13 name: "workspace",
14 aliases: &["cwd"],
15 usage: "/workspace [path|worktrees]",
16 description_key: "cmd_workspace_description",
17 };
18
19 pub struct WorkspaceCmd;
20
21 const WORKTREE_INFO: CommandInfo = CommandInfo {
22 name: "worktree",
23 aliases: &[],
24 usage: "/worktree",
25 description_key: "cmd_worktree_description",
26 };
27
28 pub struct WorktreeCmd;
29
30 impl RegisterCommand<CommandResult> for WorktreeCmd {
31 fn info() -> &'static CommandInfo {
32 &WORKTREE_INFO
33 }
34 fn handler() -> CommandHandler<CommandResult> {
35 CommandHandler::Pure(|arg| {
36 if arg.is_some() {
37 return CommandResult::error(format!("Usage: {}", WORKTREE_INFO.usage));
38 }
39 CommandResult::action(Action::OpenWorktreeManager)
40 })
41 }
42 }
43
44 impl RegisterCommand<CommandResult> for WorkspaceCmd {
45 fn info() -> &'static CommandInfo {
46 &COMMAND_INFO
47 }
48 fn handler() -> CommandHandler<CommandResult> {
49 CommandHandler::Contextual {
50 capabilities: CommandCapabilities::WORKSPACE.union(CommandCapabilities::PRESENTATION),
51 handler: workspace,
52 }
53 }
54 }
55
56 fn workspace(contexts: CommandContexts<'_>, arg: Option<&str>) -> CommandResult {
57 let parts = contexts.into_parts();
58 let Some(workspace) = parts.workspace else {
59 return CommandResult::error("Command capability unavailable: workspace");
60 };
61 let Some(presentation) = parts.presentation else {
62 return CommandResult::error("Command capability unavailable: presentation");
63 };
64 match arg.map(str::trim).filter(|arg| !arg.is_empty()) {
65 Some("worktrees" | "worktree" | "wt") => CommandResult::action(Action::OpenWorktreeManager),
66 other => {
67 workspace_switch(workspace, presentation, other).unwrap_or_else(CommandResult::error)
68 }
69 }
70 }
71
72 fn workspace_switch(
73 workspace: &dyn CommandWorkspaceContext,
74 presentation: &dyn CommandPresentationContext,
75 arg: Option<&str>,
76 ) -> Result<CommandResult, String> {
77 let Some(raw_path) = arg.map(str::trim).filter(|path| !path.is_empty()) else {
78 return Ok(CommandResult::message(presentation.translate(
79 "workspace_current",
80 &[("path", &workspace.workspace().display().to_string())],
81 )?));
82 };
83 let expanded = expand_workspace_path(workspace, presentation, raw_path)?;
84 let candidate = if expanded.is_absolute() {
85 expanded
86 } else {
87 workspace.workspace().join(expanded)
88 };
89 let target = match workspace.inspect_workspace_target(&candidate) {
90 WorkspaceTarget::Missing => {
91 return Ok(CommandResult::error(presentation.translate(
92 "workspace_not_found",
93 &[("path", &candidate.display().to_string())],
94 )?));
95 }
96 WorkspaceTarget::File | WorkspaceTarget::NotDirectory => {
97 return Ok(CommandResult::error(presentation.translate(
98 "workspace_not_directory",
99 &[("path", &candidate.display().to_string())],
100 )?));
101 }
102 WorkspaceTarget::Directory {
103 canonical_or_candidate,
104 } => canonical_or_candidate,
105 };
106 let message = presentation.translate(
107 "workspace_switching",
108 &[("path", &target.display().to_string())],
109 )?;
110 Ok(CommandResult::with_message_and_action(
111 message,
112 Action::SwitchWorkspace { workspace: target },
113 ))
114 }
115
116 fn expand_workspace_path(
117 workspace: &dyn CommandWorkspaceContext,
118 presentation: &dyn CommandPresentationContext,
119 path: &str,
120 ) -> Result<PathBuf, String> {
121 if path == "~" || path.starts_with("~/") {
122 let Some(home) = workspace.effective_home_dir() else {
123 return Err(presentation.translate("workspace_home_unresolved", &[])?);
124 };
125 return Ok(match path.strip_prefix("~/") {
126 Some(rest) => home.join(rest),
127 None => home,
128 });
129 }
130 Ok(PathBuf::from(path))
131 }
132
132 lines RUST