1 /// Completion support for Fish
2 #[derive(Copy, Clone, PartialEq, Eq, Debug)]
3 pub struct Fish;
4 
5 impl crate::dynamic::Completer for Fish {
file_name(&self, name: &str) -> String6     fn file_name(&self, name: &str) -> String {
7         format!("{name}.fish")
8     }
write_registration( &self, _name: &str, bin: &str, completer: &str, buf: &mut dyn std::io::Write, ) -> Result<(), std::io::Error>9     fn write_registration(
10         &self,
11         _name: &str,
12         bin: &str,
13         completer: &str,
14         buf: &mut dyn std::io::Write,
15     ) -> Result<(), std::io::Error> {
16         let bin = shlex::quote(bin);
17         let completer = shlex::quote(completer);
18         writeln!(
19             buf,
20             r#"complete -x -c {bin} -a "("'{completer}'" complete --shell fish -- (commandline --current-process --tokenize --cut-at-cursor) (commandline --current-token))""#
21         )
22     }
write_complete( &self, cmd: &mut clap::Command, args: Vec<std::ffi::OsString>, current_dir: Option<&std::path::Path>, buf: &mut dyn std::io::Write, ) -> Result<(), std::io::Error>23     fn write_complete(
24         &self,
25         cmd: &mut clap::Command,
26         args: Vec<std::ffi::OsString>,
27         current_dir: Option<&std::path::Path>,
28         buf: &mut dyn std::io::Write,
29     ) -> Result<(), std::io::Error> {
30         let index = args.len() - 1;
31         let completions = crate::dynamic::complete(cmd, args, index, current_dir)?;
32 
33         for (completion, help) in completions {
34             write!(buf, "{}", completion.to_string_lossy())?;
35             if let Some(help) = help {
36                 write!(
37                     buf,
38                     "\t{}",
39                     help.to_string().lines().next().unwrap_or_default()
40                 )?;
41             }
42             writeln!(buf)?;
43         }
44         Ok(())
45     }
46 }
47