From d5a5dc62737084e263f9ca629d4a1744f702007b Mon Sep 17 00:00:00 2001 From: Alberto Sartori Date: Tue, 29 Nov 2022 09:56:27 +0100 Subject: bash completion: to avoid code duplication, implement tab-completion for just and just-mr within only one file Co-authored-by: Oliver Reiche --- share/just-mr_complete.bash | 77 --------------------------------------------- share/just_complete.bash | 72 ++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 72 insertions(+), 77 deletions(-) delete mode 100644 share/just-mr_complete.bash diff --git a/share/just-mr_complete.bash b/share/just-mr_complete.bash deleted file mode 100644 index 4fc69f33..00000000 --- a/share/just-mr_complete.bash +++ /dev/null @@ -1,77 +0,0 @@ -_just-mr_options(){ - local cmd=$1 - for w in $($cmd --help 2>/dev/null) - do - [[ $w =~ ^-. ]] && printf "%s\n" ${w//,/" "} - done -} - -_just-mr_parse_subcommand() { - local readonly FLAGS=("--help\n-h\n--norc\ndo") # treat 'do' as flag - local readonly OPTIONS=("--distdir\n--just\n--local-build-root\n--main\n--rc\n-C\n-L") - shift - while [ -n "$1" ]; do - if echo -e "$FLAGS" | grep -q -- "^$1$"; then shift; continue; fi - if echo -e "$OPTIONS" | grep -q -- "^$1$"; then shift; shift; continue; fi - if [ "$1" = "--" ]; then shift; fi - break - done - echo "$1" -} - -_just-mr_repos(){ - command -v python3 &>/dev/null || return - local CONF=$(just-mr setup --all 2>/dev/null) - if [ ! -f "$CONF" ]; then return; fi - python3 - </dev/null)) - COMPREPLY=($(compgen -f -W "${_opts[*]} ${_targets[*]}" -- $word )) - compopt -o plusdirs -o bashdefault -o default - else - # just-mr top-level options - local _opts=($(_just-mr_options "just-mr")) - COMPREPLY=($(compgen -W "${_opts[*]} ${SUBCOMMANDS[*]}" -- $word)) - fi -} - -complete -F _just-mr_completion just-mr diff --git a/share/just_complete.bash b/share/just_complete.bash index 41f8f5aa..c62d446a 100644 --- a/share/just_complete.bash +++ b/share/just_complete.bash @@ -55,3 +55,75 @@ _just_completion(){ } complete -F _just_completion just +_just-mr_options(){ + local cmd=$1 + for w in $($cmd --help 2>/dev/null) + do + [[ $w =~ ^-. ]] && printf "%s\n" ${w//,/" "} + done +} + +_just-mr_parse_subcommand() { + local readonly FLAGS=("--help\n-h\n--norc\ndo") # treat 'do' as flag + local readonly OPTIONS=("--distdir\n--just\n--local-build-root\n--main\n--rc\n-C\n-L") + shift + while [ -n "$1" ]; do + if echo -e "$FLAGS" | grep -q -- "^$1$"; then shift; continue; fi + if echo -e "$OPTIONS" | grep -q -- "^$1$"; then shift; shift; continue; fi + if [ "$1" = "--" ]; then shift; fi + break + done + echo "$1" +} + +_just-mr_repos(){ + command -v python3 &>/dev/null || return + local CONF=$(just-mr setup --all 2>/dev/null) + if [ ! -f "$CONF" ]; then return; fi + python3 - <