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