fix: interactive completion for fish (#385)

Co-authored-by: Ajeet D'Souza <98ajeet@gmail.com>
This commit is contained in:
NextAlone 2022-04-27 21:35:52 +08:00 committed by GitHub
parent 6b2c6a2bc3
commit 7d0ddedc6b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 38 additions and 46 deletions

View File

@ -15,7 +15,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
### Fixed
- Fish: fix paths on Cygwin.
- Bash/Fish/Posix/Zsh: paths on Cygwin.
- Fish: completions not working on certain systems.
## [0.8.1] - 2021-04-23

View File

@ -34,7 +34,8 @@ function __zoxide_cd() {
{%- if hook == InitHook::Prompt %}
function __zoxide_hook() {
\builtin local -r retval="$?"
\command zoxide add -- "$(__zoxide_pwd || \builtin true)"
# shellcheck disable=SC2312
\command zoxide add -- "$(__zoxide_pwd)"
return "${retval}"
}
{%- else if hook == InitHook::Pwd %}
@ -80,7 +81,8 @@ function __zoxide_z() {
__zoxide_cd "{{ "${result:${#__zoxide_z_prefix}}" }}"
else
\builtin local result
result="$(\command zoxide query --exclude "$(__zoxide_pwd || \builtin true)" -- "$@")" &&
# shellcheck disable=SC2312
result="$(\command zoxide query --exclude "$(__zoxide_pwd)" -- "$@")" &&
__zoxide_cd "${result}"
fi
}
@ -98,19 +100,12 @@ function __zoxide_zi() {
{%- match cmd %}
{%- when Some with (cmd) %}
# Remove definitions.
function __zoxide_unset() {
\builtin unset -f "$@" &>/dev/null
\builtin unset -v "$@" &>/dev/null
\builtin unalias "$@" &>/dev/null || \builtin :
}
__zoxide_unset {{cmd}}
\builtin unalias {{cmd}} &>/dev/null || \builtin true
function {{cmd}}() {
__zoxide_z "$@"
}
__zoxide_unset {{cmd}}i
\builtin unalias {{cmd}}i &>/dev/null || \builtin true
function {{cmd}}i() {
__zoxide_zi "$@"
}
@ -136,13 +131,15 @@ if [[ ${BASH_VERSINFO[0]:-0} -eq 4 && ${BASH_VERSINFO[1]:-0} -ge 4 || ${BASH_VER
# If there is a space after the last word, use interactive selection.
elif [[ -z ${COMP_WORDS[-1]} ]]; then
\builtin local result
result="$(\command zoxide query -i -- "{{ "${COMP_WORDS[@]:1:${#COMP_WORDS[@]}-2}" }}")" &&
# shellcheck disable=SC2312
result="$(\command zoxide query --exclude "$(__zoxide_pwd)" -i -- "{{ "${COMP_WORDS[@]:1:${#COMP_WORDS[@]}-2}" }}")" &&
COMPREPLY=("${__zoxide_z_prefix}${result@Q}")
\builtin printf '\e[5n'
fi
}
\builtin complete -F __zoxide_z_complete -o nospace -- {{cmd}}
\builtin complete -r {{cmd}}i &>/dev/null || \builtin true
fi
{%- when None %}

View File

@ -62,15 +62,21 @@ end
# When using zoxide with --no-cmd, alias these internal functions as desired.
#
set __zoxide_z_prefix 'z#'
# Jump to a directory using only keywords.
function __zoxide_z
set argc (count $argv)
set -l argc (count $argv)
set -l completion_regex '^'(string escape --style=regex $__zoxide_z_prefix)'(.*)$'
if test $argc -eq 0
__zoxide_cd $HOME
else if test "$argv" = -
__zoxide_cd -
else if test $argc -eq 1 -a -d $argv[1]
__zoxide_cd $argv[1]
else if set -l result (string match --groups-only --regex $completion_regex $argv[-1])
__zoxide_cd $result
else
set -l result (command zoxide query --exclude (__zoxide_pwd) -- $argv)
and __zoxide_cd $result
@ -88,8 +94,8 @@ function __zoxide_z_complete
else if test (count $tokens) -eq (count $curr_tokens)
# If the last argument is empty, use interactive selection.
set -l query $tokens[2..-1]
set -l result (zoxide query -i -- $query)
and commandline --current-process "$tokens[1] "(string escape $result)
set -l result (zoxide query --exclude (__zoxide_pwd) -i -- $query)
and echo $__zoxide_z_prefix$result
commandline --function repaint
end
end
@ -107,18 +113,18 @@ end
{%- match cmd %}
{%- when Some with (cmd) %}
abbr --erase {{cmd}}
complete -c {{cmd}} -e
function {{cmd}}
__zoxide_z $argv
end
abbr --erase {{cmd}}
complete -c {{cmd}} -e
complete -c {{cmd}} -f -a '(__zoxide_z_complete)'
abbr --erase {{cmd}}i
complete -c {{cmd}}i -e
function {{cmd}}i
__zoxide_zi $argv
end
abbr --erase {{cmd}}i
complete -c {{cmd}}i -e
{%- when None %}

View File

@ -7,7 +7,9 @@
# pwd based on the value of _ZO_RESOLVE_SYMLINKS.
__zoxide_pwd() {
{%- if resolve_symlinks %}
{%- if cfg!(windows) %}
\command cygpath -w "$(\builtin pwd -P)"
{%- else if resolve_symlinks %}
\command pwd -P
{%- else %}
\command pwd -L
@ -82,20 +84,12 @@ __zoxide_zi() {
{%- match cmd %}
{%- when Some with (cmd) %}
# Remove definitions.
__zoxide_unset() {
\command unset -f "$@" >/dev/null 2>&1
\command unset -v "$@" >/dev/null 2>&1
# shellcheck disable=SC1001
\command unalias "$@" >/dev/null 2>&1 || \:
}
__zoxide_unset '{{cmd}}'
\command unalias {{cmd}} >/dev/null 2>&1 || \true
{{cmd}}() {
__zoxide_z "$@"
}
__zoxide_unset '{{cmd}}i'
\command unalias {{cmd}}i >/dev/null 2>&1 || \true
{{cmd}}i() {
__zoxide_zi "$@"
}

View File

@ -32,7 +32,8 @@ function __zoxide_cd() {
{% else -%}
# Hook to add new entries to the database.
function __zoxide_hook() {
\command zoxide add -- "$(__zoxide_pwd || \builtin true)"
# shellcheck disable=SC2312
\command zoxide add -- "$(__zoxide_pwd)"
}
# Initialize hook.
@ -74,7 +75,8 @@ function __zoxide_z() {
__zoxide_cd "{{ "${result:${#__zoxide_z_prefix}}" }}"
else
\builtin local result
result="$(\command zoxide query --exclude "$(__zoxide_pwd || \builtin true)" -- "$@")" &&
# shellcheck disable=SC2312
result="$(\command zoxide query --exclude "$(__zoxide_pwd)" -- "$@")" &&
__zoxide_cd "${result}"
fi
}
@ -92,25 +94,17 @@ function __zoxide_zi() {
{%- match cmd %}
{%- when Some with (cmd) %}
# Remove definitions.
function __zoxide_unset() {
\builtin unalias "$@" &>/dev/null || \builtin true
\builtin unfunction "$@" &>/dev/null || \builtin true
\builtin unset "$@" &>/dev/null
}
__zoxide_unset {{cmd}}
\builtin unalias {{cmd}} &>/dev/null || \builtin true
function {{cmd}}() {
__zoxide_z "$@"
}
__zoxide_unset {{cmd}}i
\builtin unalias {{cmd}}i &>/dev/null || \builtin true
function {{cmd}}i() {
__zoxide_zi "$@"
}
if [[ -o zle ]]; then
__zoxide_unset __zoxide_z_complete
function __zoxide_z_complete() {
# Only show completions when the cursor is at the end of the line.
# shellcheck disable=SC2154
@ -120,8 +114,8 @@ if [[ -o zle ]]; then
_files -/
elif [[ "${words[-1]}" == '' ]]; then
\builtin local result
# shellcheck disable=SC2086
if result="$(\command zoxide query -i -- ${words[2,-1]})"; then
# shellcheck disable=SC2086,SC2312
if result="$(\command zoxide query --exclude "$(__zoxide_pwd)" -i -- ${words[2,-1]})"; then
__zoxide_result="${result}"
else
__zoxide_result=''
@ -130,7 +124,6 @@ if [[ -o zle ]]; then
fi
}
__zoxide_unset __zoxide_z_complete_helper
function __zoxide_z_complete_helper() {
\builtin local result="${__zoxide_z_prefix}${__zoxide_result}"
# shellcheck disable=SC2296
@ -142,6 +135,7 @@ if [[ -o zle ]]; then
\builtin bindkey "\e[0n" __zoxide_z_complete_helper
if [[ "${+functions[compdef]}" -ne 0 ]]; then
\compdef -d {{cmd}}
\compdef -d {{cmd}}i
\compdef __zoxide_z_complete {{cmd}}
fi
fi