rockandska / fzf-obc

fzf over bash complete
48 stars 6 forks source link

(question) Is there a basic example for a custom completion function? #48

Open slayer152 opened 2 years ago

slayer152 commented 2 years ago

Hello,

I've been using https://github.com/lincheney/fzf-tab-completion/ but want to try fzf-obc as it supports completion post functions.

Is there a basic example to make a custom completion function work with fzf-obc?

Bash version: GNU bash, version 5.0.17(1)-release (x86_64-pc-linux-gnu)

_foo() {
  COMPREPLY=()
  local cur
  cur=$(_get_cword)
  local completions="10foo - first foo
11foo - second foo
12foo - third foo"
  local OLDIFS="$IFS"
  local IFS=$'\n'
  COMPREPLY=( $( compgen -W "$completions" -- "$cur" ) )
  IFS="$OLDIFS"
  if [[ ${#COMPREPLY[*]} -eq 1 ]]; then #Only one completion
    COMPREPLY=( ${COMPREPLY[0]%% - *} ) #Remove ' - ' and everything after
  fi
  return 0
} && complete -F _foo foo

After source'ing fzf-obc

source <path-to-fzf-obc>/fzf-obc.bash

When I press foo <tab-key>, I don't see the completions using the fzf-filter. I only see the default method of completion of bash.

complete -p foo returns complete -F __fzf_obc_wrapper__foo foo , so fzf-obc seems to be doing something but doesn't show the candidates in fzf.

However, when I use it one of the standard linux commands, e.g. ls -, then I see the ls options using the fzf window.

Am I missing an important step to make fzf-obc work with custom completion functions?

Thanks in advance.

rockandska commented 2 years ago

Hi,

It is indeed not specified, but completion functions need to use __get_cword_at_cursor_by_ref to work with fzf-obc. A typical example on how to start a custom function is to check default ones

bash-4.4$ declare -f _cd
_cd () 
{ 
    local cur prev words cword;
    _init_completion || return;
   .....
}

That said, a completion as your example will need a post function as for gradle due to the necessity to clean up the result