aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorPeter Xu <peterx@redhat.com>2016-03-14 14:49:32 +0800
committerSteven Rostedt <rostedt@goodmis.org>2016-03-21 16:04:48 -0400
commit797db1e341ce7d85a41ead4cf632a28235cb8a69 (patch)
treed4f72eb0fc7a55e9722b11811e3b109b376bbfa0
parent7477e7057e201466b2fa801f7a47ff5976cb06ab (diff)
downloadtrace-cmd-797db1e341ce7d85a41ead4cf632a28235cb8a69.tar.gz
trace-cmd: Add bash completion for "-{e|p|l|n|g}"
"-e", "-p" and "-{l|n|g}" are frequently used parameters in many sub-commands, adding auto-completion support for them. It may be very slow when triggering "-{l|n|g}" auto completion. However, it's better than nothing. Signed-off-by: Peter Xu <peterx@redhat.com> Signed-off-by: Steven Rostedt <rostedt@goodmis.org>
-rw-r--r--trace-cmd.bash55
1 files changed, 46 insertions, 9 deletions
diff --git a/trace-cmd.bash b/trace-cmd.bash
index e7e4673d..3b76af2d 100644
--- a/trace-cmd.bash
+++ b/trace-cmd.bash
@@ -1,13 +1,50 @@
_trace_cmd_complete()
{
- local sub_cmd_list
- sub_cmd_list=$(trace-cmd --help 2>/dev/null | \
- grep " - " | sed 's/^ *//; s/ -.*//')
- COMPREPLY=()
- prev="${COMP_WORDS[COMP_CWORD-1]}"
- cur="${COMP_WORDS[COMP_CWORD]}"
- if [[ "$prev" == "trace-cmd" ]]; then
- COMPREPLY=( $(compgen -W "${sub_cmd_list}" -- ${cur} ))
- fi
+ local cur=""
+ local prev=""
+
+ # Not to use COMP_WORDS to avoid buggy behavior of Bash when
+ # handling with words including ":", like:
+ #
+ # prev="${COMP_WORDS[COMP_CWORD-1]}"
+ # cur="${COMP_WORDS[COMP_CWORD]}"
+ #
+ # Instead, we use _get_comp_words_by_ref() magic.
+ _get_comp_words_by_ref -n : cur prev
+
+ case "$prev" in
+ trace-cmd)
+ local cmds=$(trace-cmd --help 2>/dev/null | \
+ grep " - " | sed 's/^ *//; s/ -.*//')
+ COMPREPLY=( $(compgen -W "${cmds}" -- "${cur}") )
+ ;;
+ -e)
+ local events=$(trace-cmd list -e)
+ local prefix=${cur%%:*}
+
+ COMPREPLY=( $(compgen -W "${events}" -- "${cur}") )
+
+ # This is still to handle the "*:*" special case
+ if [[ -n "$prefix" ]]; then
+ local reply_n=${#COMPREPLY[*]}
+ for (( i = 0; i < $reply_n; i++)); do
+ COMPREPLY[$i]=${COMPREPLY[i]##${prefix}:}
+ done
+ fi
+ ;;
+ -p)
+ local plugins=$(trace-cmd list -p)
+ COMPREPLY=( $(compgen -W "${plugins}" -- "${cur}") )
+ ;;
+ -l|-n|-g)
+ # This is extremely slow still (may take >1sec).
+ local funcs=$(trace-cmd list -f | sed 's/ .*//')
+ COMPREPLY=( $(compgen -W "${funcs}" -- "${cur}") )
+ ;;
+ *)
+ # By default, we do not provide any hints.
+ COMPREPLY=()
+ ;;
+ esac
}
complete -F _trace_cmd_complete trace-cmd