Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Remote URL Completion in Bash

I'm currently adding support for remote url completion for svn in Bash. I'm currently using a modified bash-completion-svn that figures out when to trigger the completion on URls looking like

  svn+ssh://$HOST/$DIR/prefix_string

I have managed to extract the parenting directory part of the URL (svn+ssh://$HOST/$DIR) and called svn ls in it and

  cur_suffix=prefix_string

Now what is left is how to use the directory part of the URL and the prefix_string the user typed after it (can be empty) to make the completion happen. Can I use compgen to make this work?

That is to get list of possible completions if last character in url is a slash or a complete directory if suffix is empty.

Currently I'm using

  fix=( $(compgen -o nospace -W "$remote_fileds" -- $cur_suffix))

to make completion happen for URLS such as

svn://HOST/DIR/a 

to complete into forexample

svn://HIST/DIR/a_file

It however always picks the first alternative no matter what and always inserts a space eventhough I have written -o nospace. How do I prevent completion compgen from completing if there are other possible matches (prefix a is nont unique) and how do I make these matches print below the cursor without moving the cursor. Can only complete do this?

like image 221
Nordlöw Avatar asked Jan 27 '26 03:01

Nordlöw


1 Answers

For my bash completion scripts, which necessarily run on across a variety of versions of bash with differing levels of support for completion, I use the following idiom to attempt the best "complete" possible, then gracefully fallback to at least something that should work; e.g., assuming a command svn2, and a completion function called _complete_svn2:

complete -o bashdefault -o default -o nospace -F _complete_svn2 svn2 2>/dev/null \
 || complete -o default -o nospace -F _complete_svn2 svn2 2>/dev/null \
 || complete -o default -F _complete_svn2 svn2 2>/dev/null \
 || complete -F _complete_svn2 svn2

Ideally "-o nospace" exists and works, but if it doesn't, you can fallback to "-o default".

Also, the above example uses a function to generate the completions, arbitrarily called _complete_svn2. Perhaps you can try using something like that also. The following example works for me, showing matching options, not adding in the extra space, etc (it intentionally does not complete "-" options):

_complete_svn2() {
    local options="one two three four five"
    local cur="${COMP_WORDS[COMP_CWORD]}"
    COMPREPLY=()
    [[ ${cur} != -* ]] && COMPREPLY=( $(compgen -W "${options}" -- ${cur}) )
}

E.g., (and no space is added after "five")

$ svn2 f[tab]
five four
$ svn2 fi[tab]
$ svn2 five
like image 84
michael Avatar answered Jan 28 '26 17:01

michael



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!