Skip to content

Commit

Permalink
Improved completions for Fish
Browse files Browse the repository at this point in the history
  • Loading branch information
ajeetdsouza committed Nov 1, 2021
1 parent 904bfd4 commit ba7b445
Show file tree
Hide file tree
Showing 6 changed files with 38 additions and 31 deletions.
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

### Changed

- Bash: improved completions for `z` command.
- Bash/Fish: improved completions for `z` command.

### Fixed

Expand Down
2 changes: 1 addition & 1 deletion build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ fn main() {
fn git_version() -> Option<String> {
let dir = env!("CARGO_MANIFEST_DIR");
let mut git = Command::new("git");
git.args(&["-C", &dir, "describe", "--tags", "--match=v*.*.*", "--always", "--broken"]);
git.args(&["-C", dir, "describe", "--tags", "--match=v*.*.*", "--always", "--broken"]);

let output = git.output().ok()?;
if !output.status.success() || output.stdout.is_empty() || !output.stderr.is_empty() {
Expand Down
27 changes: 23 additions & 4 deletions src/shell.rs
Original file line number Diff line number Diff line change
Expand Up @@ -162,9 +162,8 @@ mod tests {
let tempdir = tempfile::tempdir().unwrap();
let tempdir = tempdir.path().to_str().unwrap();

Command::new("fish")
Command::new("fish_indent")
.env("HOME", tempdir)
.args(&["--command", "fish_indent", "--private"])
.write_stdin(source.to_string())
.assert()
.success()
Expand Down Expand Up @@ -193,6 +192,26 @@ mod tests {
}
}

#[rstest]
fn posix_bash(
#[values(None, Some("z"))] cmd: Option<&str>,
#[values(InitHook::None, InitHook::Prompt, InitHook::Pwd)] hook: InitHook,
#[values(false, true)] echo: bool,
#[values(false, true)] resolve_symlinks: bool,
) {
let opts = Opts { cmd, hook, echo, resolve_symlinks };
let source = Posix(&opts).render().unwrap();

let assert = Command::new("bash")
.args(&["--posix", "--noprofile", "--norc", "-e", "-u", "-o", "pipefail", "-c", &source])
.assert()
.success()
.stderr("");
if opts.hook != InitHook::Pwd {
assert.stdout("");
}
}

#[rstest]
fn posix_dash(
#[values(None, Some("z"))] cmd: Option<&str>,
Expand All @@ -203,7 +222,7 @@ mod tests {
let opts = Opts { cmd, hook, echo, resolve_symlinks };
let source = Posix(&opts).render().unwrap();

let assert = Command::new("dash").args(&["-c", &source]).assert().success().stderr("");
let assert = Command::new("dash").args(&["-c", &source, "-e", "-u"]).assert().success().stderr("");
if opts.hook != InitHook::Pwd {
assert.stdout("");
}
Expand Down Expand Up @@ -361,7 +380,7 @@ mod tests {
let source = Zsh(&opts).render().unwrap();

Command::new("zsh")
.args(&["-c", &source, "--no-globalrcs", "--no-rcs"])
.args(&["-c", &source, "-e", "-u", "-o", "pipefail", "--no-globalrcs", "--no-rcs"])
.assert()
.success()
.stdout("")
Expand Down
2 changes: 1 addition & 1 deletion templates/bash.txt
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ if [[ :"${SHELLOPTS}": =~ :(vi|emacs): && ${TERM} != 'dumb' ]]; then
# If there is only one argument, use `cd` completions.
if [[ {{ "${#COMP_WORDS[@]}" }} -eq 2 ]]; then
\builtin mapfile -t COMPREPLY < <(compgen -A directory -S / -- "${COMP_WORDS[-1]}")
# Otherwise, use interactive selection.
# If there is a space after the last word, use interactive selection.
elif [[ -z ${COMP_WORDS[-1]} ]]; then
\local result
result="$(
Expand Down
34 changes: 11 additions & 23 deletions templates/fish.txt
Original file line number Diff line number Diff line change
Expand Up @@ -74,31 +74,19 @@ end

# Completions for `z`.
function __zoxide_z_complete
set -l trigger '**'
set -l trigger_length (string length $trigger)

set -l line (commandline -op)
set -l interactive 0
if test (string sub -s "-$trigger_length" $line[-1]) = $trigger
set line[-1] (string sub -e "-$trigger_length" $line[-1])
set interactive 1
end
set -l tokens (commandline -op)
set -l curr_tokens (commandline -cop)

set -l query $line[2..-1]
if test $interactive -eq 0 -a (count (commandline -cop)) -le 1
__fish_complete_directories "$query" ''
return
end
set -l result (_ZO_FZF_OPTS='{{ crate::shell::FZF_COMPLETE_OPTS }}' zoxide query -i -- $query)
set -l exit $status
if test $exit -ne 0
test $exit -eq 130
and commandline -p "$line"
return
if test (count $tokens) -le 2 -a (count $curr_tokens) -eq 1
# If there is only one argument, use `cd` completions.
__fish_complete_directories "$tokens[2]" ''
else
# Otherwise, use interactive selection.
set -l query $tokens[2..-1]
set -l result (_ZO_FZF_OPTS='{{ crate::shell::FZF_COMPLETE_OPTS }}' zoxide query -i -- $query)
and commandline -p "$tokens[1] "(string escape $result)
commandline -f repaint
end

set -l cmd $line[1]
commandline -p "$cmd "(string escape $result)
end

# Jump to a directory using interactive search.
Expand Down
2 changes: 1 addition & 1 deletion templates/posix.txt
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ __zoxide_hook() {
}

# Initialize hook.
if [ "${PS1#*\$(__zoxide_hook)}" = "${PS1}" ]; then
if [ "${PS1:=}" = "${PS1#*\$(__zoxide_hook)}" ]; then
PS1="${PS1}\$(__zoxide_hook)"
fi

Expand Down

0 comments on commit ba7b445

Please sign in to comment.