Permalink
Switch branches/tags
Nothing to show
Find file Copy path
Fetching contributors…
Cannot retrieve contributors at this time
78 lines (65 sloc) 2.27 KB
# This file provides a bash completion function for autocompleting remote
# Google Drive paths with rclone
# It might well work with other cloud services supported by rclone, but I have
# not tested any others.
# Requirements:
# rclone >= v1.40 # https://rclone.org/install/
# Installation:
# Cat or copy this script into "${HOME}/.bash_completion", or
# Copy this script into /etc/bash_completion.d/ for system-wide use
#
# Caveats:
# Rclone itself also provides some bash autocompletion, though it does not
# include remote paths. So this should override the other autocompletes, if
# they are installed. In the future, I'll consider how to perhaps integrate
# with them in the future.
_rclone_remote_ls() {
local foundremote opts remotename remotepath remotes rpath_cur rpath_start
COMPREPLY=()
# Since we're dealing with a colon in the remote path, we'll get the
# current arg this way (colon is a stop character for readline)
_get_comp_words_by_ref -n : cur
# Check first that `cur` is an rclone remote in the form of
# `remotename:words/`. A slash (or multiple) may or may or may not be present.
if ! [[ "${cur}" =~ ^[a-zA-Z0-9]*:/?.*/?.* ]]; then
return
fi
# Split the path at the last slash
remotename=${cur%:*}
remotepath=${cur#*\:}
remotes=($(rclone --quiet listremotes | sed 's/:$//g'))
# Look for a remote drive matching the input
foundremote=0
for remote in "${remotes[@]}"; do
if [ "${remotename}" = "${remote}" ]; then
foundremote=1
break
fi
done
if ! [ "${foundremote}" -eq 1 ]; then
return
fi
# If remote path has a slash, we can set up to look in a subdir.
if [[ "${remotepath}" == */* ]]; then
rpath_start=${remotepath%/*}
rpath_cur=${remotepath##*/}
else
rpath_start=''
rpath_cur="${remotepath}"
fi
# Create a final array containing matching file paths for bash autocomplete
opts=()
while IFS= read -r line; do
if [ -z "${rpath_start}" ]; then
line=("${line}")
else
line=("${rpath_start}/${line}")
fi
if [[ "${line}" == "${remotepath}"* ]]; then
opts+=("${line}")
fi
done < <(rclone --quiet lsf --max-depth 1 "${remotename}:${rpath_start}" 2>/dev/null)
COMPREPLY=("${opts[@]}")
return
}
complete -o filenames -F _rclone_remote_ls rclone