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

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

Open
beaugunderson opened this issue Dec 10, 2014 · 7 comments · May be fixed by #5
Open

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

beaugunderson opened this issue Dec 10, 2014 · 7 comments · May be fixed by #5

Comments

@beaugunderson
Copy link

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.

@wcandillon
Copy link

+1

@wcandillon
Copy link

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

@beaugunderson
Copy link
Author

@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
Copy link
Author

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

@wcandillon
Copy link

@beaugunderson thks a lot!

@wcandillon
Copy link

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

@beaugunderson
Copy link
Author

@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.

@wcandillon wcandillon linked a pull request Jan 19, 2016 that will close this issue
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants