返回 CodeWhale
home.rs
根目录 / crates / tui / src / commands / groups / core / home.rs
1 //! `/home` command.
2
3 use codewhale_command_contract::facets::CommandPresentationContext;
4 use codewhale_command_contract::handler::{CommandCapabilities, CommandContexts, CommandHandler};
5 use codewhale_command_contract::metadata::{CommandInfo, RegisterCommand};
6 use codewhale_command_contract::navigation::NavigationOverview;
7 use codewhale_command_contract::outcome::CoreNavigationCommandResult as CommandResult;
8 use codewhale_command_contract::types::CommandMode;
9 use std::fmt::Write;
10
11 pub const COMMAND_INFO: CommandInfo = CommandInfo {
12 name: "home",
13 aliases: &["zhuye", "shouye"],
14 usage: "/home",
15 description_key: "cmd_home_description",
16 };
17
18 pub struct HomeCmd;
19 pub struct OverviewCmd;
20
21 impl RegisterCommand<CommandResult> for HomeCmd {
22 fn info() -> &'static CommandInfo {
23 &COMMAND_INFO
24 }
25 fn handler() -> CommandHandler<CommandResult> {
26 CommandHandler::Contextual {
27 capabilities: CommandCapabilities::NAVIGATION.union(CommandCapabilities::PRESENTATION),
28 handler: home,
29 }
30 }
31 }
32
33 impl RegisterCommand<CommandResult> for OverviewCmd {
34 fn info() -> &'static CommandInfo {
35 &CommandInfo {
36 name: "overview",
37 aliases: &["stats"],
38 usage: "/overview",
39 description_key: "cmd_overview_description",
40 }
41 }
42 fn handler() -> CommandHandler<CommandResult> {
43 CommandHandler::Contextual {
44 capabilities: CommandCapabilities::NAVIGATION.union(CommandCapabilities::PRESENTATION),
45 handler: overview,
46 }
47 }
48 }
49
50 fn home(contexts: CommandContexts<'_>, _: Option<&str>) -> CommandResult {
51 let parts = contexts.into_parts();
52 let Some(navigation) = parts.navigation else {
53 return CommandResult::error("Command capability unavailable: navigation");
54 };
55 let Some(presentation) = parts.presentation else {
56 return CommandResult::error("Command capability unavailable: presentation");
57 };
58 if navigation.home_is_blocked() {
59 return match presentation.translate("home_navigation_busy", &[]) {
60 Ok(message) => CommandResult::error(message),
61 Err(error) => CommandResult::error(error),
62 };
63 }
64 navigation.restore_home();
65 CommandResult::ok()
66 }
67
68 fn overview(contexts: CommandContexts<'_>, _: Option<&str>) -> CommandResult {
69 let parts = contexts.into_parts();
70 let Some(navigation) = parts.navigation else {
71 return CommandResult::error("Command capability unavailable: navigation");
72 };
73 let Some(presentation) = parts.presentation else {
74 return CommandResult::error("Command capability unavailable: presentation");
75 };
76 render_overview(presentation, navigation.overview())
77 .map(CommandResult::message)
78 .unwrap_or_else(CommandResult::error)
79 }
80
81 fn render_overview(
82 presentation: &dyn CommandPresentationContext,
83 facts: NavigationOverview,
84 ) -> Result<String, String> {
85 let label = |key| presentation.translate(key, &[]);
86 let mut stats = String::new();
87 let _ = writeln!(stats, "{}", label("home_dashboard_title")?);
88 let _ = writeln!(stats, "============================================");
89 let _ = writeln!(stats, "{} {}", label("home_model")?, facts.model);
90 let _ = writeln!(stats, "{} {}", label("home_mode")?, facts.mode_label);
91 let _ = writeln!(
92 stats,
93 "{} {}",
94 label("home_workspace")?,
95 facts.workspace.display()
96 );
97 let _ = writeln!(
98 stats,
99 "{} {} messages",
100 label("home_history")?,
101 facts.history_count
102 );
103 let _ = writeln!(
104 stats,
105 "{} {} (session)",
106 label("home_tokens")?,
107 facts.total_tokens
108 );
109 if facts.queued_count > 0 {
110 let _ = writeln!(
111 stats,
112 "{} {} messages",
113 label("home_queued")?,
114 facts.queued_count
115 );
116 }
117 if facts.subagent_count > 0 {
118 let _ = writeln!(
119 stats,
120 "{} {} active",
121 label("home_subagents")?,
122 facts.subagent_count
123 );
124 }
125 if let Some(skill) = &facts.active_skill {
126 let _ = writeln!(stats, "{} {} (active)", label("home_skill")?, skill);
127 }
128 let _ = writeln!(stats, "\n{}", label("home_quick_actions")?);
129 let _ = writeln!(stats, "--------------------------------------------");
130 for key in [
131 "home_quick_workspace",
132 "home_quick_restore",
133 "home_quick_tokens",
134 "home_quick_links",
135 "home_quick_skills",
136 "home_quick_config",
137 "home_quick_settings",
138 "home_quick_model",
139 "home_quick_subagents",
140 "home_quick_task_list",
141 "home_quick_help",
142 ] {
143 let _ = writeln!(stats, "{}", label(key)?);
144 }
145 let _ = writeln!(stats, "\n{}", label("home_mode_tips")?);
146 let _ = writeln!(stats, "--------------------------------------------");
147 let tips: &[&str] = match facts.mode {
148 CommandMode::Agent => &[
149 "home_agent_mode_tip",
150 "home_agent_mode_review_tip",
151 "home_agent_mode_yolo_tip",
152 ],
153 CommandMode::Plan => &["home_plan_mode_tip", "home_plan_mode_checklist_tip"],
154 };
155 for key in tips {
156 let _ = writeln!(stats, "{}", label(key)?);
157 }
158 Ok(stats)
159 }
160
160 lines RUST