From 56ae84c2dbc56ed492d4c6edc2410cb981ee85e0 Mon Sep 17 00:00:00 2001 From: sharder996 Date: Tue, 4 Jul 2023 13:02:53 -0700 Subject: [PATCH 1/9] [bash] remove duplicates from completions --- completions/bash/multipass | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/completions/bash/multipass b/completions/bash/multipass index fe5aa32716..85c2e75a3e 100644 --- a/completions/bash/multipass +++ b/completions/bash/multipass @@ -388,7 +388,12 @@ _multipass_complete() fi if [[ -n "${opts}" ]]; then - COMPREPLY=( $(compgen -W "${opts}" -- ${cur}) ) + for word in "${COMP_WORDS[@]}"; do + if [[ " ${opts[@]} " =~ " $word " ]]; then + opts=("${opts[@]/$word}") + fi + done + COMPREPLY=( $(compgen -W "${opts[*]}" -- ${cur}) ) fi return 0 From 3564d2bd63d62fa6c17478acdb6b13c4d31e8204 Mon Sep 17 00:00:00 2001 From: sharder996 Date: Tue, 12 Sep 2023 14:41:45 -0500 Subject: [PATCH 2/9] [bash] compare COMP_WORDS against COMP_LINE instead of against opts --- completions/bash/multipass | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/completions/bash/multipass b/completions/bash/multipass index 85c2e75a3e..f393e5b8e7 100644 --- a/completions/bash/multipass +++ b/completions/bash/multipass @@ -389,7 +389,7 @@ _multipass_complete() if [[ -n "${opts}" ]]; then for word in "${COMP_WORDS[@]}"; do - if [[ " ${opts[@]} " =~ " $word " ]]; then + if [[ "${COMP_LINE}" == *"${word} "* ]]; then opts=("${opts[@]/$word}") fi done From 2c1caba9a5d8fbc197f973c999faf16c93f2f561 Mon Sep 17 00:00:00 2001 From: sharder996 Date: Fri, 29 Sep 2023 11:38:25 -0500 Subject: [PATCH 3/9] [bash] revert previous attempt at filtering --- completions/bash/multipass | 7 +------ 1 file changed, 1 insertion(+), 6 deletions(-) diff --git a/completions/bash/multipass b/completions/bash/multipass index f393e5b8e7..fe5aa32716 100644 --- a/completions/bash/multipass +++ b/completions/bash/multipass @@ -388,12 +388,7 @@ _multipass_complete() fi if [[ -n "${opts}" ]]; then - for word in "${COMP_WORDS[@]}"; do - if [[ "${COMP_LINE}" == *"${word} "* ]]; then - opts=("${opts[@]/$word}") - fi - done - COMPREPLY=( $(compgen -W "${opts[*]}" -- ${cur}) ) + COMPREPLY=( $(compgen -W "${opts}" -- ${cur}) ) fi return 0 From a8cd4b6171c44b6f309087ed43f4b5b8cc0e1a0e Mon Sep 17 00:00:00 2001 From: sharder996 Date: Fri, 29 Sep 2023 12:28:07 -0500 Subject: [PATCH 4/9] [bash] factor out common code into separate function --- completions/bash/multipass | 64 ++++++++++++++------------------------ 1 file changed, 23 insertions(+), 41 deletions(-) diff --git a/completions/bash/multipass b/completions/bash/multipass index fe5aa32716..9352949977 100644 --- a/completions/bash/multipass +++ b/completions/bash/multipass @@ -23,21 +23,7 @@ _multipass_complete() local instances=$( \eval $cmd | \tail -n +2 | \cut -d',' -f 1 ) - local found - - _get_comp_words_by_ref -n := -w WORDS -i CWORD cur prev - for instance in $instances; do - found=0 - for ((i=2; i Date: Fri, 29 Sep 2023 12:28:58 -0500 Subject: [PATCH 5/9] [bash] use filtering function to add options for commands based on whether or not they allow duplicates --- completions/bash/multipass | 28 ++++++++++++---------------- 1 file changed, 12 insertions(+), 16 deletions(-) diff --git a/completions/bash/multipass b/completions/bash/multipass index 9352949977..5888706073 100644 --- a/completions/bash/multipass +++ b/completions/bash/multipass @@ -198,7 +198,8 @@ _multipass_complete() if [[ "${multipass_cmds}" =~ " ${cmd} " || "${multipass_cmds}" =~ ^${cmd} || "${multipass_cmds}" =~ \ ${cmd}$ ]]; then - opts="--help --verbose" + _add_nonrepeating_args "--help" + opts="${opts} --verbose" fi case "${cmd}" in @@ -206,41 +207,36 @@ _multipass_complete() opts="${opts} --working-directory --no-map-working-directory" ;; "info") - opts="${opts} --all --format" + _add_nonrepeating_args "--all --format" ;; - "list"|"ls") - opts="${opts} --format" - ;; - "networks") - opts="${opts} --format" + "list"|"ls"|"networks"|"aliases") + _add_nonrepeating_args "--format" ;; "delete") - opts="${opts} --all --purge" + _add_nonrepeating_args "--all --purge" ;; "launch") - opts="${opts} --cpus --disk --memory --name --cloud-init --network --bridged --mount" + _add_nonrepeating_args "--cpus --disk --memory --name --cloud-init --bridged --mount" + opts="${opts} --network --mount" ;; "mount") opts="${opts} --gid-map --uid-map" ;; "recover"|"start"|"suspend"|"restart") - opts="${opts} --all" + _add_nonrepeating_args "--all" ;; "stop") - opts="${opts} --all --cancel --time" + _add_nonrepeating_args "--all --cancel --time" ;; "find") - opts="${opts} --show-unsupported --force-update --format" - ;; - "aliases") - opts="${opts} --format" + _add_nonrepeating_args "--show-unsupported --force-update --format" ;; "unalias") _multipass_aliases _add_nonrepeating_args $multipass_aliases ;; "transfer"|"copy-files") - opts="${opts} --parents --recursive" + _add_nonrepeating_args "--parents --recursive" ;; esac From 6f56810eeac0443e6924b85dd8371525c9dcab51 Mon Sep 17 00:00:00 2001 From: sharder996 Date: Tue, 3 Oct 2023 12:41:26 -0500 Subject: [PATCH 6/9] [bash] format newline separated list of instances with space separated list --- completions/bash/multipass | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/completions/bash/multipass b/completions/bash/multipass index 5888706073..9b75ee2e60 100644 --- a/completions/bash/multipass +++ b/completions/bash/multipass @@ -21,9 +21,9 @@ _multipass_complete() local cmd="multipass list --format=csv --no-ipv4" [ -n "$state" ] && cmd="$cmd | \awk -F',' '\$2 == \"$state\"'" - local instances=$( \eval $cmd | \tail -n +2 | \cut -d',' -f 1 ) + local instances=$( \eval $cmd | \tail -n +2 | \cut -d',' -f 1 | \tr '\r\n' ' ') - _add_nonrepeating_args $instances + _add_nonrepeating_args "$instances" } # Set $opts to the list of available networks. From 3f28037af4fcedcbb36116ab42d6a058a61385d1 Mon Sep 17 00:00:00 2001 From: sharder996 Date: Tue, 3 Oct 2023 12:44:27 -0500 Subject: [PATCH 7/9] [bash] command line checking with function call to filter args --- completions/bash/multipass | 12 ++---------- 1 file changed, 2 insertions(+), 10 deletions(-) diff --git a/completions/bash/multipass b/completions/bash/multipass index 9b75ee2e60..6a0448f647 100644 --- a/completions/bash/multipass +++ b/completions/bash/multipass @@ -56,22 +56,14 @@ _multipass_complete() { opts="" - local no_map_found=0 - local help_found=0 + _add_nonrepeating_args "--no-map-working-directory --help --verbose" + local definition_found=0 - local verbose_found=0 for ((i=2; i Date: Tue, 3 Oct 2023 15:31:09 -0500 Subject: [PATCH 8/9] [bash] stop updating `prev` --- completions/bash/multipass | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/completions/bash/multipass b/completions/bash/multipass index 6a0448f647..deae11f4e7 100644 --- a/completions/bash/multipass +++ b/completions/bash/multipass @@ -151,7 +151,7 @@ _multipass_complete() { local array=$1 local found - _get_comp_words_by_ref -n := -w WORDS -i CWORD cur prev + for item in $array; do found=0 for ((i=2; i Date: Fri, 13 Oct 2023 08:33:32 -0500 Subject: [PATCH 9/9] [bash] cut out header before filtering on instance state --- completions/bash/multipass | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/completions/bash/multipass b/completions/bash/multipass index deae11f4e7..6e9aa70e08 100644 --- a/completions/bash/multipass +++ b/completions/bash/multipass @@ -19,9 +19,9 @@ _multipass_complete() local state=$1 local cmd="multipass list --format=csv --no-ipv4" - [ -n "$state" ] && cmd="$cmd | \awk -F',' '\$2 == \"$state\"'" + [ -n "$state" ] && cmd="$cmd | \tail -n +2 | \awk -F',' '\$2 == \"$state\"'" - local instances=$( \eval $cmd | \tail -n +2 | \cut -d',' -f 1 | \tr '\r\n' ' ') + local instances=$( \eval $cmd | \cut -d',' -f 1 | \tr '\r\n' ' ') _add_nonrepeating_args "$instances" }