Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

bleh.sh #431

Closed
4 tasks done
rsteube opened this issue Feb 20, 2022 · 9 comments
Closed
4 tasks done

bleh.sh #431

rsteube opened this issue Feb 20, 2022 · 9 comments
Labels

Comments

@rsteube
Copy link
Member

rsteube commented Feb 20, 2022

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

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

related carapace-sh/carapace-bin#938

@rsteube rsteube changed the title bash-ble bleh.sh Feb 20, 2022
@akinomyoga
Copy link

  • 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.

@rsteube
Copy link
Member Author

rsteube commented Feb 23, 2022

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>

@akinomyoga
Copy link

akinomyoga commented Feb 23, 2022

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!

@rsteube
Copy link
Member Author

rsteube commented Feb 23, 2022

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?).

@akinomyoga
Copy link

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
Copy link

akinomyoga commented Feb 23, 2022

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

@rsteube
Copy link
Member Author

rsteube commented Feb 23, 2022

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

@akinomyoga
Copy link

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

@akinomyoga
Copy link

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 (akinomyoga/ble.sh@f8bbe2c 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 (akinomyoga/ble.sh@7b70a0e). You can call the following command in the completion function for ble.sh.

compopt -o ble/no-default

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants