lexicon 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. _lexicon_complete_subcommand ()
  2. {
  3. local command="${COMP_WORDS[1]}"
  4. local subcommands="$(${COMP_WORDS[0]} $command --help 2>/dev/null \
  5. | grep '^ -' \
  6. | awk '{ print $1 }' \
  7. | sed 's/,//g')"
  8. subcommands+="$(${COMP_WORDS[0]} $command --help 2>/dev/null \
  9. | sed -n 's/^ {//p;' \
  10. | sed 's/}//;' \
  11. | sed 's/,/\n/g' \
  12. | sed 's/\r$//g')"
  13. COMPREPLY=($(compgen -W "$subcommands" -- "${COMP_WORDS[$COMP_CWORD]}"))
  14. }
  15. _lexicon_is_command ()
  16. {
  17. local word
  18. local result="false"
  19. for word in ${COMP_WORDS[*]}t
  20. do
  21. if [ "$word" = "$1" ]
  22. then
  23. result=true
  24. break
  25. fi
  26. done
  27. $result
  28. }
  29. _lexicon_complete()
  30. {
  31. local word_count=${#COMP_WORDS[*]}
  32. local word_at_point="${COMP_WORDS[$COMP_CWORD]}"
  33. case $COMP_CWORD in
  34. 1)
  35. if [ -z "$_lexicon_subcommands" ]
  36. then
  37. # Cache the list of subcommands to speed things up.
  38. _lexicon_subcommands="$(lexicon --help 2>/dev/null | grep '^ [a-z]' | awk '{ print $1 }')"
  39. fi
  40. COMPREPLY=($(compgen -W "$_lexicon_subcommands" -- "$word_at_point"))
  41. ;;
  42. *)
  43. case $COMP_CWORD in
  44. 2) _lexicon_complete_subcommand;;
  45. esac
  46. ;;
  47. esac
  48. }
  49. complete -F _lexicon_complete lexicon