gogo.compl 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. # gogo completion file
  2. # source it to use it.
  3. _gogoAliases()
  4. {
  5. cur=$1
  6. CONF_FILE="${HOME}/.config/gogo/gogo.conf"
  7. # read aliases from $CONF_FILE
  8. if [[ -f ${CONF_FILE} ]]; then
  9. IFS=' ='
  10. CONFALIASES=
  11. while read -r confalias directory
  12. do
  13. # Recognise comments
  14. if [[ ${confalias} =~ '^#' ]]; then
  15. continue
  16. fi
  17. # When <tab> is pressed when full alias is typed in, completion will add "/" to it. This
  18. # way path completion will be enabled.
  19. if [[ ${cur} != "" && (${cur} == ${confalias}) ]]; then
  20. COMPREPLY=( $(compgen -W "${confalias}/" -- ${cur}) )
  21. return
  22. fi
  23. # Path completion
  24. # Basically changes alias to real directory name, fetches all directories inside it and
  25. # changes real directory name back to alias.
  26. if [[ ${cur} == ${confalias}/* ]]; then
  27. unset IFS
  28. if [[ ${directory} != *://* ]]; then
  29. # Use this instead of $directory which may contain a tilde interpreted as string
  30. # literal (eval enforces '~' expansion)
  31. eval realdirectory=${directory}
  32. # Replace the first occurence of alias in cur with a real directory
  33. currealdir=${cur/${confalias}/${realdirectory}}
  34. # List directories matching an expression
  35. compdirs="$(echo ${currealdir}*/)"
  36. # compdirs contain this silly string (like /home/user/*/) when there's no
  37. # directory inside
  38. if [[ ${compdirs} != "${currealdir}*/" ]]; then
  39. aliasdirs=${compdirs//${realdirectory}/${confalias}}
  40. COMPREPLY=( $(compgen -W "${aliasdirs}" -- ${cur}) )
  41. fi
  42. fi
  43. return
  44. fi
  45. # don't display the default target
  46. if [[ ${confalias} == "default" ]]; then
  47. continue
  48. fi
  49. CONFALIASES="${CONFALIASES} ${confalias}"
  50. done < ${CONF_FILE}
  51. unset IFS
  52. COMPREPLY=( $(compgen -W "${CONFALIASES}" -- ${cur}) )
  53. fi
  54. }
  55. _gogo()
  56. {
  57. local cur prev opts
  58. COMPREPLY=()
  59. cur="${COMP_WORDS[COMP_CWORD]}"
  60. prev="${COMP_WORDS[COMP_CWORD-1]}"
  61. if [[ ${COMP_CWORD} -ge 2 ]]; then
  62. return 0
  63. fi
  64. opts="-a -l --ls -e --edit -h --help -v --version"
  65. case "${cur}" in
  66. -*)
  67. COMPREPLY=( $(compgen -W "${opts}" -- ${cur}) )
  68. ;;
  69. *)
  70. _gogoAliases ${cur}
  71. ;;
  72. esac
  73. }
  74. complete -o nospace -F _gogo gogo