From e2ce26121072adb8e4632f1f7de29a06f7e0662b Mon Sep 17 00:00:00 2001 From: a-kenji Date: Sat, 5 Mar 2022 16:23:51 +0100 Subject: [PATCH] add(comp): dynamic completions for fish (#1176) And infrastructure to make it possible to add more dynamic completions for different shells in the future. eg: ``` zellij attach [completes-active-sessions] zellij kill-session [completes-active-sessions] ``` fixes: #1030 --- zellij-utils/assets/completions/README.md | 2 ++ zellij-utils/assets/completions/comp.fish | 8 ++++++++ zellij-utils/src/setup.rs | 17 +++++++++++++++++ 3 files changed, 27 insertions(+) create mode 100644 zellij-utils/assets/completions/README.md create mode 100644 zellij-utils/assets/completions/comp.fish diff --git a/zellij-utils/assets/completions/README.md b/zellij-utils/assets/completions/README.md new file mode 100644 index 00000000..d69eb510 --- /dev/null +++ b/zellij-utils/assets/completions/README.md @@ -0,0 +1,2 @@ +Extra completion files that get appended to the +clap output, in order to support dynamic commands. diff --git a/zellij-utils/assets/completions/comp.fish b/zellij-utils/assets/completions/comp.fish new file mode 100644 index 00000000..a672b7e4 --- /dev/null +++ b/zellij-utils/assets/completions/comp.fish @@ -0,0 +1,8 @@ +function __fish_complete_sessions + zellij list-sessions 2>/dev/null +end +complete -c zellij -n "__fish_seen_subcommand_from attach" -f -a "(__fish_complete_sessions)" -d "Session" +complete -c zellij -n "__fish_seen_subcommand_from a" -f -a "(__fish_complete_sessions)" -d "Session" +complete -c zellij -n "__fish_seen_subcommand_from kill-session" -f -a "(__fish_complete_sessions)" -d "Session" +complete -c zellij -n "__fish_seen_subcommand_from a" -f -a "(__fish_complete_sessions)" -d "Session" +complete -c zellij -n "__fish_seen_subcommand_from setup --generate-completion" -f -a "bash elvish fish zsh powershell" -d "Shell" diff --git a/zellij-utils/src/setup.rs b/zellij-utils/src/setup.rs index 565e72c9..0912e016 100644 --- a/zellij-utils/src/setup.rs +++ b/zellij-utils/src/setup.rs @@ -109,6 +109,12 @@ pub const NO_STATUS_LAYOUT: &[u8] = include_bytes!(concat!( "assets/layouts/disable-status-bar.yaml" )); +pub const FISH_EXTRA_COMPLETION: &[u8] = include_bytes!(concat!( + env!("CARGO_MANIFEST_DIR"), + "/", + "assets/completions/comp.fish" +)); + pub fn dump_default_config() -> std::io::Result<()> { dump_asset(DEFAULT_CONFIG) } @@ -387,6 +393,17 @@ impl Setup { }; let mut out = std::io::stdout(); clap_complete::generate(shell, &mut CliArgs::into_app(), "zellij", &mut out); + // add shell dependent extra completion + match shell { + Shell::Bash => {} + Shell::Elvish => {} + Shell::Fish => { + let _ = out.write_all(&FISH_EXTRA_COMPLETION); + } + Shell::PowerShell => {} + Shell::Zsh => {} + _ => {} + }; } }