Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve completion descriptions #133

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
91 changes: 76 additions & 15 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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.
Expand Down
39 changes: 38 additions & 1 deletion fzf-tab.zsh
Original file line number Diff line number Diff line change
Expand Up @@ -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)\"\$(</tmp/fzf-tab/compcap.$$)\"}\")
local -a _fzf_tab_groups=(\"\${(@f)\"\$(</tmp/fzf-tab/groups.$$)\"}\")
local bs=\$'\2'
# get descriptoin
local desc=\${\${\"\$(<{f})\"%\$'\0'*}#*\$'\0'}
# get ctxt for current completion
local -A ctxt=(\"\${(@0)\${_fzf_tab_compcap[(r)\${(b)desc}\$bs*]#*\$bs}}\")
# get group
local gid=\$ctxt[group]
local group=\$_fzf_tab_groups[gid]
# get original word
local word=\$ctxt[word]
# get real path if it is file
if (( \$+ctxt[isfile] )); then
local realpath=\${(Qe)~\${:-\${ctxt[IPREFIX]}\${ctxt[hpre]}}}\$word
fi
"

_fzf_tab_debug() {
echo -E $'\n'${(qqqq)1}$'\n'
}

# thanks Valodim/zsh-capture-completion
_fzf_tab_compadd() {
# parse all options
Expand Down Expand Up @@ -67,6 +90,11 @@ _fzf_tab_compadd() {
_opts+=("${(@kv)apre}" "${(@kv)hpre}" $isfile)
__tmp_value+=$'\0args\0'${(pj:\1:)_opts}

# Hook defined by user to alter the description of the completion
if typeset -f fzf_tab_compadd_hook > /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
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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]}")
Expand Down