Improve Fish's completions (#212)

This commit is contained in:
Kid 2021-09-19 01:26:43 +08:00 committed by GitHub
parent 41b8f08c46
commit 2f2f588595
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 44 additions and 15 deletions

View File

@ -1,5 +1,14 @@
use crate::app::InitHook; use crate::app::InitHook;
const FZF_COMPLETE_OPTS: &str = "\
--bind=ctrl-z:ignore \
--exit-0 \
--height=35% \
--inline-info \
--no-sort \
--reverse \
--select-1";
#[derive(Debug, Eq, PartialEq)] #[derive(Debug, Eq, PartialEq)]
pub struct Opts<'a> { pub struct Opts<'a> {
pub cmd: Option<&'a str>, pub cmd: Option<&'a str>,

View File

@ -122,15 +122,7 @@ if [[ :"${SHELLOPTS}": =~ :(vi|emacs): ]] && [ "${TERM}" != 'dumb' ]; then
if [[ ${query} == *"${trigger}" ]]; then if [[ ${query} == *"${trigger}" ]]; then
query="${query:0:$(({{ "${#query} - ${#trigger}" }}))}" query="${query:0:$(({{ "${#query} - ${#trigger}" }}))}"
COMPREPLY=("$(_ZO_FZF_OPTS="\ COMPREPLY=("$(_ZO_FZF_OPTS='{{ crate::shell::FZF_COMPLETE_OPTS }}' zoxide query -i -- "${query}")")
--bind=ctrl-z:ignore \
--exit-0 \
--height=35% \
--inline-info \
--no-sort \
--reverse \
--select-1 \
" zoxide query -i -- "${query}")")
[[ $? -eq 130 ]] && COMPREPLY=("${query}") [[ $? -eq 130 ]] && COMPREPLY=("${query}")
\builtin printf '\e[5n' \builtin printf '\e[5n'
else else

View File

@ -20,7 +20,7 @@ if ! builtin functions -q __zoxide_cd_internal
if builtin functions -q cd if builtin functions -q cd
builtin functions -c cd __zoxide_cd_internal builtin functions -c cd __zoxide_cd_internal
else else
alias __zoxide_cd_internal="builtin cd" alias __zoxide_cd_internal='builtin cd'
end end
end end
@ -65,9 +65,7 @@ function __zoxide_z
__zoxide_cd $HOME __zoxide_cd $HOME
else if test "$argv" = - else if test "$argv" = -
__zoxide_cd - __zoxide_cd -
else if begin else if test $argc -eq 1 -a -d $argv[1]
test $argc -eq 1; and test -d $argv[1]
end
__zoxide_cd $argv[1] __zoxide_cd $argv[1]
else else
set -l result (command zoxide query --exclude (__zoxide_pwd) -- $argv) set -l result (command zoxide query --exclude (__zoxide_pwd) -- $argv)
@ -75,6 +73,35 @@ function __zoxide_z
end end
end 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 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
end
set -l cmd $line[1]
commandline -p "$cmd "(string escape $result)
end
# Jump to a directory using interactive search. # Jump to a directory using interactive search.
function __zoxide_zi function __zoxide_zi
set -l result (command zoxide query -i -- $argv) set -l result (command zoxide query -i -- $argv)
@ -96,10 +123,11 @@ function __zoxide_unset
end end
__zoxide_unset {{cmd}} __zoxide_unset {{cmd}}
alias {{cmd}}="__zoxide_z" alias {{cmd}}=__zoxide_z
complete -c {{cmd}} -f -a '(__zoxide_z_complete)'
__zoxide_unset {{cmd}}i __zoxide_unset {{cmd}}i
alias {{cmd}}i="__zoxide_zi" alias {{cmd}}i=__zoxide_zi
{%- when None %} {%- when None %}