Skip to content

Commit

Permalink
Improve Fish's completions (#212)
Browse files Browse the repository at this point in the history
  • Loading branch information
kidonng authored Sep 18, 2021
1 parent 41b8f08 commit 2f2f588
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 15 deletions.
9 changes: 9 additions & 0 deletions src/shell.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,14 @@
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)]
pub struct Opts<'a> {
pub cmd: Option<&'a str>,
Expand Down
10 changes: 1 addition & 9 deletions templates/bash.txt
Original file line number Diff line number Diff line change
Expand Up @@ -122,15 +122,7 @@ if [[ :"${SHELLOPTS}": =~ :(vi|emacs): ]] && [ "${TERM}" != 'dumb' ]; then

if [[ ${query} == *"${trigger}" ]]; then
query="${query:0:$(({{ "${#query} - ${#trigger}" }}))}"
COMPREPLY=("$(_ZO_FZF_OPTS="\
--bind=ctrl-z:ignore \
--exit-0 \
--height=35% \
--inline-info \
--no-sort \
--reverse \
--select-1 \
" zoxide query -i -- "${query}")")
COMPREPLY=("$(_ZO_FZF_OPTS='{{ crate::shell::FZF_COMPLETE_OPTS }}' zoxide query -i -- "${query}")")
[[ $? -eq 130 ]] && COMPREPLY=("${query}")
\builtin printf '\e[5n'
else
Expand Down
40 changes: 34 additions & 6 deletions templates/fish.txt
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ if ! builtin functions -q __zoxide_cd_internal
if builtin functions -q cd
builtin functions -c cd __zoxide_cd_internal
else
alias __zoxide_cd_internal="builtin cd"
alias __zoxide_cd_internal='builtin cd'
end
end

Expand Down Expand Up @@ -65,16 +65,43 @@ function __zoxide_z
__zoxide_cd $HOME
else if test "$argv" = -
__zoxide_cd -
else if begin
test $argc -eq 1; and test -d $argv[1]
end
else if test $argc -eq 1 -a -d $argv[1]
__zoxide_cd $argv[1]
else
set -l result (command zoxide query --exclude (__zoxide_pwd) -- $argv)
and __zoxide_cd $result
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.
function __zoxide_zi
set -l result (command zoxide query -i -- $argv)
Expand All @@ -96,10 +123,11 @@ function __zoxide_unset
end

__zoxide_unset {{cmd}}
alias {{cmd}}="__zoxide_z"
alias {{cmd}}=__zoxide_z
complete -c {{cmd}} -f -a '(__zoxide_z_complete)'

__zoxide_unset {{cmd}}i
alias {{cmd}}i="__zoxide_zi"
alias {{cmd}}i=__zoxide_zi

{%- when None %}

Expand Down

0 comments on commit 2f2f588

Please sign in to comment.