Skip to content

Commit

Permalink
add(comp): dynamic completions for fish (#1176)
Browse files Browse the repository at this point in the history
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
  • Loading branch information
a-kenji authored Mar 5, 2022
1 parent be022e2 commit e2ce261
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 0 deletions.
2 changes: 2 additions & 0 deletions zellij-utils/assets/completions/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Extra completion files that get appended to the
clap output, in order to support dynamic commands.
8 changes: 8 additions & 0 deletions zellij-utils/assets/completions/comp.fish
Original file line number Diff line number Diff line change
@@ -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"

This comment has been minimized.

Copy link
@folliehiyuki

folliehiyuki Mar 5, 2022

Contributor

Typo? Shouldn't it be __fish_seen_subcommand_from k?

This comment has been minimized.

Copy link
@a-kenji

a-kenji Mar 5, 2022

Author Contributor

Oh yes, you are right!

complete -c zellij -n "__fish_seen_subcommand_from setup --generate-completion" -f -a "bash elvish fish zsh powershell" -d "Shell"

This comment has been minimized.

Copy link
@folliehiyuki

folliehiyuki Mar 5, 2022

Contributor

Suggest complete -c zellij -n "__fish_seen_subcommand_from setup" -l "generate-completion" -x -a "bash elvish fish zsh powershell" -d "Shell"

This comment has been minimized.

Copy link
@a-kenji

a-kenji Mar 5, 2022

Author Contributor

👍 , good idea!

17 changes: 17 additions & 0 deletions zellij-utils/src/setup.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down Expand Up @@ -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 => {}
_ => {}
};
}
}

Expand Down

0 comments on commit e2ce261

Please sign in to comment.