Repository: rsteube/carapace
Pull request #433 (2022-02-21 11:19:29)

bash-ble: fallback to bash if not attached
#431 


Patch:
diff --git a/internal/bash_ble/snippet.go b/internal/bash_ble/snippet.go
index f3d34cd..3571b1b 100644
--- a/internal/bash_ble/snippet.go
+++ b/internal/bash_ble/snippet.go
@@ -3,31 +3,40 @@ package bash_ble
 
 import (
 	"fmt"
+	"regexp"
 
+	"github.com/rsteube/carapace/internal/bash"
 	"github.com/rsteube/carapace/internal/uid"
 	"github.com/spf13/cobra"
 )
 
 // Snippet creates the bash-ble completion script
 func Snippet(cmd *cobra.Command) string {
-	result := fmt.Sprintf(`#!/bin/bash
-_%v_completion() {
-  bleopt complete_menu_style=desc
-  #export COMP_WORDBREAKS
-
-  local compline="${COMP_LINE:0:${COMP_POINT}}"
-  local IFS=$'\n'
-  local c
-  mapfile -t c < <(echo "$compline" | sed -e "s/ \$/ ''/" -e 's/"/\"/g' | xargs %v _carapace bash-ble)
-  [[ "${COMPREPLY[*]}" == "" ]] && COMPREPLY=() # fix for mapfile creating a non-empty array from empty command output
-
-  for cand in "${c[@]}"; do
-    [ ! -z "$cand" ] && ble/complete/cand/yield mandb "${cand%%$'\t'*}" "${cand##*$'\t'}"
-  done
+	bashSnippet := bash.Snippet(cmd)
+	bashSnippet = regexp.MustCompile("complete -F [^\n]+").ReplaceAllString(bashSnippet, "")
+
+	result := fmt.Sprintf(`
+_%v_completion_ble() {
+  if [[ ${BLE_ATTACHED-} ]]; then
+    bleopt complete_menu_style=desc
+
+    local compline="${COMP_LINE:0:${COMP_POINT}}"
+    local IFS=$'\n'
+    local c
+    mapfile -t c < <(echo "$compline" | sed -e "s/ \$/ ''/" -e 's/"/\"/g' | xargs %v _carapace bash-ble)
+    [[ "${c[*]}" == "" ]] && c=() # fix for mapfile creating a non-empty array from empty command output
+
+    local cand
+    for cand in "${c[@]}"; do
+      [ ! -z "$cand" ] && ble/complete/cand/yield mandb "${cand%%$'\t'*}" "${cand##*$'\t'}"
+    done
+  else
+    complete -F _%v_completion %v
+  fi
 }
 
-complete -F _%v_completion %v
-`, cmd.Name(), uid.Executable(), cmd.Name(), cmd.Name())
+complete -F _%v_completion_ble %v
+`, cmd.Name(), uid.Executable(), cmd.Name(), cmd.Name(), cmd.Name(), cmd.Name())
 
-	return result
+	return bashSnippet + result
 }


Test patch:
diff --git a/example/cmd/_test/bash-ble.sh b/example/cmd/_test/bash-ble.sh
new file mode 100644
index 0000000..3e32497
--- /dev/null
+++ b/example/cmd/_test/bash-ble.sh
@@ -0,0 +1,38 @@
+#!/bin/bash
+_example_completion() {
+  export COMP_WORDBREAKS
+
+  local compline="${COMP_LINE:0:${COMP_POINT}}"
+  local IFS=$'\n'
+  mapfile -t COMPREPLY < <(echo "$compline" | sed -e "s/ \$/ ''/" -e 's/"/\"/g' | xargs example _carapace bash)
+  [[ "${COMPREPLY[*]}" == "" ]] && COMPREPLY=() # fix for mapfile creating a non-empty array from empty command output
+
+  [[ ${COMPREPLY[0]} == *[/=@:.,$'\001'] ]] && compopt -o nospace
+  # TODO use mapfile
+  # shellcheck disable=SC2206
+  [[ ${#COMPREPLY[@]} -eq 1 ]] && COMPREPLY=(${COMPREPLY%$'\001'})
+}
+
+
+
+_example_completion_ble() {
+  if [[ ${BLE_ATTACHED-} ]]; then
+    bleopt complete_menu_style=desc
+
+    local compline="${COMP_LINE:0:${COMP_POINT}}"
+    local IFS=$'\n'
+    local c
+    mapfile -t c < <(echo "$compline" | sed -e "s/ \$/ ''/" -e 's/"/\"/g' | xargs example _carapace bash-ble)
+    [[ "${c[*]}" == "" ]] && c=() # fix for mapfile creating a non-empty array from empty command output
+
+    local cand
+    for cand in "${c[@]}"; do
+      [ ! -z "$cand" ] && ble/complete/cand/yield mandb "${cand%$'\t'*}" "${cand##*$'\t'}"
+    done
+  else
+    complete -F _example_completion example
+  fi
+}
+
+complete -F _example_completion_ble example
+
diff --git a/example/cmd/root_test.go b/example/cmd/root_test.go
index 18cccce..b5c17bf 100644
--- a/example/cmd/root_test.go
+++ b/example/cmd/root_test.go
@@ -22,6 +22,10 @@ func TestBash(t *testing.T) {
 	testScript(t, "bash", "./_test/bash.sh")
 }
 
+func TestBashBle(t *testing.T) {
+	testScript(t, "bash-ble", "./_test/bash-ble.sh")
+}
+
 func TestElvish(t *testing.T) {
 	testScript(t, "elvish", "./_test/elvish.elv")
 }