From 76c92efdab7afd6b01a0fb4b90964722577279e5 Mon Sep 17 00:00:00 2001 From: Tom Ryder Date: Sat, 12 Oct 2013 16:47:32 +1300 Subject: Add .netrc completion for ftp(1) --- .gitignore | 1 + bash/bashrc.d/ftp.bash | 31 +++++++++++++++++++++++++++++++ 2 files changed, 32 insertions(+) create mode 100644 bash/bashrc.d/ftp.bash diff --git a/.gitignore b/.gitignore index 5a749c56..40f82fb4 100644 --- a/.gitignore +++ b/.gitignore @@ -4,6 +4,7 @@ bash/bashrc.d/* !bash/bashrc.d/cd.bash !bash/bashrc.d/completion.bash !bash/bashrc.d/ed.bash +!bash/bashrc.d/ftp.bash !bash/bashrc.d/grep.bash !bash/bashrc.d/ls.bash !bash/bashrc.d/mysql.bash diff --git a/bash/bashrc.d/ftp.bash b/bash/bashrc.d/ftp.bash new file mode 100644 index 00000000..8952cbb8 --- /dev/null +++ b/bash/bashrc.d/ftp.bash @@ -0,0 +1,31 @@ +# Completion for ftp with .netrc machines +_ftp() { + local netrc=$HOME/.netrc + local word=${COMP_WORDS[COMP_CWORD]} + local -a machines + + # Bail if the .netrc file is illegible + if [[ ! -r $netrc ]]; then + return 1 + fi + + # Tokenize the file + local -a tokens + IFS=$' \t\n' read -a tokens -d '' -r < "$netrc" + + # Iterate through tokens and collect machine names + local machine=0 + for token in "${tokens[@]}"; do + if ((machine)); then + machines=("${machines[@]}" "$token") + machine=0 + elif [[ $token == machine ]]; then + machine=1 + fi + done + + # Generate completion reply + COMPREPLY=( $(compgen -W "${machines[*]}" -- "$word") ) +} +complete -F _ftp ftp + -- cgit v1.2.3