cmd: make autocomplete compatible with bash's posix mode #3489

This commit is contained in:
Danil Semelenov 2019-09-06 15:11:08 +03:00 committed by Nick Craig-Wood
parent f1347139fa
commit 1382dba3c8

View file

@ -39,7 +39,7 @@ documentation, changelog and configuration walkthroughs.
const ( const (
bashCompletionFunc = ` bashCompletionFunc = `
__rclone_custom_func() { __rclone_custom_func() {
if [[ ${#COMPREPLY[@]} -eq 0 && :$SHELLOPTS: != *:posix:* ]]; then if [[ ${#COMPREPLY[@]} -eq 0 ]]; then
local cur cword prev words local cur cword prev words
if declare -F _init_completion > /dev/null; then if declare -F _init_completion > /dev/null; then
_init_completion -n : || return _init_completion -n : || return
@ -47,10 +47,14 @@ __rclone_custom_func() {
__rclone_init_completion -n : || return __rclone_init_completion -n : || return
fi fi
if [[ $cur != *:* ]]; then if [[ $cur != *:* ]]; then
local ifs=$IFS
IFS=$'\n'
local remotes=($(command rclone listremotes))
IFS=$ifs
local remote local remote
while IFS= read -r remote; do for remote in "${remotes[@]}"; do
[[ $remote != $cur* ]] || COMPREPLY+=("$remote") [[ $remote != $cur* ]] || COMPREPLY+=("$remote")
done < <(command rclone listremotes) done
if [[ ${COMPREPLY[@]} ]]; then if [[ ${COMPREPLY[@]} ]]; then
local paths=("$cur"*) local paths=("$cur"*)
[[ ! -f ${paths[0]} ]] || COMPREPLY+=("${paths[@]}") [[ ! -f ${paths[0]} ]] || COMPREPLY+=("${paths[@]}")
@ -62,11 +66,15 @@ __rclone_custom_func() {
else else
local prefix= local prefix=
fi fi
local ifs=$IFS
IFS=$'\n'
local lines=($(rclone lsf "${cur%%:*}:$prefix" 2>/dev/null))
IFS=$ifs
local line local line
while IFS= read -r line; do for line in "${lines[@]}"; do
local reply=${prefix:+$prefix/}$line local reply=${prefix:+$prefix/}$line
[[ $reply != $path* ]] || COMPREPLY+=("$reply") [[ $reply != $path* ]] || COMPREPLY+=("$reply")
done < <(rclone lsf "${cur%%:*}:$prefix" 2>/dev/null) done
[[ ! ${COMPREPLY[@]} || $(type -t compopt) != builtin ]] || compopt -o filenames [[ ! ${COMPREPLY[@]} || $(type -t compopt) != builtin ]] || compopt -o filenames
fi fi
[[ ! ${COMPREPLY[@]} || $(type -t compopt) != builtin ]] || compopt -o nospace [[ ! ${COMPREPLY[@]} || $(type -t compopt) != builtin ]] || compopt -o nospace