Skip to content

Commit

Permalink
feat: add pixi run completion for fish shell (#1680)
Browse files Browse the repository at this point in the history
Add one more line to the fish script for code completion to support
listing tasks in `pixi run`.
  • Loading branch information
dennis-wey authored Jul 29, 2024
1 parent 761f2a1 commit 679ea7d
Showing 1 changed file with 21 additions and 0 deletions.
21 changes: 21 additions & 0 deletions src/cli/completion.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ pub(crate) fn execute(args: Args) -> miette::Result<()> {
let script = match args.shell {
Shell::Bash => replace_bash_completion(&script),
Shell::Zsh => replace_zsh_completion(&script),
Shell::Fish => replace_fish_completion(&script),
Shell::Nushell => replace_nushell_completion(&script),
_ => Cow::Owned(script),
};
Expand Down Expand Up @@ -126,6 +127,17 @@ $2::task"#;
re.replace(script, replacement)
}

fn replace_fish_completion(script: &str) -> Cow<str> {
// Adds tab completion to the pixi run command.
let addition = "complete -c pixi -n \"__fish_seen_subcommand_from run\" -f -a \"(string split ' ' (pixi task list --machine-readable 2> /dev/null))\"";
let new_script = format!("{}{}\n", script, addition);
let pattern = r#"-n "__fish_seen_subcommand_from run""#;
let replacement = r#"-n "__fish_seen_subcommand_from run; or __fish_seen_subcommand_from r""#;
let re = Regex::new(pattern).unwrap();
let result = re.replace_all(&new_script, replacement);
Cow::Owned(result.into_owned())
}

/// Replace the parts of the nushell completion script that need different functionality.
fn replace_nushell_completion(script: &str) -> Cow<str> {
// Adds tab completion to the pixi run command.
Expand Down Expand Up @@ -279,6 +291,15 @@ _arguments "${_arguments_options[@]}" \
assert_ne!(replace_zsh_completion(&script), script);
}

#[test]
pub fn test_fish_completion_working_regex() {
// Generate the original completion script.
let script = get_completion_script(Shell::Fish);
let replaced_script = replace_fish_completion(&script);
// Test if there was a replacement done on the clap generated completions
assert_ne!(replaced_script, script);
}

#[test]
pub fn test_nushell_completion_working_regex() {
// Generate the original completion script.
Expand Down

0 comments on commit 679ea7d

Please sign in to comment.