diff options
author | Tom Ryder <tom@sanctum.geek.nz> | 2015-10-21 13:08:09 +1300 |
---|---|---|
committer | Tom Ryder <tom@sanctum.geek.nz> | 2015-10-21 13:08:09 +1300 |
commit | dcfc80aaa112498498d93bae3ae0eb2088244c47 (patch) | |
tree | 889854ba1c96f1c86465da1bbd5cc87f0c8d4b9f /bash/bashrc.d/ftp.bash | |
parent | Handle spaces correctly in completions (diff) | |
download | dotfiles-dcfc80aaa112498498d93bae3ae0eb2088244c47.tar.gz dotfiles-dcfc80aaa112498498d93bae3ae0eb2088244c47.zip |
Tidy up completion considerably; no more compgen
* Remove all instances of compgen; for filename completion it's quite
broken as it relies on implicit wordsplitting in array context, and
doesn't have an option to print with a null delimiter; replaced with
manual for/while loops instead
* Add IFS= to while/read loops over filenames
* Use "dirname/s" instead of "dir/s" variables to avoid keyword
collisions and for clarity
* Remove some unnecessary variables
* Use shorter syntax for loop exit conditions
* Move completion options into functions where applicable rather than
having them on the completion definition itself
Diffstat (limited to 'bash/bashrc.d/ftp.bash')
-rw-r--r-- | bash/bashrc.d/ftp.bash | 25 |
1 files changed, 14 insertions, 11 deletions
diff --git a/bash/bashrc.d/ftp.bash b/bash/bashrc.d/ftp.bash index b8e8c55e..f046a683 100644 --- a/bash/bashrc.d/ftp.bash +++ b/bash/bashrc.d/ftp.bash @@ -1,14 +1,13 @@ # Completion for ftp with .netrc machines _ftp() { - local word - word=${COMP_WORDS[COMP_CWORD]} + + # Do default completion if no results + compopt -o default # Bail if the .netrc file is illegible local netrc netrc=$HOME/.netrc - if [[ ! -r $netrc ]] ; then - return 1 - fi + [[ -r $netrc ]] || return 1 # Tokenize the file local -a tokens @@ -16,19 +15,23 @@ _ftp() { # Iterate through tokens and collect machine names local -a machines - local -i machine + local -i nxm local token for token in "${tokens[@]}" ; do - if ((machine)) ; then + if ((nxm)) ; then machines=("${machines[@]}" "$token") - machine=0 + nxm=0 elif [[ $token == machine ]] ; then - machine=1 + nxm=1 fi done # Generate completion reply - COMPREPLY=( $(compgen -W "${machines[*]}" -- "$word") ) + local machine + for machine in "${machines[@]}" ; do + [[ $machine == "${COMP_WORDS[COMP_CWORD]}"* ]] || continue + COMPREPLY=("${COMPREPLY[@]}" "$machine") + done } -complete -F _ftp -o default ftp +complete -F _ftp ftp |