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
5 changes: 5 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
8 changes: 7 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
```

Expand All @@ -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
Expand Down
3 changes: 2 additions & 1 deletion src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -262,6 +262,7 @@ pub fn main() -> ! {
fn run() -> Result<()> {
let mut args: Vec<OsString> = 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)),
_ => {}
Expand All @@ -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(());
}

Expand Down
13 changes: 13 additions & 0 deletions src/completion/completion.bash
Original file line number Diff line number Diff line change
@@ -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
7 changes: 7 additions & 0 deletions src/completion/completion.fish
Original file line number Diff line number Diff line change
@@ -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)'
27 changes: 27 additions & 0 deletions src/completion/completion.zsh
Original file line number Diff line number Diff line change
@@ -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
70 changes: 9 additions & 61 deletions src/completion.rs → src/completion/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<OsString>) -> 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(())
}

Expand Down
3 changes: 3 additions & 0 deletions tests/completion.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand All @@ -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();

Expand All @@ -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();

Expand Down