omelette icon indicating copy to clipboard operation
omelette copied to clipboard

Fall back to _filedir, compgen -f, etc. if no matches are returned

Open beaugunderson opened this issue 11 years ago • 7 comments

Programs that use omelette completion can no longer complete files and directories. I modified my completion script to fall back to _filedir (it relies on the $cur and $prev variables):

  _p_complette() {
    local cur prev

    cur=${COMP_WORDS[COMP_CWORD]}
    prev=${COMP_WORDS[COMP_CWORD-1]}

    COMPREPLY=( $(compgen -W '$(projects --compbash --compgen "${COMP_CWORD}" "${prev}" "${COMP_LINE}")' -- "$cur") )

    [[ $COMPREPLY ]] && return

    _filedir
  }
  complete -F _p_complette p

In the same way compgen -f with compopt -o filenames could be used, I think.

beaugunderson avatar Dec 10 '14 10:12 beaugunderson

+1

wcandillon avatar Mar 18 '15 00:03 wcandillon

@beaugunderson Is there a way I can use your solution in my own omelette integration?

wcandillon avatar Mar 18 '15 00:03 wcandillon

@wcandillon yeah, just modify the completion function after omelette generates it; here's the diff of mine:

--- projects-unmodified.sh  2015-03-17 18:23:39.000000000 -0700
+++ projects-modified.sh    2014-12-13 14:00:02.000000000 -0800
@@ -6,8 +6,39 @@
   compdef _projects_complette projects
 elif type complete &>/dev/null; then
   _projects_complette() {
-    COMPREPLY=( $(compgen -W '$(projects --compbash --compgen "${COMP_CWORD}" "${COMP_WORDS[COMP_CWORD-1]}" "${COMP_LINE}")' -- "${COMP_WORDS[COMP_CWORD]}") )
+    local cur prev
+
+    cur=${COMP_WORDS[COMP_CWORD]}
+    prev=${COMP_WORDS[COMP_CWORD-1]}
+
+    COMPREPLY=( $(compgen -W '$(projects --compbash --compgen "${COMP_CWORD}" "${prev}" "${COMP_LINE}")' -- "$cur") )
+
+    [[ $COMPREPLY ]] && return
+
+    _filedir
   }
   complete -F _projects_complette projects
 fi
 ### projects completion - end ###

projects here is the name of the command I'm adding tab completion to.

beaugunderson avatar Mar 18 '15 01:03 beaugunderson

The changes I made were:

  • Use the cur and prev variables that the bash builtins like _filedir rely on
  • Try the omelette completion, and if it returns anything, return from the function
  • If it doesn't return anything, try completing with _filedir, which is a builtin that completes files and directories

beaugunderson avatar Mar 18 '15 01:03 beaugunderson

@beaugunderson thks a lot!

wcandillon avatar Mar 18 '15 01:03 wcandillon

@beaugunderson On the fallback I'm getting: _filedir: command not found

wcandillon avatar Mar 18 '15 02:03 wcandillon

@wcandillon What operating system?

On OS X you can brew install bash-completion if you have homebrew installed.

On Ubuntu you can apt-get install bash-completion.

You could also try replacing the line _filedir with:

compopt -o filenames
compgen -f

But I think _filedir provides some additional niceties so it's worth installing bash-completion.

beaugunderson avatar Mar 18 '15 02:03 beaugunderson