summaryrefslogtreecommitdiff
path: root/src/dynamic/shells/fish.rs
blob: 9d7e8c6846bda3c5ca750e87316b9b07590573a2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
/// Fish completions
#[derive(Copy, Clone, PartialEq, Eq, Debug)]
pub struct Fish;

impl crate::dynamic::Completer for Fish {
    fn file_name(&self, name: &str) -> String {
        format!("{name}.fish")
    }
    fn write_registration(
        &self,
        _name: &str,
        bin: &str,
        completer: &str,
        buf: &mut dyn std::io::Write,
    ) -> Result<(), std::io::Error> {
        let bin = shlex::quote(bin);
        let completer = shlex::quote(completer);
        writeln!(
            buf,
            r#"complete -x -c {bin} -a "("'{completer}'" complete --shell fish -- (commandline --current-process --tokenize --cut-at-cursor) (commandline --current-token))""#
        )
    }
    fn 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> {
        let index = args.len() - 1;
        let completions = crate::dynamic::complete(cmd, args, index, current_dir)?;

        for (completion, help) in completions {
            write!(buf, "{}", completion.to_string_lossy())?;
            if let Some(help) = help {
                write!(
                    buf,
                    "\t{}",
                    help.to_string().lines().next().unwrap_or_default()
                )?;
            }
            writeln!(buf)?;
        }
        Ok(())
    }
}