bash_completion 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. # perf completion
  2. # Taken from git.git's completion script.
  3. __my_reassemble_comp_words_by_ref()
  4. {
  5. local exclude i j first
  6. # Which word separators to exclude?
  7. exclude="${1//[^$COMP_WORDBREAKS]}"
  8. cword_=$COMP_CWORD
  9. if [ -z "$exclude" ]; then
  10. words_=("${COMP_WORDS[@]}")
  11. return
  12. fi
  13. # List of word completion separators has shrunk;
  14. # re-assemble words to complete.
  15. for ((i=0, j=0; i < ${#COMP_WORDS[@]}; i++, j++)); do
  16. # Append each nonempty word consisting of just
  17. # word separator characters to the current word.
  18. first=t
  19. while
  20. [ $i -gt 0 ] &&
  21. [ -n "${COMP_WORDS[$i]}" ] &&
  22. # word consists of excluded word separators
  23. [ "${COMP_WORDS[$i]//[^$exclude]}" = "${COMP_WORDS[$i]}" ]
  24. do
  25. # Attach to the previous token,
  26. # unless the previous token is the command name.
  27. if [ $j -ge 2 ] && [ -n "$first" ]; then
  28. ((j--))
  29. fi
  30. first=
  31. words_[$j]=${words_[j]}${COMP_WORDS[i]}
  32. if [ $i = $COMP_CWORD ]; then
  33. cword_=$j
  34. fi
  35. if (($i < ${#COMP_WORDS[@]} - 1)); then
  36. ((i++))
  37. else
  38. # Done.
  39. return
  40. fi
  41. done
  42. words_[$j]=${words_[j]}${COMP_WORDS[i]}
  43. if [ $i = $COMP_CWORD ]; then
  44. cword_=$j
  45. fi
  46. done
  47. }
  48. type _get_comp_words_by_ref &>/dev/null ||
  49. _get_comp_words_by_ref()
  50. {
  51. local exclude cur_ words_ cword_
  52. if [ "$1" = "-n" ]; then
  53. exclude=$2
  54. shift 2
  55. fi
  56. __my_reassemble_comp_words_by_ref "$exclude"
  57. cur_=${words_[cword_]}
  58. while [ $# -gt 0 ]; do
  59. case "$1" in
  60. cur)
  61. cur=$cur_
  62. ;;
  63. prev)
  64. prev=${words_[$cword_-1]}
  65. ;;
  66. words)
  67. words=("${words_[@]}")
  68. ;;
  69. cword)
  70. cword=$cword_
  71. ;;
  72. esac
  73. shift
  74. done
  75. }
  76. type __ltrim_colon_completions &>/dev/null ||
  77. __ltrim_colon_completions()
  78. {
  79. if [[ "$1" == *:* && "$COMP_WORDBREAKS" == *:* ]]; then
  80. # Remove colon-word prefix from COMPREPLY items
  81. local colon_word=${1%"${1##*:}"}
  82. local i=${#COMPREPLY[*]}
  83. while [[ $((--i)) -ge 0 ]]; do
  84. COMPREPLY[$i]=${COMPREPLY[$i]#"$colon_word"}
  85. done
  86. fi
  87. }
  88. type perf &>/dev/null &&
  89. _perf()
  90. {
  91. local cur words cword prev cmd
  92. COMPREPLY=()
  93. _get_comp_words_by_ref -n =: cur words cword prev
  94. cmd=${words[0]}
  95. # List perf subcommands or long options
  96. if [ $cword -eq 1 ]; then
  97. if [[ $cur == --* ]]; then
  98. COMPREPLY=( $( compgen -W '--help --version \
  99. --exec-path --html-path --paginate --no-pager \
  100. --perf-dir --work-tree --debugfs-dir' -- "$cur" ) )
  101. else
  102. cmds=$($cmd --list-cmds)
  103. COMPREPLY=( $( compgen -W '$cmds' -- "$cur" ) )
  104. fi
  105. # List possible events for -e option
  106. elif [[ $prev == "-e" && "${words[1]}" == @(record|stat|top) ]]; then
  107. evts=$($cmd list --raw-dump)
  108. COMPREPLY=( $( compgen -W '$evts' -- "$cur" ) )
  109. __ltrim_colon_completions $cur
  110. # List long option names
  111. elif [[ $cur == --* ]]; then
  112. subcmd=${words[1]}
  113. opts=$($cmd $subcmd --list-opts)
  114. COMPREPLY=( $( compgen -W '$opts' -- "$cur" ) )
  115. fi
  116. } &&
  117. complete -o bashdefault -o default -o nospace -F _perf perf 2>/dev/null \
  118. || complete -o default -o nospace -F _perf perf