Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 28 additions & 0 deletions build.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,23 @@
use std::fs;
use std::path::{Path, PathBuf};

use clap_complete::{generate_to, Shell};

include!("src/cli.rs");

const FISH_COMMAND_COMPLETION: &str = "\
# Complete executables from $PATH for benchmark command arguments (see #872)
complete -c hyperfine -n \"__fish_use_subcommand\" -x -a \"(__fish_complete_command)\"
";

fn patch_fish_completions(path: &Path) {
let mut content = fs::read_to_string(path).expect("read fish completions");
if !content.contains("__fish_use_subcommand") {

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P1 Badge Guard fish patch with a unique marker

The contains("__fish_use_subcommand") check prevents the patch from being applied in practice. build_command() defines no subcommands, and clap_complete’s fish generator already emits __fish_use_subcommand in generated completions for that case, so hyperfine.fish will already contain this token before patching. As a result, FISH_COMMAND_COMPLETION is never appended and the positional executable completion fix does not take effect. Check for a marker unique to the injected snippet (for example the comment line or the full complete -c hyperfine ... (__fish_complete_command) string) instead.

Useful? React with 👍 / 👎.

content.push_str(FISH_COMMAND_COMPLETION);
fs::write(path, content).expect("write fish completions");
}
}

fn main() {
let var = std::env::var_os("SHELL_COMPLETIONS_DIR").or_else(|| std::env::var_os("OUT_DIR"));
let outdir = match var {
Expand All @@ -21,5 +35,19 @@ fn main() {
Shell::Elvish,
] {
generate_to(shell, &mut command, "hyperfine", &outdir).unwrap();
if shell == Shell::Fish {
patch_fish_completions(&PathBuf::from(&outdir).join("hyperfine.fish"));
}
}
}

#[cfg(test)]
mod tests {
use super::FISH_COMMAND_COMPLETION;

#[test]
fn fish_command_completion_snippet_uses_path_executable_helper() {
assert!(FISH_COMMAND_COMPLETION.contains("__fish_use_subcommand"));
assert!(FISH_COMMAND_COMPLETION.contains("__fish_complete_command"));
}
}
1 change: 1 addition & 0 deletions src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@ fn build_command() -> Command {
.long("reference")
.action(ArgAction::Set)
.value_name("CMD")
.value_hint(ValueHint::CommandString)
.help(
"The reference command for the relative comparison of results. \
If this is unset, results are compared with the fastest command as reference."
Expand Down