completion.sh 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. #!/usr/bin/env bash
  2. ###-begin-{{cmd}}-completion-###
  3. #
  4. # {{cmd}} command completion script
  5. #
  6. # Installation: {{cmd}} completion >> ~/.bashrc (or ~/.zshrc)
  7. # Or, maybe: {{cmd}} completion > /usr/local/etc/bash_completion.d/{{cmd}}
  8. #
  9. COMP_WORDBREAKS=${COMP_WORDBREAKS/=/}
  10. COMP_WORDBREAKS=${COMP_WORDBREAKS/@/}
  11. export COMP_WORDBREAKS
  12. if complete &>/dev/null; then
  13. _{{cmd}}_completion () {
  14. local si="$IFS"
  15. IFS=$'\n' COMPREPLY=($(COMP_CWORD="$COMP_CWORD" \
  16. COMP_LINE="$COMP_LINE" \
  17. COMP_POINT="$COMP_POINT" \
  18. {{cmd}} completion -- "${COMP_WORDS[@]}" \
  19. 2>/dev/null)) || return $?
  20. IFS="$si"
  21. }
  22. complete -F _{{cmd}}_completion {{cmd}}
  23. elif compctl &>/dev/null; then
  24. _{{cmd}}_completion () {
  25. local cword line point words si
  26. read -Ac words
  27. read -cn cword
  28. let cword-=1
  29. read -l line
  30. read -ln point
  31. si="$IFS"
  32. IFS=$'\n' reply=($(COMP_CWORD="$cword" \
  33. COMP_LINE="$line" \
  34. COMP_POINT="$point" \
  35. {{cmd}} completion -- "${words[@]}" \
  36. 2>/dev/null)) || return $?
  37. IFS="$si"
  38. }
  39. compctl -K _{{cmd}}_completion {{cmd}}
  40. fi
  41. ###-end-{{cmd}}-completion-###