1use crate::config::YamlConfig;
2use crate::constants::{self, section, config_key, CONTAIN_SEARCH_SECTIONS};
3use crate::{error, info, md, usage};
4use colored::Colorize;
5
6const VERSION_TEMPLATE: &str = include_str!("../../assets/version.md");
8
9pub fn handle_version(config: &YamlConfig) {
11 let mut extra = String::new();
12
13 if let Some(version_map) = config.get_section("version") {
15 for (key, value) in version_map {
16 if key == "email" || key == "author" {
17 continue;
18 }
19 extra.push_str(&format!("| {} | {} |\n", key, value));
20 }
21 }
22
23 let text = VERSION_TEMPLATE
24 .replace("{version}", constants::VERSION)
25 .replace("{os}", std::env::consts::OS)
26 .replace("{extra}", &extra);
27 md!("{}", text);
28}
29
30const HELP_TEXT: &str = include_str!("../../assets/help.md");
32
33pub fn handle_help() {
35 md!("{}", HELP_TEXT);
36}
37
38pub fn handle_exit() {
40 info!("Bye~ See you again 😭");
41 std::process::exit(0);
42}
43
44pub fn handle_log(key: &str, value: &str, config: &mut YamlConfig) {
46 if key == config_key::MODE {
47 let mode = if value == config_key::VERBOSE {
48 config_key::VERBOSE
49 } else {
50 config_key::CONCISE
51 };
52 config.set_property(section::LOG, config_key::MODE, mode);
53 info!("✅ 日志模式已切换为: {}", mode);
54 } else {
55 usage!("j log mode <verbose|concise>");
56 }
57}
58
59pub fn handle_clear() {
61 print!("\x1B[2J\x1B[1;1H");
63}
64
65pub fn handle_contain(alias: &str, containers: Option<&str>, config: &YamlConfig) {
68 let sections: Vec<&str> = match containers {
69 Some(c) => c.split(',').collect(),
70 None => CONTAIN_SEARCH_SECTIONS.to_vec(),
71 };
72
73 let mut found = Vec::new();
74
75 for section in §ions {
76 if config.contains(section, alias) {
77 if let Some(value) = config.get_property(section, alias) {
78 found.push(format!(
79 "{} {}: {}",
80 format!("[{}]", section).green(),
81 alias,
82 value
83 ));
84 }
85 }
86 }
87
88 if found.is_empty() {
89 info!("nothing found 😢");
90 } else {
91 info!("找到 {} 条结果 😊", found.len().to_string().green());
92 for line in &found {
93 info!("{}", line);
94 }
95 }
96}
97
98pub fn handle_change(part: &str, field: &str, value: &str, config: &mut YamlConfig) {
101 if config.get_section(part).is_none() {
102 error!("❌ 在配置文件中未找到该 section:{}", part);
103 return;
104 }
105
106 let old_value = config.get_property(part, field).cloned();
107 config.set_property(part, field, value);
108
109 match old_value {
110 Some(old) => {
111 info!("✅ 已修改 {}.{} 的值为 {},旧值为 {}", part, field, value, old);
112 }
113 None => {
114 info!("✅ 已新增 {}.{} = {}", part, field, value);
115 }
116 }
117 info!("🚧 此命令可能会导致配置文件属性错乱而使 Copilot 无法正常使用,请确保在您清楚在做什么的情况下使用");
118}
119
120pub fn handle_completion(shell_type: Option<&str>, config: &YamlConfig) {
125 let shell = shell_type.unwrap_or("zsh");
126
127 match shell {
128 "zsh" => generate_zsh_completion(config),
129 "bash" => generate_bash_completion(config),
130 _ => {
131 error!("❌ 不支持的 shell 类型: {},可选: zsh, bash", shell);
132 usage!("j completion [zsh|bash]");
133 }
134 }
135}
136
137fn generate_zsh_completion(config: &YamlConfig) {
139 let mut all_aliases = Vec::new();
141 for s in constants::ALIAS_EXISTS_SECTIONS {
142 if let Some(map) = config.get_section(s) {
143 for key in map.keys() {
144 if !all_aliases.contains(key) {
145 all_aliases.push(key.clone());
146 }
147 }
148 }
149 }
150 all_aliases.sort();
151
152 let editor_aliases: Vec<String> = config
154 .get_section(section::EDITOR)
155 .map(|m| m.keys().cloned().collect())
156 .unwrap_or_default();
157
158 let browser_aliases: Vec<String> = config
160 .get_section(section::BROWSER)
161 .map(|m| m.keys().cloned().collect())
162 .unwrap_or_default();
163
164 let keywords = constants::cmd::all_keywords();
166
167 let subcmds = keywords.iter().map(|s| *s).collect::<Vec<_>>();
169 let subcmds_str = subcmds.join(" ");
170
171 let aliases_str = all_aliases.join(" ");
173
174 let editor_pattern = if editor_aliases.is_empty() {
176 String::new()
177 } else {
178 editor_aliases.join("|")
179 };
180
181 let browser_pattern = if browser_aliases.is_empty() {
183 String::new()
184 } else {
185 browser_aliases.join("|")
186 };
187
188 let mut script = String::new();
190 script.push_str("#compdef j\n");
191 script.push_str("# Zsh completion for j (work-copilot)\n");
192 script.push_str("# 生成方式: eval \"$(j completion zsh)\"\n");
193 script.push_str("# 或: j completion zsh > ~/.zsh/completions/_j && fpath=(~/.zsh/completions $fpath)\n\n");
194 script.push_str("_j() {\n");
195 script.push_str(" local curcontext=\"$curcontext\" state line\n");
196 script.push_str(" typeset -A opt_args\n\n");
197
198 script.push_str(&format!(" local -a subcmds=({})\n", subcmds_str));
200 script.push_str(&format!(" local -a aliases=({})\n", aliases_str));
201
202 if !editor_pattern.is_empty() {
204 script.push_str(&format!(" local -a editor_aliases=({})\n", editor_aliases.join(" ")));
205 }
206
207 script.push_str("\n _arguments -C \\\n");
208 script.push_str(" '1: :->cmd' \\\n");
209 script.push_str(" '*: :->args'\n\n");
210
211 script.push_str(" case $state in\n");
212 script.push_str(" cmd)\n");
213 script.push_str(" _describe 'command' subcmds\n");
214 script.push_str(" _describe 'alias' aliases\n");
215 script.push_str(" ;;\n");
216 script.push_str(" args)\n");
217 script.push_str(" case $words[2] in\n");
218
219 script.push_str(" set|s|modify|mf)\n");
221 script.push_str(" if (( CURRENT == 3 )); then\n");
222 script.push_str(" _describe 'alias' aliases\n");
223 script.push_str(" else\n");
224 script.push_str(" _files\n");
225 script.push_str(" fi\n");
226 script.push_str(" ;;\n");
227
228 script.push_str(" rm|remove|rename|rn|note|nt|denote|dnt|contain|find)\n");
230 script.push_str(" _describe 'alias' aliases\n");
231 script.push_str(" ;;\n");
232
233 let sections_str = constants::ALL_SECTIONS.join(" ");
235 script.push_str(&format!(" ls|list)\n"));
236 script.push_str(&format!(" local -a sections=(all {})\n", sections_str));
237 script.push_str(" _describe 'section' sections\n");
238 script.push_str(" ;;\n");
239
240 script.push_str(" reportctl|rctl)\n");
242 script.push_str(" local -a rctl_actions=(new sync push pull set-url open)\n");
243 script.push_str(" _describe 'action' rctl_actions\n");
244 script.push_str(" ;;\n");
245
246 script.push_str(" log)\n");
248 script.push_str(" if (( CURRENT == 3 )); then\n");
249 script.push_str(" local -a log_keys=(mode)\n");
250 script.push_str(" _describe 'key' log_keys\n");
251 script.push_str(" else\n");
252 script.push_str(" local -a log_values=(verbose concise)\n");
253 script.push_str(" _describe 'value' log_values\n");
254 script.push_str(" fi\n");
255 script.push_str(" ;;\n");
256
257 script.push_str(&format!(" change|chg)\n"));
259 script.push_str(&format!(" local -a sections=({})\n", sections_str));
260 script.push_str(" _describe 'section' sections\n");
261 script.push_str(" ;;\n");
262
263 script.push_str(" time)\n");
265 script.push_str(" local -a time_funcs=(countdown)\n");
266 script.push_str(" _describe 'function' time_funcs\n");
267 script.push_str(" ;;\n");
268
269 script.push_str(" completion)\n");
271 script.push_str(" local -a shells=(zsh bash)\n");
272 script.push_str(" _describe 'shell' shells\n");
273 script.push_str(" ;;\n");
274
275 if !editor_pattern.is_empty() {
277 script.push_str(&format!(" {})\n", editor_pattern));
278 script.push_str(" _files\n");
279 script.push_str(" ;;\n");
280 }
281
282 if !browser_pattern.is_empty() {
284 script.push_str(&format!(" {})\n", browser_pattern));
285 script.push_str(" _describe 'alias' aliases\n");
286 script.push_str(" _files\n");
287 script.push_str(" ;;\n");
288 }
289
290 script.push_str(" *)\n");
292 script.push_str(" _files\n");
293 script.push_str(" _describe 'alias' aliases\n");
294 script.push_str(" ;;\n");
295
296 script.push_str(" esac\n");
297 script.push_str(" ;;\n");
298 script.push_str(" esac\n");
299 script.push_str("}\n\n");
300 script.push_str("_j \"$@\"\n");
301
302 print!("{}", script);
303}
304
305fn generate_bash_completion(config: &YamlConfig) {
307 let mut all_aliases = Vec::new();
309 for s in constants::ALIAS_EXISTS_SECTIONS {
310 if let Some(map) = config.get_section(s) {
311 for key in map.keys() {
312 if !all_aliases.contains(key) {
313 all_aliases.push(key.clone());
314 }
315 }
316 }
317 }
318 all_aliases.sort();
319
320 let keywords = constants::cmd::all_keywords();
321 let all_completions: Vec<String> = keywords.iter().map(|s| s.to_string())
322 .chain(all_aliases.iter().cloned())
323 .collect();
324
325 let editor_aliases: Vec<String> = config
327 .get_section(section::EDITOR)
328 .map(|m| m.keys().cloned().collect())
329 .unwrap_or_default();
330
331 let mut script = String::new();
332 script.push_str("# Bash completion for j (work-copilot)\n");
333 script.push_str("# 生成方式: eval \"$(j completion bash)\"\n");
334 script.push_str("# 或: j completion bash > /etc/bash_completion.d/j\n\n");
335 script.push_str("_j_completion() {\n");
336 script.push_str(" local cur prev words cword\n");
337 script.push_str(" _init_completion || return\n\n");
338
339 script.push_str(&format!(" local commands=\"{}\"\n", all_completions.join(" ")));
340 script.push_str(&format!(" local aliases=\"{}\"\n", all_aliases.join(" ")));
341
342 if !editor_aliases.is_empty() {
343 script.push_str(&format!(" local editor_aliases=\"{}\"\n", editor_aliases.join(" ")));
344 }
345
346 script.push_str("\n if [[ $cword -eq 1 ]]; then\n");
347 script.push_str(" COMPREPLY=( $(compgen -W \"$commands\" -- \"$cur\") )\n");
348 script.push_str(" return\n");
349 script.push_str(" fi\n\n");
350
351 script.push_str(" case \"${words[1]}\" in\n");
352 script.push_str(" set|s|modify|mf)\n");
353 script.push_str(" if [[ $cword -eq 2 ]]; then\n");
354 script.push_str(" COMPREPLY=( $(compgen -W \"$aliases\" -- \"$cur\") )\n");
355 script.push_str(" else\n");
356 script.push_str(" _filedir\n");
357 script.push_str(" fi\n");
358 script.push_str(" ;;\n");
359 script.push_str(" rm|remove|rename|rn|note|nt|denote|dnt|contain|find)\n");
360 script.push_str(" COMPREPLY=( $(compgen -W \"$aliases\" -- \"$cur\") )\n");
361 script.push_str(" ;;\n");
362 script.push_str(" reportctl|rctl)\n");
363 script.push_str(" COMPREPLY=( $(compgen -W \"new sync push pull set-url open\" -- \"$cur\") )\n");
364 script.push_str(" ;;\n");
365
366 if !editor_aliases.is_empty() {
368 for alias in &editor_aliases {
369 script.push_str(&format!(" {})\n", alias));
370 script.push_str(" _filedir\n");
371 script.push_str(" ;;\n");
372 }
373 }
374
375 script.push_str(" *)\n");
376 script.push_str(" _filedir\n");
377 script.push_str(" COMPREPLY+=( $(compgen -W \"$aliases\" -- \"$cur\") )\n");
378 script.push_str(" ;;\n");
379 script.push_str(" esac\n");
380 script.push_str("}\n\n");
381 script.push_str("complete -F _j_completion j\n");
382
383 print!("{}", script);
384}