completion.sh 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. #!/bin/bash
  2. ###-begin-npm-completion-###
  3. #
  4. # npm command completion script
  5. #
  6. # Installation: npm completion >> ~/.bashrc (or ~/.zshrc)
  7. # Or, maybe: npm completion > /usr/local/etc/bash_completion.d/npm
  8. #
  9. if type complete &>/dev/null; then
  10. _npm_completion () {
  11. local words cword
  12. if type _get_comp_words_by_ref &>/dev/null; then
  13. _get_comp_words_by_ref -n = -n @ -n : -w words -i cword
  14. else
  15. cword="$COMP_CWORD"
  16. words=("${COMP_WORDS[@]}")
  17. fi
  18. local si="$IFS"
  19. IFS=$'\n' COMPREPLY=($(COMP_CWORD="$cword" \
  20. COMP_LINE="$COMP_LINE" \
  21. COMP_POINT="$COMP_POINT" \
  22. npm completion -- "${words[@]}" \
  23. 2>/dev/null)) || return $?
  24. IFS="$si"
  25. if type __ltrim_colon_completions &>/dev/null; then
  26. __ltrim_colon_completions "${words[cword]}"
  27. fi
  28. }
  29. complete -o default -F _npm_completion npm
  30. elif type compdef &>/dev/null; then
  31. _npm_completion() {
  32. local si=$IFS
  33. compadd -- $(COMP_CWORD=$((CURRENT-1)) \
  34. COMP_LINE=$BUFFER \
  35. COMP_POINT=0 \
  36. npm completion -- "${words[@]}" \
  37. 2>/dev/null)
  38. IFS=$si
  39. }
  40. compdef _npm_completion npm
  41. elif type compctl &>/dev/null; then
  42. _npm_completion () {
  43. local cword line point words si
  44. read -Ac words
  45. read -cn cword
  46. let cword-=1
  47. read -l line
  48. read -ln point
  49. si="$IFS"
  50. IFS=$'\n' reply=($(COMP_CWORD="$cword" \
  51. COMP_LINE="$line" \
  52. COMP_POINT="$point" \
  53. npm completion -- "${words[@]}" \
  54. 2>/dev/null)) || return $?
  55. IFS="$si"
  56. }
  57. compctl -K _npm_completion npm
  58. fi
  59. ###-end-npm-completion-###