2 # Copyright (C) 2018 Red Hat, Inc.
4 # This library is free software; you can redistribute it and/or modify
5 # it under the terms of the GNU Lesser General Public License as
6 # published by the Free Software Foundation; either version 2.1 of the
7 # licence, or (at your option) any later version.
9 # This is distributed in the hope that it will be useful, but WITHOUT
10 # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 # FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
12 # License for more details.
14 # You should have received a copy of the GNU Lesser General Public License
15 # along with this library; if not, see <http://www.gnu.org/licenses/>.
19 [ -z "$BASH_VERSION" ] && return
21 ####################################################################################################
23 # Check whether the suggestions have common prefix (i.e. suggestions won't be
24 # shown and prefix will be completed first)
25 __has_common_prefix() {
26 for (( i = 1; i < ${#COMPREPLY[@]}; i++ )); do
27 if [[ "${COMPREPLY[i-1]:${#cur}:1}" != "${COMPREPLY[i]:${#cur}:1}" ]]; then
35 # Complete file location
37 # Prevent breaking on colons, we have to work with uris
39 _get_comp_words_by_ref -n : cur
41 # Resolve dirname for dir listing
43 if [[ $cur =~ "/"$ ]]; then
45 elif [[ $cur =~ "/" ]]; then
46 dir="$(dirname "$cur")/"
49 # List daemon mounts, just if dir is not specified, or looks like scheme
51 if [[ $dir == "" ]] || [[ $dir =~ ":"$ && ! $dir =~ "/" ]]; then
52 while IFS=$'\n' read mount; do
53 # Do not care about local mounts
54 [[ "$mount" =~ ^"file:" ]] && continue
56 # Use only matching mounts
57 [[ "$mount" =~ ^"$cur" && "$mount" != "$cur" ]] && mounts+=("$mount")
58 done < <(gio mount -l | sed -n -r 's/^ *Mount\([0-9]+\): .* -> (.*)$/\1/p')
61 # Workaround to unescape dir name (e.g. "\ " -> " ")
62 declare -a tmp="( ${dir} )"
63 unescaped_dir="${tmp[0]}"
68 while IFS=$'\t' read name size type; do
69 # Escape name properly
70 local escaped_name="$(printf "%q" "$name")"
72 # Append slash for directories and space for files
73 if [[ "$type" == "(directory)" ]]; then
74 escaped_name="$escaped_name/"
76 escaped_name="$escaped_name "
79 path="$dir$escaped_name"
81 # Use only matching paths
82 if [[ "$path" =~ ^"$cur" ]]; then
84 names+=("$escaped_name")
86 done < <(gio list -hl "$unescaped_dir" 2> /dev/null)
88 COMPREPLY=("${files[@]}" "${mounts[@]}")
90 # Workaround to show suggestions as basenames only
91 if ! __has_common_prefix; then
92 COMPREPLY=("${mounts[@]} ${names[@]}")
94 # Workaround to prevent overwritting suggestions, it adds empty
95 # suggestion, otherwise names with colons will be corrupted
101 # Workaround to complete names with colons, it removes colon prefix from
103 __ltrim_colon_completions "$cur"
107 # Complete subcommands
108 if (( ${COMP_CWORD} == 1 )); then
109 COMPREPLY=($(compgen -W "help version cat copy info list mime mkdir monitor mount move open rename save set trash tree" -- "${COMP_WORDS[1]}"))
114 # Complete file locations
118 ####################################################################################################
120 complete -o nospace -F __gio gio