From 2f2f588595e1b6e568c6503297804954303e00f7 Mon Sep 17 00:00:00 2001 From: Kid <44045911+kidonng@users.noreply.github.com> Date: Sun, 19 Sep 2021 01:26:43 +0800 Subject: [PATCH] Improve Fish's completions (#212) --- src/shell.rs | 9 +++++++++ templates/bash.txt | 10 +--------- templates/fish.txt | 40 ++++++++++++++++++++++++++++++++++------ 3 files changed, 44 insertions(+), 15 deletions(-) diff --git a/src/shell.rs b/src/shell.rs index c20acc33..9a4fe823 100644 --- a/src/shell.rs +++ b/src/shell.rs @@ -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>, diff --git a/templates/bash.txt b/templates/bash.txt index 5ac0c735..6fdc2a51 100644 --- a/templates/bash.txt +++ b/templates/bash.txt @@ -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 diff --git a/templates/fish.txt b/templates/fish.txt index 7af58b82..573f71c9 100644 --- a/templates/fish.txt +++ b/templates/fish.txt @@ -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 @@ -65,9 +65,7 @@ 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) @@ -75,6 +73,35 @@ function __zoxide_z 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) @@ -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 %}