carapace icon indicating copy to clipboard operation
carapace copied to clipboard

bleh.sh

Open rsteube opened this issue 3 years ago • 9 comments

https://github.com/akinomyoga/ble.sh

  • [x] fix optarg argument compeltion
  • [ ] verify/fix special characters
  • [ ] verify/fix open quotes
  • [x] prevent default file completion when no values returned

related https://github.com/rsteube/carapace-bin/issues/938

rsteube avatar Feb 20 '22 13:02 rsteube

  • [ ] prevent default file completion when no values returned

Currently, the default completions are always started when there are no completions. I'll later add a compopt option to change the behavior, probably this weekend.

If there are any other requests or questions on the detailed behavior of ble.sh, please feel free to tell me that. Thank you.

akinomyoga avatar Feb 23 '22 03:02 akinomyoga

Thanks, I've been meaning to ask about that. Regarding optarg arguments it seems the completer is not invoked when the flagname contains =:

❯ example action --optarg=<TAB>

rsteube avatar Feb 23 '22 09:02 rsteube

Regarding optarg arguments it seems the completer is not invoked when the flagname contains =:

❯ example action --optarg=<TAB>

Thank you for the report! How can I reproduce it?

~~I have tried to run the completion by the above command and ended up the following ~/.bashrc but still cannot properly set up the carapace completion for example.~~

# git repository for carapace (HEAD = c79a097)
carapace_path=~/.mwg/git/rsteube/carapace
# git repository for carapace (HEAD = 775bab9a) compiled with "docker-compose run build"
carapace_bin_path=~/.mwg/git/rsteube/carapace-bin

PATH=$carapace_bin_path/cmd/carapace:$PATH
source ~/.mwg/src/ble.sh/out/ble.sh --norc
eval "$(carapace _carapace bash-ble)"
source "$carapace_path/example/cmd/_test/bash-ble.sh"
ble/bin#has example || ln -s carapace "$carapace_bin_path/cmd/carapace/example"

I have fixed carapace/example/cmd/_test/bash-ble.sh by guess as follows, which still doesn't work:

diff --git a/example/cmd/_test/bash-ble.sh b/example/cmd/_test/bash-ble.sh
index 62c1509..013bb7c 100644
--- a/example/cmd/_test/bash-ble.sh
+++ b/example/cmd/_test/bash-ble.sh
@@ -24,7 +24,7 @@ _example_completion_ble() {
     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)
+    mapfile -t c < <(echo "$compline" | sed -e "s/ \$/ ''/" -e 's/"/\"/g' | xargs example example bash-ble)
     [[ "${c[*]}" == "" ]] && c=() # fix for mapfile creating a non-empty array from empty command output

     local cand

Edit: Ah, OK. I now noticed that example is probably a program independent from carapace, and I guess I need to build it in carapace repository. I'm now running docker-compose up in the carapace repository. Wait for a while until my machine compiles everything...

For now, I gave up trying to check the behavior for example action --optarg= and tried with ls --time-style=[TAB], but it seems the completion function _ls_completion is called with COMP_LINE='ls --time-style=' COMP_POINT='16' (though it doesn't generate any candidates). Do you have any advice to reproduce the behavior in my environment? Thank you!

akinomyoga avatar Feb 23 '22 10:02 akinomyoga

Will have a look at this later. It was just a quick assumption, i haven't analysed this in detail yet. I think there might rather be a problem with the default ble.sh completions interfering with the ones provided by carapace. E.g. tail --follow= does indeed invoke the completions by carapace (not sure what was the problem with example) but i am getting shown file completion from ble.sh. Surely possible the completion candidates don't have the right format yet (does ble.sh use COMP_WORDBREAKS in any way?).

rsteube avatar Feb 23 '22 10:02 rsteube

Thank you for the comment! If I could reproduce it, I'll also investigate it.

does ble.sh use COMP_WORDBREAKS in any way?

The internal completion generation does not use COMP_WORDBREAKS, but ble.sh uses COMP_WORDBREAKS to prepare COMP_WORDS and COMP_CWORD for external completions. However, example completion does not seem to use these variables. Anyway, I will look at it from now as the build has completed just now!

akinomyoga avatar Feb 23 '22 10:02 akinomyoga

OK, there are two reasons that prevent the optarg completion from working properly.

  • One is that another internal completion context, which has higher precedence than the command-argument completion context, is involved here. I think I need to redesign the precedence determination of different completion contexts.

    The current implementation is this: When ble.sh tries completions, it first enumerates possible completion contexts, where the context with a later starting point has higher precedence:

    • For example, for echo "${var[TAB], it will enumerate 1) the command-argument completion context for the argument "${var and 2) the variable-name completion context for var. In this case, the variable-name context has higher precedence.
    • For the present case of example action --optarg=[TAB], it actually enumerates 1) the command-argument completion context for --optarg= and 2) the RHS-of-assignment completion context for "" (empty string). In this case, the RHS context wins, so the command-argument completion will not be attempted.

    I think I need to think about other criteria to determine the precedence of multiple completion contexts than the starting point of the completion. Hmm.

  • Another is that ble/complete/cand/yield is the function for the native completion (which doesn't use COMP_WORDBREAK), so we need to pass the full word (before being split by COMP_WORDBREAK) in the second argument, such as --optarg=blue (instead of just blue). The solution to this one is easy. The prefix truncated by COMP_WORDBREAKS is stored in progcomp_prefix (progcomp_prefix=--optarg= in this case), so we can actually yield "$progcomp_prefix$cand" as follows:

diff --git a/example/cmd/_test/bash-ble.sh b/example/cmd/_test/bash-ble.sh
index 62c1509..84aa22b 100644
--- a/example/cmd/_test/bash-ble.sh
+++ b/example/cmd/_test/bash-ble.sh
@@ -29,7 +29,7 @@ _example_completion_ble() {

     local cand
     for cand in "${c[@]}"; do
-      [ ! -z "$cand" ] && ble/complete/cand/yield mandb "${cand%$'\t'*}" "${cand##*$'\t'}"
+      [ ! -z "$cand" ] && ble/complete/cand/yield mandb "$progcomp_prefix${cand%$'\t'*}" "${cand##*$'\t'}"
     done
   else
     complete -F _example_completion example
diff --git a/internal/bash_ble/snippet.go b/internal/bash_ble/snippet.go
index 244936c..902bc5a 100644
--- a/internal/bash_ble/snippet.go
+++ b/internal/bash_ble/snippet.go
@@ -30,7 +30,7 @@ _%v_completion_ble() {

     local cand
     for cand in "${c[@]}"; do
-      [ ! -z "$cand" ] && ble/complete/cand/yield mandb "${cand%%$'\t'*}" "${cand##*$'\t'}"
+      [ ! -z "$cand" ] && ble/complete/cand/yield mandb "$progcomp_prefix${cand%%$'\t'*}" "${cand##*$'\t'}"
     done
   else
     complete -F _%v_completion %v

akinomyoga avatar Feb 23 '22 12:02 akinomyoga

The prefix should already be present: 2022-02-23-160121_1276x770_scrot

rsteube avatar Feb 23 '22 15:02 rsteube

Ah, OK. I see. Then the only problem must be the first one. Thank you for pointing it out.

akinomyoga avatar Feb 23 '22 15:02 akinomyoga

Sorry for the delay.

Ah, OK. I see. Then the only problem must be the first one. Thank you for pointing it out.

For this problem, I have finally decided not to generate the RHS-completion context, so that only the argument completion (and thus progcomp completion) is activated. I have now pushed the fix in master (https://github.com/akinomyoga/ble.sh/commit/f8bbe2c4e407156c9d3d941ec7a049792261cd13 is the corresponding commit).

[ ] prevent default file completion when no values returned

For this one, I have newly added a compopt option ble/no-default (https://github.com/akinomyoga/ble.sh/commit/7b70a0e03bee527570449fdbf41abecd4928da7b). You can call the following command in the completion function for ble.sh.

compopt -o ble/no-default

akinomyoga avatar Mar 04 '22 13:03 akinomyoga