返回 CodeWhale
links.rs
根目录 / crates / tui / src / commands / groups / core / links.rs
1 //! `/links` 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::{
7 NavigationLinks, NavigationProviderKind, public_site_locale_segment,
8 };
9 use codewhale_command_contract::outcome::CoreNavigationCommandResult as CommandResult;
10 use std::fmt::Write;
11
12 pub const COMMAND_INFO: CommandInfo = CommandInfo {
13 name: "links",
14 aliases: &["api", "lianjie"],
15 usage: "/links",
16 description_key: "cmd_links_description",
17 };
18
19 pub struct LinksCmd;
20
21 impl RegisterCommand<CommandResult> for LinksCmd {
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: links,
29 }
30 }
31 }
32
33 fn links(contexts: CommandContexts<'_>, _: Option<&str>) -> CommandResult {
34 let parts = contexts.into_parts();
35 let Some(navigation) = parts.navigation else {
36 return CommandResult::error("Command capability unavailable: navigation");
37 };
38 let Some(presentation) = parts.presentation else {
39 return CommandResult::error("Command capability unavailable: presentation");
40 };
41 render_links(presentation, navigation.links())
42 .map(CommandResult::message)
43 .unwrap_or_else(CommandResult::error)
44 }
45
46 fn render_links(
47 presentation: &dyn CommandPresentationContext,
48 facts: NavigationLinks,
49 ) -> Result<String, String> {
50 let label = |key| presentation.translate(key, &[]);
51 let site_locale = public_site_locale_segment(&facts.locale_tag);
52 let mut message = format!(
53 "{}\n─────────────────────────────\n",
54 label("links_project_title")?
55 );
56 let _ = writeln!(
57 message,
58 "{} `https://codewhale.net/{site_locale}/docs`",
59 label("links_documentation")?
60 );
61 let _ = writeln!(
62 message,
63 "{} `https://codewhale.net/{site_locale}/community`",
64 label("links_community")?
65 );
66 let _ = writeln!(
67 message,
68 "{} `https://github.com/codewhale-hq/CodeWhale`",
69 label("links_git_hub")?
70 );
71 let _ = writeln!(
72 message,
73 "{} `https://app.codewhale.net`",
74 label("links_managed_app")?
75 );
76 let _ = writeln!(message, "{}", label("links_managed_app_note")?);
77 let _ = write!(
78 message,
79 "\n{}\n─────────────────────────────\n",
80 label("links_title")?
81 );
82 for provider in facts.providers {
83 let active_marker = if provider.id == facts.active_provider {
84 " <- current"
85 } else {
86 ""
87 };
88 let _ = writeln!(
89 message,
90 "\n{} ({}){}",
91 provider.name, provider.id, active_marker
92 );
93 if let Some(key_url) = provider.credential_url {
94 let _ = writeln!(message, "{} `{}`", label("links_dashboard")?, key_url);
95 } else {
96 let _ = writeln!(
97 message,
98 "{} {}",
99 label("links_dashboard")?,
100 provider.guidance
101 );
102 }
103 if let Some(docs_url) = provider.docs_url {
104 let _ = writeln!(message, "{} `{}`", label("links_docs")?, docs_url);
105 }
106 if provider.kind == NavigationProviderKind::Moonshot {
107 let _ = writeln!(
108 message,
109 "{}",
110 presentation.translate(
111 "links_kimi_code_route_note",
112 &[
113 ("route", &facts.kimi_route),
114 ("console", &facts.kimi_console),
115 ]
116 )?
117 );
118 }
119 if provider.env_names.is_empty() {
120 let _ = writeln!(message, "Env: none");
121 } else {
122 let _ = writeln!(message, "Env: {}", provider.env_names.join(", "));
123 }
124 }
125 let _ = writeln!(message, "\n{}", label("links_tip")?);
126 Ok(message)
127 }
128
128 lines RUST