1use crate::command;
2use crate::config::YamlConfig;
3use crate::constants::{
4 self, ALIAS_PATH_SECTIONS, ALL_SECTIONS, LIST_ALL, NOTE_CATEGORIES, cmd, config_key,
5 rmeta_action, search_flag, time_function, voice as vc,
6};
7use rustyline::completion::{Completer, Pair};
8use rustyline::highlight::CmdKind;
9use rustyline::highlight::Highlighter;
10use rustyline::hint::{Hinter, HistoryHinter};
11
12use rustyline::Context;
13use rustyline::validate::Validator;
14use std::borrow::Cow;
15
16pub struct CopilotCompleter {
20 pub config: YamlConfig,
21}
22
23impl CopilotCompleter {
24 pub fn new(config: &YamlConfig) -> Self {
25 Self {
26 config: config.clone(),
27 }
28 }
29
30 pub fn refresh(&mut self, config: &YamlConfig) {
31 self.config = config.clone();
32 }
33
34 fn all_aliases(&self) -> Vec<String> {
35 let mut aliases = Vec::new();
36 for s in ALIAS_PATH_SECTIONS {
37 if let Some(map) = self.config.get_section(s) {
38 aliases.extend(map.keys().cloned());
39 }
40 }
41 aliases.sort();
42 aliases.dedup();
43 aliases
44 }
45
46 fn all_sections(&self) -> Vec<String> {
47 self.config
48 .all_section_names()
49 .iter()
50 .map(|s| s.to_string())
51 .collect()
52 }
53
54 fn section_keys(&self, section: &str) -> Vec<String> {
55 self.config
56 .get_section(section)
57 .map(|m| m.keys().cloned().collect())
58 .unwrap_or_default()
59 }
60}
61
62#[derive(Clone)]
64#[allow(dead_code)]
65pub enum ArgHint {
66 Alias,
67 Category,
68 Section,
69 SectionKeys(String),
70 Fixed(Vec<&'static str>),
71 Placeholder(&'static str),
72 FilePath,
73 None,
74}
75
76pub fn command_completion_rules() -> Vec<(&'static [&'static str], Vec<ArgHint>)> {
78 vec![
79 (
80 cmd::SET,
81 vec![ArgHint::Placeholder("<alias>"), ArgHint::FilePath],
82 ),
83 (cmd::REMOVE, vec![ArgHint::Alias]),
84 (
85 cmd::RENAME,
86 vec![ArgHint::Alias, ArgHint::Placeholder("<new_alias>")],
87 ),
88 (cmd::MODIFY, vec![ArgHint::Alias, ArgHint::FilePath]),
89 (cmd::NOTE, vec![ArgHint::Alias, ArgHint::Category]),
90 (cmd::DENOTE, vec![ArgHint::Alias, ArgHint::Category]),
91 (
92 cmd::LIST,
93 vec![ArgHint::Fixed({
94 let mut v: Vec<&'static str> = vec!["", LIST_ALL];
95 for s in ALL_SECTIONS {
96 v.push(s);
97 }
98 v
99 })],
100 ),
101 (
102 cmd::CONTAIN,
103 vec![ArgHint::Alias, ArgHint::Placeholder("<sections>")],
104 ),
105 (
106 cmd::LOG,
107 vec![
108 ArgHint::Fixed(vec![config_key::MODE]),
109 ArgHint::Fixed(vec![config_key::VERBOSE, config_key::CONCISE]),
110 ],
111 ),
112 (
113 cmd::CHANGE,
114 vec![
115 ArgHint::Section,
116 ArgHint::Placeholder("<field>"),
117 ArgHint::Placeholder("<value>"),
118 ],
119 ),
120 (cmd::REPORT, vec![ArgHint::Placeholder("<content>")]),
121 (
122 cmd::REPORTCTL,
123 vec![
124 ArgHint::Fixed(vec![
125 rmeta_action::NEW,
126 rmeta_action::SYNC,
127 rmeta_action::PUSH,
128 rmeta_action::PULL,
129 rmeta_action::SET_URL,
130 rmeta_action::OPEN,
131 ]),
132 ArgHint::Placeholder("<date|message|url>"),
133 ],
134 ),
135 (cmd::CHECK, vec![ArgHint::Placeholder("<line_count>")]),
136 (
137 cmd::SEARCH,
138 vec![
139 ArgHint::Placeholder("<line_count|all>"),
140 ArgHint::Placeholder("<target>"),
141 ArgHint::Fixed(vec![search_flag::FUZZY_SHORT, search_flag::FUZZY]),
142 ],
143 ),
144 (
145 cmd::TODO,
146 vec![
147 ArgHint::Fixed(vec!["list", "add"]),
148 ArgHint::Placeholder("<content>"),
149 ],
150 ),
151 (cmd::CHAT, vec![ArgHint::Placeholder("<message>")]),
152 (cmd::VOICE, vec![ArgHint::Fixed(vec![vc::ACTION_DOWNLOAD])]),
153 (
154 cmd::CONCAT,
155 vec![
156 ArgHint::Placeholder("<script_name>"),
157 ArgHint::Placeholder("<script_content>"),
158 ],
159 ),
160 (
161 cmd::TIME,
162 vec![
163 ArgHint::Fixed(vec![time_function::COUNTDOWN]),
164 ArgHint::Placeholder("<duration>"),
165 ],
166 ),
167 (cmd::COMPLETION, vec![ArgHint::Fixed(vec!["zsh", "bash"])]),
168 (cmd::VERSION, vec![]),
169 (cmd::HELP, vec![]),
170 (cmd::CLEAR, vec![]),
171 (cmd::EXIT, vec![]),
172 ]
173}
174
175const ALL_NOTE_CATEGORIES: &[&str] = NOTE_CATEGORIES;
176
177impl Completer for CopilotCompleter {
178 type Candidate = Pair;
179
180 fn complete(
181 &self,
182 line: &str,
183 pos: usize,
184 _ctx: &Context<'_>,
185 ) -> rustyline::Result<(usize, Vec<Pair>)> {
186 let line_to_cursor = &line[..pos];
187 let parts: Vec<&str> = line_to_cursor.split_whitespace().collect();
188
189 let trailing_space = line_to_cursor.ends_with(' ');
190 let word_index = if trailing_space {
191 parts.len()
192 } else {
193 parts.len().saturating_sub(1)
194 };
195 let current_word = if trailing_space {
196 ""
197 } else {
198 parts.last().copied().unwrap_or("")
199 };
200 let start_pos = pos - current_word.len();
201
202 if !parts.is_empty() && (parts[0] == "!" || parts[0].starts_with('!')) {
204 let candidates = complete_file_path(current_word);
205 return Ok((start_pos, candidates));
206 }
207
208 if word_index == 0 {
209 let mut candidates = Vec::new();
210 let rules = command_completion_rules();
211 for (names, _) in &rules {
212 for name in *names {
213 if name.starts_with(current_word) {
214 candidates.push(Pair {
215 display: name.to_string(),
216 replacement: name.to_string(),
217 });
218 }
219 }
220 }
221 for alias in self.all_aliases() {
222 if alias.starts_with(current_word)
223 && !command::all_command_keywords().contains(&alias.as_str())
224 {
225 candidates.push(Pair {
226 display: alias.clone(),
227 replacement: alias,
228 });
229 }
230 }
231 return Ok((start_pos, candidates));
232 }
233
234 let cmd_str = parts[0];
235 let rules = command_completion_rules();
236
237 for (names, arg_hints) in &rules {
238 if names.contains(&cmd_str) {
239 let arg_index = word_index - 1;
240 if arg_index < arg_hints.len() {
241 let candidates = match &arg_hints[arg_index] {
242 ArgHint::Alias => self
243 .all_aliases()
244 .into_iter()
245 .filter(|a| a.starts_with(current_word))
246 .map(|a| Pair {
247 display: a.clone(),
248 replacement: a,
249 })
250 .collect(),
251 ArgHint::Category => ALL_NOTE_CATEGORIES
252 .iter()
253 .filter(|c| c.starts_with(current_word))
254 .map(|c| Pair {
255 display: c.to_string(),
256 replacement: c.to_string(),
257 })
258 .collect(),
259 ArgHint::Section => self
260 .all_sections()
261 .into_iter()
262 .filter(|s| s.starts_with(current_word))
263 .map(|s| Pair {
264 display: s.clone(),
265 replacement: s,
266 })
267 .collect(),
268 ArgHint::SectionKeys(section) => self
269 .section_keys(section)
270 .into_iter()
271 .filter(|k| k.starts_with(current_word))
272 .map(|k| Pair {
273 display: k.clone(),
274 replacement: k,
275 })
276 .collect(),
277 ArgHint::Fixed(options) => options
278 .iter()
279 .filter(|o| !o.is_empty() && o.starts_with(current_word))
280 .map(|o| Pair {
281 display: o.to_string(),
282 replacement: o.to_string(),
283 })
284 .collect(),
285 ArgHint::Placeholder(_) => vec![],
286 ArgHint::FilePath => complete_file_path(current_word),
287 ArgHint::None => vec![],
288 };
289 return Ok((start_pos, candidates));
290 }
291 break;
292 }
293 }
294
295 if self.config.alias_exists(cmd_str) {
297 if self.config.contains(constants::section::EDITOR, cmd_str) {
298 return Ok((start_pos, complete_file_path(current_word)));
299 }
300 if self.config.contains(constants::section::BROWSER, cmd_str) {
301 let mut candidates: Vec<Pair> = self
302 .all_aliases()
303 .into_iter()
304 .filter(|a| a.starts_with(current_word))
305 .map(|a| Pair {
306 display: a.clone(),
307 replacement: a,
308 })
309 .collect();
310 candidates.extend(complete_file_path(current_word));
311 return Ok((start_pos, candidates));
312 }
313 let mut candidates = complete_file_path(current_word);
314 candidates.extend(
315 self.all_aliases()
316 .into_iter()
317 .filter(|a| a.starts_with(current_word))
318 .map(|a| Pair {
319 display: a.clone(),
320 replacement: a,
321 }),
322 );
323 return Ok((start_pos, candidates));
324 }
325
326 Ok((start_pos, vec![]))
327 }
328}
329
330pub struct CopilotHinter {
333 history_hinter: HistoryHinter,
334}
335
336impl CopilotHinter {
337 pub fn new() -> Self {
338 Self {
339 history_hinter: HistoryHinter::new(),
340 }
341 }
342}
343
344impl Hinter for CopilotHinter {
345 type Hint = String;
346
347 fn hint(&self, line: &str, pos: usize, ctx: &Context<'_>) -> Option<String> {
348 self.history_hinter.hint(line, pos, ctx)
349 }
350}
351
352pub struct CopilotHighlighter;
355
356impl Highlighter for CopilotHighlighter {
357 fn highlight_hint<'h>(&self, hint: &'h str) -> Cow<'h, str> {
358 Cow::Owned(format!("\x1b[90m{}\x1b[0m", hint))
359 }
360
361 fn highlight_char(&self, _line: &str, _pos: usize, _forced: CmdKind) -> bool {
362 true
363 }
364}
365
366pub struct CopilotHelper {
369 pub completer: CopilotCompleter,
370 hinter: CopilotHinter,
371 highlighter: CopilotHighlighter,
372}
373
374impl CopilotHelper {
375 pub fn new(config: &YamlConfig) -> Self {
376 Self {
377 completer: CopilotCompleter::new(config),
378 hinter: CopilotHinter::new(),
379 highlighter: CopilotHighlighter,
380 }
381 }
382
383 pub fn refresh(&mut self, config: &YamlConfig) {
384 self.completer.refresh(config);
385 }
386}
387
388impl Completer for CopilotHelper {
389 type Candidate = Pair;
390
391 fn complete(
392 &self,
393 line: &str,
394 pos: usize,
395 ctx: &Context<'_>,
396 ) -> rustyline::Result<(usize, Vec<Pair>)> {
397 self.completer.complete(line, pos, ctx)
398 }
399}
400
401impl Hinter for CopilotHelper {
402 type Hint = String;
403
404 fn hint(&self, line: &str, pos: usize, ctx: &Context<'_>) -> Option<String> {
405 self.hinter.hint(line, pos, ctx)
406 }
407}
408
409impl Highlighter for CopilotHelper {
410 fn highlight_hint<'h>(&self, hint: &'h str) -> Cow<'h, str> {
411 self.highlighter.highlight_hint(hint)
412 }
413
414 fn highlight_char(&self, line: &str, pos: usize, forced: CmdKind) -> bool {
415 self.highlighter.highlight_char(line, pos, forced)
416 }
417}
418
419impl Validator for CopilotHelper {}
420
421impl rustyline::Helper for CopilotHelper {}
422
423pub fn complete_file_path(partial: &str) -> Vec<Pair> {
427 let mut candidates = Vec::new();
428
429 let expanded = if partial.starts_with('~') {
430 if let Some(home) = dirs::home_dir() {
431 partial.replacen('~', &home.to_string_lossy(), 1)
432 } else {
433 partial.to_string()
434 }
435 } else {
436 partial.to_string()
437 };
438
439 let (dir_path, file_prefix) =
440 if expanded.ends_with('/') || expanded.ends_with(std::path::MAIN_SEPARATOR) {
441 (std::path::Path::new(&expanded).to_path_buf(), String::new())
442 } else {
443 let p = std::path::Path::new(&expanded);
444 let parent = p
445 .parent()
446 .unwrap_or(std::path::Path::new("."))
447 .to_path_buf();
448 let fp = p
449 .file_name()
450 .map(|s| s.to_string_lossy().to_string())
451 .unwrap_or_default();
452 (parent, fp)
453 };
454
455 if let Ok(entries) = std::fs::read_dir(&dir_path) {
456 for entry in entries.flatten() {
457 let name = entry.file_name().to_string_lossy().to_string();
458 if name.starts_with('.') && !file_prefix.starts_with('.') {
459 continue;
460 }
461 if name.starts_with(&file_prefix) {
462 let is_dir = entry.file_type().map(|t| t.is_dir()).unwrap_or(false);
463 let full_replacement =
464 if partial.ends_with('/') || partial.ends_with(std::path::MAIN_SEPARATOR) {
465 format!("{}{}{}", partial, name, if is_dir { "/" } else { "" })
466 } else if partial.contains('/') || partial.contains(std::path::MAIN_SEPARATOR) {
467 let last_sep = partial
468 .rfind('/')
469 .or_else(|| partial.rfind(std::path::MAIN_SEPARATOR))
470 .unwrap();
471 format!(
472 "{}/{}{}",
473 &partial[..last_sep],
474 name,
475 if is_dir { "/" } else { "" }
476 )
477 } else {
478 format!("{}{}", name, if is_dir { "/" } else { "" })
479 };
480 let display_name = format!("{}{}", name, if is_dir { "/" } else { "" });
481 candidates.push(Pair {
482 display: display_name,
483 replacement: full_replacement,
484 });
485 }
486 }
487 }
488
489 candidates.sort_by(|a, b| a.display.cmp(&b.display));
490 candidates
491}