From 60de78b5d1f86bc8fd6a1ee954b3e97cd0e2a2fb Mon Sep 17 00:00:00 2001 From: Ilia Zyrin Date: Thu, 12 Nov 2026 02:12:23 +0100 Subject: [PATCH] Add shell completions command `proofline completions zsh|bash|fish|powershell|elvish` prints a completion script built with clap_complete. It runs before the base URL is parsed, so it works with no sign-in and makes no network call. The static generators would also offer the hidden `__update-check` command, so the top-level command is rebuilt without hidden subcommands before generating. A shell profile may run this command at every start-up, so it also skips the background update check. --- Cargo.lock | 30 ++++++++++++------ Cargo.toml | 1 + README.md | 5 +++ src/lib.rs | 39 +++++++++++++++++++++++ src/update.rs | 3 +- tests/completions.rs | 74 ++++++++++++++++++++++++++++++++++++++++++++ 6 files changed, 141 insertions(+), 11 deletions(-) create mode 100644 tests/completions.rs diff --git a/Cargo.lock b/Cargo.lock index 8ce9496..bf136dd 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -84,7 +84,7 @@ version = "1.1.5" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "40c48f72fd53cd289104fc64099abca73db4166ad86ea0b4341abe65af83dadc" dependencies = [ - "windows-sys 0.60.2", + "windows-sys 0.61.2", ] [[package]] @@ -95,7 +95,7 @@ checksum = "291e6a250ff86cd4a820112fb8898808a366d8f9f58ce16d1f538353ad55747d" dependencies = [ "anstyle", "once_cell_polyfill", - "windows-sys 0.60.2", + "windows-sys 0.61.2", ] [[package]] @@ -453,6 +453,15 @@ dependencies = [ "strsim", ] +[[package]] +name = "clap_complete" +version = "4.6.11" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "037e2a1a92236d0aff7e845093f64661d6df4c02c9fcc61a60e9e1d736fa392f" +dependencies = [ + "clap", +] + [[package]] name = "clap_derive" version = "4.6.7" @@ -682,7 +691,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "39cab71617ae0d63f51a36d69f866391735b51691dbda63cf6f96d042b63efeb" dependencies = [ "libc", - "windows-sys 0.52.0", + "windows-sys 0.61.2", ] [[package]] @@ -1785,6 +1794,7 @@ dependencies = [ "anyhow", "axum", "clap", + "clap_complete", "flate2", "jsonschema", "keyring", @@ -1857,7 +1867,7 @@ dependencies = [ "once_cell", "socket2", "tracing", - "windows-sys 0.52.0", + "windows-sys 0.61.2", ] [[package]] @@ -2089,7 +2099,7 @@ dependencies = [ "errno", "libc", "linux-raw-sys", - "windows-sys 0.52.0", + "windows-sys 0.61.2", ] [[package]] @@ -2390,7 +2400,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "c3d1e2c7f27f8d4cb10542a02c49005dbd6e93095799d6f3be745fae9f8fedd4" dependencies = [ "libc", - "windows-sys 0.60.2", + "windows-sys 0.61.2", ] [[package]] @@ -2498,10 +2508,10 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "32497e9a4c7b38532efcdebeef879707aa9f794296a4f0244f6f69e9bc8574bd" dependencies = [ "fastrand", - "getrandom 0.3.4", + "getrandom 0.4.3", "once_cell", "rustix", - "windows-sys 0.52.0", + "windows-sys 0.61.2", ] [[package]] @@ -2736,7 +2746,7 @@ checksum = "f2f6fb2847f6742cd76af783a2a2c49e9375d0a111c7bef6f71cd9e738c72d6e" dependencies = [ "memoffset", "tempfile", - "windows-sys 0.60.2", + "windows-sys 0.61.2", ] [[package]] @@ -2961,7 +2971,7 @@ version = "0.1.11" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "c2a7b1c03c876122aa43f3020e6c3c3ee5c05081c9a00739faf7503aeba10d22" dependencies = [ - "windows-sys 0.52.0", + "windows-sys 0.61.2", ] [[package]] diff --git a/Cargo.toml b/Cargo.toml index fc76317..15e6207 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -19,6 +19,7 @@ path = "src/main.rs" [dependencies] anyhow = "1" clap = { version = "4", features = ["derive", "env"] } +clap_complete = "4" flate2 = "1" sha2 = "0.10" tar = "0.4" diff --git a/README.md b/README.md index 25b8097..25ab3dd 100644 --- a/README.md +++ b/README.md @@ -13,6 +13,11 @@ A custom `CLAUDE_CONFIG_DIR` requires manual configuration with `--print`. See the [CLI documentation](https://proofline.sh/docs/cli/) for commands. +`proofline completions SHELL` prints a completion script for `zsh`, `bash`, +`fish`, `powershell` or `elvish`. It needs no sign-in and makes no network call. +For example, add `source <(proofline completions zsh)` to `~/.zshrc`, or write +`proofline completions fish > ~/.config/fish/completions/proofline.fish`. + `proofline review --path src/main.rs` reviews only that file's local changes against the normal merge-base. The path is literal and repository-relative; directories, globs and `..` are refused. Combine it with `--staged` to read diff --git a/src/lib.rs b/src/lib.rs index 6096711..3084c64 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -115,6 +115,17 @@ enum Command { FixPrompt { finding_id: Uuid }, /// Export one page of analytics as CSV or NDJSON. Export(tools::ExportArgs), + /// Print a shell completion script to standard output. + /// + /// For example, add `source <(proofline completions zsh)` to `~/.zshrc`, + /// `proofline completions bash > ~/.local/share/bash-completion/completions/proofline`, + /// `proofline completions fish > ~/.config/fish/completions/proofline.fish`, or + /// `proofline completions powershell | Out-String | Invoke-Expression` to a + /// PowerShell profile. + Completions { + /// The shell to complete for. + shell: clap_complete::Shell, + }, /// Install the newest official release over this installer-managed binary. Update, #[command(name = "__update-check", hide = true)] @@ -367,6 +378,10 @@ pub async fn run(cli: Cli, token: Option) -> anyhow::Result { + completions(*shell, &mut std::io::stdout().lock())?; + return Ok(Verdict::Done); + } Command::Update => return update::install().await.map(|()| Verdict::Done), Command::UpdateCheck => { update::check().await; @@ -376,6 +391,10 @@ pub async fn run(cli: Cli, token: Option) -> anyhow::Result { + completions(shell, &mut std::io::stdout().lock())?; + Ok(Verdict::Done) + } Command::Update => update::install().await.map(|()| Verdict::Done), Command::UpdateCheck => { update::check().await; @@ -545,6 +564,26 @@ async fn connect( Ok(Api::new(http, credential.token, credential.organization_id)) } +/// Writes the completion script for `shell`. It needs no origin, token or +/// network, so it works before `proofline login`. +fn completions(shell: clap_complete::Shell, out: &mut dyn std::io::Write) -> anyhow::Result<()> { + const NAME: &str = "proofline"; + let full = ::command(); + // The static generators offer hidden subcommands too, so leave them out. + let mut command = full.get_arguments().cloned().fold( + clap::Command::new(NAME).version(env!("CARGO_PKG_VERSION")), + clap::Command::arg, + ); + command = full + .get_subcommands() + .filter(|subcommand| !subcommand.is_hide_set()) + .cloned() + .fold(command, clap::Command::subcommand); + clap_complete::generate(shell, &mut command, NAME, out); + out.flush()?; + Ok(()) +} + fn say(line: &str) { // Progress goes to standard error so standard output stays the result. let mut stderr = std::io::stderr().lock(); diff --git a/src/update.rs b/src/update.rs index 76273ab..033fbda 100644 --- a/src/update.rs +++ b/src/update.rs @@ -90,7 +90,8 @@ pub fn on_invocation() { let args: Vec<_> = std::env::args_os().skip(1).collect(); if args .iter() - .any(|arg| arg == "update" || arg == "__update-check") + // A shell evaluates `completions` at every start-up; keep it offline. + .any(|arg| arg == "update" || arg == "__update-check" || arg == "completions") { return; } diff --git a/tests/completions.rs b/tests/completions.rs new file mode 100644 index 0000000..733bd04 --- /dev/null +++ b/tests/completions.rs @@ -0,0 +1,74 @@ +//! `proofline completions` prints a usable script for each supported shell +//! without a token, an origin or the network. +use std::process::{Command, Output}; + +fn completions(shell: &str) -> Output { + Command::new(env!("CARGO_BIN_EXE_proofline")) + .args(["--base-url", "invalid", "completions", shell]) + .env("PROOFLINE_NO_UPDATE_CHECK", "1") + .env_remove("PROOFLINE_TOKEN") + .env_remove("PROOFLINE_ORGANIZATION_ID") + .output() + .unwrap() +} + +#[test] +fn every_shell_gets_a_script_naming_the_visible_commands() { + for (shell, marker) in [ + ("zsh", "#compdef proofline"), + ("bash", "complete -F _proofline"), + ("fish", "complete -c proofline"), + ("powershell", "Register-ArgumentCompleter"), + ] { + let output = completions(shell); + assert!(output.status.success(), "{shell}: {output:?}"); + assert!(output.stderr.is_empty(), "{shell}: {output:?}"); + let script = String::from_utf8(output.stdout).unwrap(); + assert!(script.contains(marker), "{shell} script lacks {marker:?}"); + for visible in ["review", "findings", "login", "completions", "base-url"] { + assert!(script.contains(visible), "{shell} script lacks {visible:?}"); + } + assert!( + !script.contains("__update-check"), + "{shell} script offers a hidden command" + ); + } +} + +#[test] +fn an_unknown_shell_is_refused() { + let output = completions("tcsh"); + assert!(!output.status.success()); + assert!(output.stdout.is_empty()); + assert!(String::from_utf8(output.stderr).unwrap().contains("zsh")); +} + +#[test] +fn the_bash_script_completes_subcommands() { + let Ok(probe) = Command::new("bash").arg("--version").output() else { + return; + }; + if !probe.status.success() { + return; + } + let directory = tempfile::tempdir().unwrap(); + let path = directory.path().join("proofline.bash"); + std::fs::write(&path, completions("bash").stdout).unwrap(); + let output = Command::new("bash") + .arg("-c") + .arg( + r#"source "$1" +COMP_WORDS=(proofline comp) +COMP_CWORD=1 +COMP_LINE="proofline comp" +COMP_POINT=${#COMP_LINE} +_proofline proofline comp proofline +printf '%s\n' "${COMPREPLY[@]}""#, + ) + .arg("bash") + .arg(&path) + .output() + .unwrap(); + assert!(output.status.success(), "{output:?}"); + assert_eq!(String::from_utf8(output.stdout).unwrap(), "completions\n"); +}