From ce66a7c4e0ea73fc621f3792825b4354c1bc5a06 Mon Sep 17 00:00:00 2001 From: Eloy Briceno <51831786+Voldivh@users.noreply.github.com> Date: Tue, 31 Oct 2023 18:21:06 -0500 Subject: [PATCH] Adds the subcommands for the log command (#451) Signed-off-by: Voldivh --- src/cmd/transport.bash_completion.sh | 105 ++++++++++++++++++++++----- 1 file changed, 88 insertions(+), 17 deletions(-) diff --git a/src/cmd/transport.bash_completion.sh b/src/cmd/transport.bash_completion.sh index b77afe3be..f7706f0db 100644 --- a/src/cmd/transport.bash_completion.sh +++ b/src/cmd/transport.bash_completion.sh @@ -20,6 +20,34 @@ # This is a per-library function definition, used in conjunction with the # top-level entry point in ign-tools. +GZ_LOG_SUBCOMMANDS=" +record +playback +" + +GZ_LOG_COMPLETION_LIST=" + -h --help + -v --verbose +" + +GZ_PLAYBACK_COMPLETION_LIST=" + -h --help + -v --verbose + --file + --pattern + --remap + --wait + -f +" + +GZ_RECORD_COMPLETION_LIST=" + -h --help + -v --verbose + --file + --force + --pattern +" + # TODO: In Fortress+, remove --force-version and --versions. Add --help-all. # Update ../gz_TEST.cc accordingly. GZ_SERVICE_COMPLETION_LIST=" @@ -36,8 +64,6 @@ GZ_SERVICE_COMPLETION_LIST=" --versions " -# TODO: In Fortress+, remove --force-version and --versions. Add -# `-v --version` and --json-output. Update ../gz_TEST.cc accordingly. GZ_TOPIC_COMPLETION_LIST=" -h --help -v --version @@ -53,11 +79,10 @@ GZ_TOPIC_COMPLETION_LIST=" --versions " -function _gz_service -{ +function __get_comp_from_list { if [[ ${COMP_WORDS[COMP_CWORD]} == -* ]]; then # Specify options (-*) word list for this subcommand - COMPREPLY=($(compgen -W "$GZ_SERVICE_COMPLETION_LIST" \ + COMPREPLY=($(compgen -W "$@" \ -- "${COMP_WORDS[COMP_CWORD]}" )) return else @@ -69,29 +94,75 @@ function _gz_service fi } -function _gz_service_flags +function _gz_log_playback { - for word in $GZ_SERVICE_COMPLETION_LIST; do - echo "$word" - done + __get_comp_from_list "$GZ_PLAYBACK_COMPLETION_LIST" +} + +function _gz_log_record +{ + __get_comp_from_list "$GZ_RECORD_COMPLETION_LIST" +} + +function _gz_service +{ + __get_comp_from_list "$GZ_SERVICE_COMPLETION_LIST" } function _gz_topic { + __get_comp_from_list "$GZ_TOPIC_COMPLETION_LIST" +} + +# This searches the current list of typed words for one of the subcommands +# listed in GZ_LOG_SUBCOMMANDS. This should work for most cases, but may fail +# if a word that looks like a subcommand is used as an argument to a flag. +function __get_subcommand +{ + local known_subcmd + local subcmd + for ((i=2; $i<=$COMP_CWORD; i++)); do + for subcmd in $GZ_LOG_SUBCOMMANDS; do + if [[ "${COMP_WORDS[i]}" == "$subcmd" ]]; then + known_subcmd="$subcmd" + fi + done + done + echo "$known_subcmd" +} + +function _gz_log +{ + if [[ $COMP_CWORD > 2 ]]; then + local known_subcmd=$(__get_subcommand) + if [[ ! -z $known_subcmd ]]; then + local subcmd_func="_gz_log_$known_subcmd" + if [[ "$(type -t $subcmd_func)" == 'function' ]]; then + $subcmd_func + return + fi + fi + fi + + # If a subcommand is not found, assume we're still completing the subcommands + # or flags for `log`. if [[ ${COMP_WORDS[COMP_CWORD]} == -* ]]; then - # Specify options (-*) word list for this subcommand - COMPREPLY=($(compgen -W "$GZ_TOPIC_COMPLETION_LIST" \ + COMPREPLY=($(compgen -W "$GZ_LOG_COMPLETION_LIST" \ -- "${COMP_WORDS[COMP_CWORD]}" )) - return else - # Just use bash default auto-complete, because we never have two - # subcommands in the same line. If that is ever needed, change here to - # detect subsequent subcommands - COMPREPLY=($(compgen -o default -- "${COMP_WORDS[COMP_CWORD]}")) - return + COMPREPLY=($(compgen -W "${GZ_LOG_SUBCOMMANDS}" -- ${cur})) fi } + +function _gz_service_flags +{ + for word in $GZ_SERVICE_COMPLETION_LIST; do + echo "$word" + done +} + + function _gz_topic_flags { for word in $GZ_TOPIC_COMPLETION_LIST; do