-
Notifications
You must be signed in to change notification settings - Fork 4
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
Option to only complete flags when -
has been typed
#32
Comments
I am happy you find them useful, thanks for sharing. As for the "no flag completion unless the cursor is at -" - I don't hate the idea, assuming it can be implemented without influencing the user interface (config syntax). Just to be sure we are on the same page: Given this mygit:
- -h
- -v
- --help
- --version
- pull
- push
- status
- commit running this commant: $ completely test "mygit " currently outputs:
and you want it to output just:
yes? And as for the shellcheck - I would love to make the output script shellcheck compliant.
and when I manually change the script with these two changes: _mygit_completions() {
local cur=${COMP_WORDS[COMP_CWORD]}
- local compline="${COMP_WORDS[@]:1:$COMP_CWORD-1}"
+ local compline="${COMP_WORDS[*]:1:$COMP_CWORD-1}"
case "$compline" in
*)
- while read; do COMPREPLY+=( "$REPLY" ); done < <( compgen -W "-h -v --help --version pull push status commit" -- "$cur" )
+ while read -r; do COMPREPLY+=( "$REPLY" ); done < <( compgen -W "-h -v --help --version pull push status commit" -- "$cur" )
;;
esac shellcheck is satisfied, and it seems to be working just the same. If this can be confirmed as non breaking, I can modify the template. |
Exactly, and running: $ completely test "mygit -" would output:
This is how I find completions to work in general, e.g. for git, grep, sed, kubectl, docker, etc.
Yeah, on this one I always listened to shellcheck, this demonstrates the issue: # with -r
$ while read -r line; do printf '%s\n' "$line"; done < <(echo 'so much fu\\n')
so much fu\n # correct, backslash preserved literally, as it was in the original output
# without -r
$ while read line; do printf '%s\n' "$line"; done < <(echo $'so much fu\\n')
so much fu
# <- incorrect, we're on a new line |
My "fix" for the other shellcheck issue (https://www.shellcheck.net/wiki/SC2124) is wrong. As for the implementation of the primary topic of this issue - I will need to take a deeper look if this can be easily implemented. I am not sure - due to the fact that at this point, the generated script does not care at all about the cursor position - it lets |
If you can take a look at #33 - I will merge it, so we have at least 1/3 of the issues solved. |
I think #33 does it.
The script doesn't need to worry about the cursor position, if [[ $cur == -* ]] ; then
case "$comp_line" in
# complete flags
esac
else
case "$comp_line" in
# complete other stuff
esac
fi And then the main problem is splitting specific cases and |
No. It only solves the
Yeah. I said cursor, but meant current word. Completely currently discards it, it will need to use it. |
Oh, I completely missed this. So the right side produces an array. Since there's a difference in behaviour in different shells (in zsh local comptemp=("${COMP_WORDS[@]:1:$COMP_CWORD-1}")
local compline="${comptemp[*]}" That would satisfy shellcheck.
I'm not sure what you mean, Assuming config: mygit:
- --help
- --version
- push
mygit push:
- --help
- --force it would produce: if [[ $cur == -* ]] ; then
case "$compline" in
'push'*)
while read -r; do COMPREPLY+=( "$REPLY" ); done < <( compgen -W "--help --force" -- "$cur" )
;;
*)
while read -r; do COMPREPLY+=( "$REPLY" ); done < <( compgen -W "--help --version" -- "$cur" )
;;
esac
else
case "$compline" in
*)
while read -r; do COMPREPLY+=( "$REPLY" ); done < <( compgen -W "push" -- "$cur" )
;;
esac
fi |
However, the problem I'm now seeing is what if I had a custom command for completions, like cli:
- --help
- --version
# this would be included in `compgen -W` if `$cur` *doesn't* start with `-`
- $(mycompleter 2>/dev/null)
# this would be included in `compgen -W` if `$cur` *does* start with `-`
- flags:$(mycompleter 2>/dev/null) So here This is basically something I'm doing right now, where I have a wrapper around $ mykubectl get pod <TAB>
pod1 pod2 --help --version instead of just: $ mykubectl get pod <TAB>
pod1 pod2 Hence I raised this 🙂 |
The second shellcheck fix is ready in #34 - if you can review it will be nice (also added a short spec to ensure the generated script is always shellcheck compliant). As for the flag completion issue - maybe we should approach it from a simpler angel. How about: # old line
while read -r; do COMPREPLY+=( "$REPLY" ); done < <( compgen -W "--help --verbose ... " -- "$cur" )
# new line
while read -r; do COMPREPLY+=( "$REPLY" ); done < <( compgen -W get_word_string -- "$cur" )
# and the function pseudo code
get_word_string() {
# remove string words if they start with "-" and $cur is not starting with "-"
return string
} This makes it so we move the logic to a "post processing" step, and therefore it will support custom commands just the same. Thoughts?
P.S.. And now when I see it, I agree it is terribly annoying. |
I like the post processing wrapper idea! |
I'll play with it then. |
Does this seem right to you as a basis? #!/usr/bin/env bash
_filter() {
local words="$1"
local cur="a" # this will be taken from the actual input
local result=()
if [[ "${cur:0:1}" == "-" ]]; then
echo "$words"
else
for word in $words; do
[[ "${word:0:1}" != "-" ]] && result+=("$word")
done
echo "${result[*]}"
fi
}
( compgen -W "$(_filter "--help --version status commit")" -- "$cur" ) EDIT: I replaced the function so it handles a string and not an array, so it it easier to send it from within a |
Ok - #35 includes the new implementation. There is no configuration to switch between the old and new completions. This is quite a change and has a breaking potential - so it needs to be thoroughly reviewed.
|
Completely 0.5.0 released to rubygems and dockerhub with this change. (bashly will be released shortly) |
It works beautifully, thank you for doing it so quickly as well ❤️ |
Excellent, thanks for confirming and for participating in this fix. |
First of all, thank you for your excellent projects, bashly and completely. They are fantastic and certainly made creating Bash tools more bearable.
Seeing the improvements made to flag autocompletion from DannyBen/bashly#224, I wonder if it would be possible, maybe as an opt-in setting, to make the completion only output flags when the cursor has stopped at
-
, and output no flags otherwise.I think this behaviour is pretty common and used in many tools' completions. In essence:
What do you think?
On a separate note, normally shellcheck complains about
read
without-r
, I'm unsure if it should be included here?See https://www.shellcheck.net/wiki/SC2162
The text was updated successfully, but these errors were encountered: