diff --git a/Cargo.toml b/Cargo.toml index 065c1a4..8a2cebc 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -4,6 +4,11 @@ version = "0.1.0" edition = "2024" license = "GPL-3.0-or-later" +[features] +default = ["completion-scripts"] +# Enables the `c completion [shell]` command. Distributions shipping the files directly can disable this to get a smaller binary. +completion-scripts = [] + [[bin]] name = "quilt" path = "src/bin/quilt.rs" diff --git a/README.md b/README.md index 0d4581a..a8554a0 100644 --- a/README.md +++ b/README.md @@ -94,7 +94,7 @@ skips the amend. ### Shell completion ```sh -source <(q completion bash) # or zsh +source <(q completion bash) # or zsh, fish q completion fish | source ``` @@ -103,6 +103,12 @@ by its basename when that is unique, so `platform/fix-build.patch` completes from `fix-build`. Commands accept these basenames as well, and `q new` completes existing patch directories. +> [!NOTE] +> Distributions may opt to ship the shell files (available in `src/completion`) +> separately to install them in the shells with no setup required. +> In that case, the `q completion` command should then be disabled via the +> `completion-scripts` Cargo feature flag to save on binary size. + ### Other differences - `q graph --lines` connects patches that change overlapping lines, rather diff --git a/src/cli.rs b/src/cli.rs index 50de066..1dc09fd 100644 --- a/src/cli.rs +++ b/src/cli.rs @@ -262,6 +262,7 @@ pub fn main() -> ! { fn run() -> Result<()> { let mut args: Vec = env::args_os().skip(1).collect(); match args.first().and_then(|arg| arg.to_str()) { + #[cfg(feature = "completion-scripts")] Some("completion") => return completion::cmd_completion(args.split_off(1)), Some("__complete") => return completion::cmd_complete(args.split_off(1)), _ => {} @@ -271,7 +272,7 @@ fn run() -> Result<()> { return print_usage(); } if args.len() == 1 && args[0] == "--version" { - println!("{}", env!("Q_VERSION")); + println!("q (quilt-compatible) {}", env!("Q_VERSION")); return Ok(()); } diff --git a/src/completion/completion.bash b/src/completion/completion.bash new file mode 100644 index 0000000..4499b0b --- /dev/null +++ b/src/completion/completion.bash @@ -0,0 +1,13 @@ +_q_complete() { + local cur + cur="${COMP_WORDS[COMP_CWORD]}" + COMPREPLY=( $(COMP_CWORD="$COMP_CWORD" q __complete "${COMP_WORDS[@]:1}" -- "$cur") ) + local reply + for reply in "${COMPREPLY[@]}"; do + if [[ "$reply" == */ ]]; then + compopt -o nospace 2>/dev/null || true + break + fi + done +} +complete -o default -F _q_complete q quilt diff --git a/src/completion/completion.fish b/src/completion/completion.fish new file mode 100644 index 0000000..f9feca9 --- /dev/null +++ b/src/completion/completion.fish @@ -0,0 +1,7 @@ +function __q_complete + set -l tokens (commandline -opc) + set -l cur (commandline -ct) + q __complete $tokens -- $cur +end +complete -c q -a '(__q_complete)' +complete -c quilt -a '(__q_complete)' diff --git a/src/completion/completion.zsh b/src/completion/completion.zsh new file mode 100644 index 0000000..01a06cb --- /dev/null +++ b/src/completion/completion.zsh @@ -0,0 +1,27 @@ +_q_complete() { + local -a completions + local completion + local no_space + completions=() + no_space=0 + while IFS= read -r completion; do + completions+=("$completion") + if [[ "$completion" == */ ]]; then + no_space=1 + fi + done < <(q __complete "${words[@]}" -- "$words[CURRENT]") + if (( ${#completions[@]} )); then + if (( no_space )); then + compadd -S '' -- "${completions[@]}" + else + compadd -- "${completions[@]}" + fi + else + _files + fi +} +if ! whence -w compdef >/dev/null; then + autoload -Uz compinit + compinit +fi +compdef _q_complete q quilt diff --git a/src/completion.rs b/src/completion/mod.rs similarity index 80% rename from src/completion.rs rename to src/completion/mod.rs index 9fd5a65..b7799cb 100644 --- a/src/completion.rs +++ b/src/completion/mod.rs @@ -8,73 +8,21 @@ use std::collections::HashMap; use std::ffi::OsString; use std::path::{Component, Path, PathBuf}; +#[cfg(feature = "completion-scripts")] pub(crate) fn cmd_completion(args: Vec) -> Result<()> { let shell = args .first() .map(|arg| arg.to_string_lossy()) .unwrap_or_default(); - match shell.as_ref() { - "bash" => { - let script = r#"_q_complete() { - local cur - cur="${COMP_WORDS[COMP_CWORD]}" - COMPREPLY=( $(COMP_CWORD="$COMP_CWORD" q __complete "${COMP_WORDS[@]:1}" -- "$cur") ) - local reply - for reply in "${COMPREPLY[@]}"; do - if [[ "$reply" == */ ]]; then - compopt -o nospace 2>/dev/null || true - break - fi - done -} -complete -o default -F _q_complete q quilt -"#; - print!("{script}"); - } - "zsh" => { - let script = r#"_q_complete() { - local -a completions - local completion - local no_space - completions=() - no_space=0 - while IFS= read -r completion; do - completions+=("$completion") - if [[ "$completion" == */ ]]; then - no_space=1 - fi - done < <(q __complete "${words[@]}" -- "$words[CURRENT]") - if (( ${#completions[@]} )); then - if (( no_space )); then - compadd -S '' -- "${completions[@]}" - else - compadd -- "${completions[@]}" - fi - else - _files - fi -} -if ! whence -w compdef >/dev/null; then - autoload -Uz compinit - compinit -fi -compdef _q_complete q quilt -"#; - print!("{script}"); - } - "fish" => { - let script = r#"function __q_complete - set -l tokens (commandline -opc) - set -l cur (commandline -ct) - q __complete $tokens -- $cur -end -complete -c q -a '(__q_complete)' -complete -c quilt -a '(__q_complete)' -"#; - print!("{script}"); - } + + let script = match shell.as_ref() { + "bash" => include_str!("completion.bash"), + "zsh" => include_str!("completion.zsh"), + "fish" => include_str!("completion.fish"), _ => bail!("Usage: quilt completion {{bash|zsh|fish}}"), - } + }; + + print!("{script}"); Ok(()) } diff --git a/tests/completion.rs b/tests/completion.rs index dfc941a..4c28f42 100644 --- a/tests/completion.rs +++ b/tests/completion.rs @@ -13,6 +13,7 @@ fn hidden_completion_suggests_commands() { } #[test] +#[cfg(feature = "completion-scripts")] fn completion_glue_falls_back_to_files() { let dir = tempdir().unwrap(); @@ -37,6 +38,7 @@ fn completion_glue_falls_back_to_files() { } #[test] +#[cfg(feature = "completion-scripts")] fn zsh_completion_initializes_completion_system() { let dir = tempdir().unwrap(); @@ -50,6 +52,7 @@ fn zsh_completion_initializes_completion_system() { } #[test] +#[cfg(feature = "completion-scripts")] fn zsh_completion_avoids_nested_command_substitution_expansion() { let dir = tempdir().unwrap();