diff --git a/README.md b/README.md index 07d750a..b55af51 100644 --- a/README.md +++ b/README.md @@ -117,25 +117,18 @@ zstyle ':completion:complete:*:options' sort false # use input as query string when completing zlua zstyle ':fzf-tab:complete:_zlua:*' query-string input -# (experimental, may change in the future) -# some boilerplate code to define the variable `extract` which will be used later -# please remember to copy them -local extract=" -# trim input(what you select) -local in=\${\${\"\$(<{f})\"%\$'\0'*}#*\$'\0'} -# get ctxt for current completion(some thing before or after the current word) -local -A ctxt=(\"\${(@ps:\2:)CTXT}\") -# real path -local realpath=\${ctxt[IPREFIX]}\${ctxt[hpre]}\$in -realpath=\${(Qe)~realpath} -" - # give a preview of commandline arguments when completing `kill` zstyle ':completion:*:*:*:*:processes' command "ps -u $USER -o pid,user,comm,cmd -w -w" -zstyle ':fzf-tab:complete:kill:argument-rest' extra-opts --preview=$extract'ps --pid=$in[(w)1] -o cmd --no-headers -w -w' --preview-window=down:3:wrap +zstyle ':fzf-tab:complete:kill:argument-rest' extra-opts --preview=$fzf_tab_preview_init'ps --pid=$word -o cmd --no-headers -w -w' --preview-window=down:3:wrap # give a preview of directory by exa when completing cd -zstyle ':fzf-tab:complete:cd:*' extra-opts --preview=$extract'exa -1 --color=always $realpath' +zstyle ':fzf-tab:complete:cd:*' extra-opts --preview=$fzf_tab_preview_init'exa -1 --color=always $realpath' + +# NOTE: `$fzf_tab_preview_init` contains some boilerplate code to init some useful variables: +# $word - the actual word to insert +# $desc - the description of the word, this is what you see +# $group - the group name of what you select +# $realpath - use this if what you select is a file name or path ``` fzf-tab is configured via command like this: `zstyle ':fzf-tab:{context}' tag value`. `fzf-tab` is the top context. @@ -203,6 +196,74 @@ If it is a number, then fzf-tab won't be activated if the number of candidates i Default value: `zstyle ':fzf-tab:*' ignore false` +### compadd-hook + +You can hook on completion value and improve it using `fzf_tab_compadd_hook` + +For example to add a lot of icons on many completions (Nerd icons patched fonts) + +```zsh +fzf_tab_compadd_hook() { + # $expl - explicit group name + # $__hits - list of orginal completions + # $__dscr - list of transformed and described completions + # $PREFIX - text that already got completed before + case $_fzf_tab_curcontext in + # (systemctl-*) + # All context + (*) + # Line by line icon assignements + for i in {1..$#__hits}; do + word=$__hits[i] dscr=$__dscr[i] + if [[ -n $dscr ]]; then + dscr=${dscr//$'\n'} + elif [[ -n $word ]]; then + dscr=$word + fi + + # absolute expanded path of a file + realpath="$(eval echo "${PREFIX}$word")" + + case "$expl" in + (parameter) icon="$" ;; + (function) icon="ƒ" ;; + (alias) icon="Ą" ;; + (*branch*) icon="" ;; + (*command*) icon="" ;; + (*target*) icon="" ;; + (commit tag) icon="" ;; + (*commit*) icon="" ;; + (*file*) + if [ -d $realpath ]; then + icon="" + else + icon="${$(echo "$word" | devicon-lookup):0:1}" + fi + # Only work inside a git directory + # [ -d .git ] && echo .git || git rev-parse --git-dir > /dev/null 2>&1 + # icon="$icon ${$( git status --short "$realpath"):0:2}" + ;; + (*local*) icon="" ;; + (*remote*) icon="" ;; + (*head*) icon="ﰛ" ;; + (*option*) + case $word in + (*rm*|*delete**remove*) + icon=" " ;; + (*help) icon="" ;; # icon=" " icon=" " icon="" + (*) icon="" ;; # icon=" " + esac + ;; + (*) icon="" ;; + esac + icon="$icon " + __dscr[i]="$icon $dscr" + done + ;; +esac +} +``` + ### fake-compadd How to do a fake compadd. This only affects the result of multiple selections. diff --git a/fzf-tab.zsh b/fzf-tab.zsh index 0ed9a8f..fe8595c 100644 --- a/fzf-tab.zsh +++ b/fzf-tab.zsh @@ -15,6 +15,29 @@ FZF_TAB_HOME=${0:h} autoload -Uz $FZF_TAB_HOME/fzf-tmux-popup source ${0:h}/lib/zsh-ls-colors/ls-colors.zsh fzf-tab-lscolors +typeset -g fzf_tab_preview_init=" +local -a _fzf_tab_compcap=(\"\${(@f)\"\$( /dev/null; then + fzf_tab_compadd_hook + fi + # dscr - the string to show to users # word - the string to be inserted local dscr word i @@ -77,7 +105,7 @@ _fzf_tab_compadd() { elif [[ -n $word ]]; then dscr=$word fi - _fzf_tab_compcap+=$dscr$'\2'$__tmp_value${word:+$'\0word\0'$word} + _fzf_tab_compcap+=$dscr$'\2'$__tmp_value$'\0word\0'$word done # tell zsh that the match is successful @@ -420,6 +448,11 @@ _fzf_tab_complete() { _fzf_tab_get -s print-query print_query _fzf_tab_get -a extra-opts opts + # export some variables for previewing + [[ -d /tmp/fzf-tab ]] || mkdir -p /tmp/fzf-tab + echo -E ${(pj:\n:)_fzf_tab_compcap} > /tmp/fzf-tab/compcap.$$ + echo -E ${(pj:\n:)_fzf_tab_groups} > /tmp/fzf-tab/groups.$$ + # TODO: this is deprecated and should be removed in the future export CTXT=${${_fzf_tab_compcap[1]#*$'\2'}//$'\0'/$'\2'} if (( $#headers )); then @@ -429,6 +462,10 @@ _fzf_tab_complete() { fi choices=("${(@f)choices}") + # remember to clean + command rm /tmp/fzf-tab/{compcap,groups}.$$ + + # insert query string directly if [[ $choices[2] == $print_query ]] || [[ -n $choices[1] && $#choices == 1 ]] ; then local -A v=("${(@0)${_fzf_tab_compcap[1]}}") local -a args=("${(@ps:\1:)v[args]}")