2018-06-15 11:20:51 +02:00
#
# Copyright (C) 2018 Red Hat, Inc.
#
# This library is free software; you can redistribute it and/or modify
# it under the terms of the GNU Lesser General Public License as
# published by the Free Software Foundation; either version 2.1 of the
# licence, or (at your option) any later version.
#
# This is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
# FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
# License for more details.
#
# You should have received a copy of the GNU Lesser General Public License
# along with this library; if not, see <http://www.gnu.org/licenses/>.
#
# Check for bash
[ -z "$BASH_VERSION" ] && return
####################################################################################################
# Check whether the suggestions have common prefix (i.e. suggestions won't be
# shown and prefix will be completed first)
2021-01-04 20:14:25 +01:00
__gio_has_common_prefix() {
local i
2018-06-15 11:20:51 +02:00
for (( i = 1; i < ${#COMPREPLY[@]}; i++ )); do
if [[ "${COMPREPLY[i-1]:${#cur}:1}" != "${COMPREPLY[i]:${#cur}:1}" ]]; then
return 1 # False
fi
done
return 0 # True
}
# Complete file location
__gio_location() {
# Prevent breaking on colons, we have to work with uris
local cur
_get_comp_words_by_ref -n : cur
# Resolve dirname for dir listing
local dir=""
if [[ $cur =~ "/"$ ]]; then
dir="$cur"
elif [[ $cur =~ "/" ]]; then
2019-10-03 14:44:57 +02:00
# Subtract basename because dirname cmd doesn't work well with schemes
dir=${cur%$(basename "$cur")}
2018-06-15 11:20:51 +02:00
fi
2019-10-03 15:09:56 +02:00
# List volumes and mounts
local mounts=( )
2021-01-04 20:14:25 +01:00
local mount
2019-10-03 15:09:56 +02:00
while IFS=$'\n' read mount; do
# Do not care about local mounts
[[ "$mount" =~ ^"file:" ]] && continue
2018-06-15 11:20:51 +02:00
2019-10-03 15:09:56 +02:00
# Use only matching mounts
[[ "$mount" =~ ^"$cur" && "$mount" != "$cur" ]] && mounts+=("$mount")
done < <(gio mount -li | sed -n -r 's/^ *(default_location|activation_root)=(.*)$/\2/p' | sort -u)
2018-06-15 11:20:51 +02:00
# Workaround to unescape dir name (e.g. "\ " -> " ")
2021-01-04 20:14:25 +01:00
local -a tmp="( ${dir} )"
local unescaped_dir="${tmp[0]}"
2018-06-15 11:20:51 +02:00
# List files
local files=()
local names=()
2021-01-04 20:14:25 +01:00
local name size type
2018-06-15 11:20:51 +02:00
while IFS=$'\t' read name size type; do
# Escape name properly
local escaped_name="$(printf "%q" "$name")"
# Append slash for directories and space for files
if [[ "$type" == "(directory)" ]]; then
escaped_name="$escaped_name/"
else
escaped_name="$escaped_name "
fi
2021-01-04 20:14:25 +01:00
local path="$dir$escaped_name"
2018-06-15 11:20:51 +02:00
# Use only matching paths
if [[ "$path" =~ ^"$cur" ]]; then
files+=("$path")
names+=("$escaped_name")
fi
done < <(gio list -hl "$unescaped_dir" 2> /dev/null)
COMPREPLY=("${files[@]}" "${mounts[@]}")
# Workaround to show suggestions as basenames only
2021-01-04 20:14:25 +01:00
if ! __gio_has_common_prefix; then
2018-06-15 11:20:51 +02:00
COMPREPLY=("${mounts[@]} ${names[@]}")
2020-06-12 15:02:30 +02:00
# Workaround to prevent overwriting suggestions, it adds empty
2018-06-15 11:20:51 +02:00
# suggestion, otherwise names with colons will be corrupted
COMPREPLY+=(" ")
return 0
fi
# Workaround to complete names with colons, it removes colon prefix from
# COMPREPLY
__ltrim_colon_completions "$cur"
}
__gio() {
# Complete subcommands
if (( ${COMP_CWORD} == 1 )); then
2020-12-03 10:42:47 +01:00
COMPREPLY=($(compgen -W "help version cat copy info launch list mime mkdir monitor mount move open rename remove save set trash tree" -- "${COMP_WORDS[1]}"))
2018-06-15 11:20:51 +02:00
compopt +o nospace
return 0
fi
# Complete file locations
__gio_location
}
####################################################################################################
complete -o nospace -F __gio gio