django_bash_completion 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. # #############################################################################
  2. # This bash script adds tab-completion feature to django-admin and manage.py.
  3. #
  4. # Testing it out without installing
  5. # =================================
  6. #
  7. # To test out the completion without "installing" this, just run this file
  8. # directly, like so:
  9. #
  10. # . ~/path/to/django_bash_completion
  11. #
  12. # Note: There's a dot ('.') at the beginning of that command.
  13. #
  14. # After you do that, tab completion will immediately be made available in your
  15. # current Bash shell. But it won't be available next time you log in.
  16. #
  17. # Installing
  18. # ==========
  19. #
  20. # To install this, point to this file from your .bash_profile, like so:
  21. #
  22. # . ~/path/to/django_bash_completion
  23. #
  24. # Do the same in your .bashrc if .bashrc doesn't invoke .bash_profile.
  25. #
  26. # Settings will take effect the next time you log in.
  27. #
  28. # Uninstalling
  29. # ============
  30. #
  31. # To uninstall, just remove the line from your .bash_profile and .bashrc.
  32. _django_completion()
  33. {
  34. COMPREPLY=( $( COMP_WORDS="${COMP_WORDS[*]}" \
  35. COMP_CWORD=$COMP_CWORD \
  36. DJANGO_AUTO_COMPLETE=1 $1 ) )
  37. }
  38. complete -F _django_completion -o default manage.py django-admin
  39. _python_django_completion()
  40. {
  41. if [[ ${COMP_CWORD} -ge 2 ]]; then
  42. local PYTHON_EXE=${COMP_WORDS[0]##*/}
  43. if echo "$PYTHON_EXE" | grep -qE "python([3-9]\.[0-9])?"; then
  44. local PYTHON_SCRIPT=${COMP_WORDS[1]##*/}
  45. if echo "$PYTHON_SCRIPT" | grep -qE "manage\.py|django-admin"; then
  46. COMPREPLY=( $( COMP_WORDS=( "${COMP_WORDS[*]:1}" )
  47. COMP_CWORD=$(( COMP_CWORD-1 ))
  48. DJANGO_AUTO_COMPLETE=1 ${COMP_WORDS[*]} ) )
  49. fi
  50. fi
  51. fi
  52. }
  53. # Support for multiple interpreters.
  54. unset pythons
  55. if command -v whereis &>/dev/null; then
  56. python_interpreters=$(whereis python | cut -d " " -f 2-)
  57. for python in $python_interpreters; do
  58. [[ $python != *-config ]] && pythons="${pythons} ${python##*/}"
  59. done
  60. unset python_interpreters
  61. pythons=$(echo "$pythons" | tr " " "\n" | sort -u | tr "\n" " ")
  62. else
  63. pythons=python
  64. fi
  65. complete -F _python_django_completion -o default $pythons
  66. unset pythons