completion: Declare variables as local in gio completion script

Most variables were, but a few were not declared as local, and hence
leaked into the calling environment every time someone tab-completed the
`gio` command.

Signed-off-by: Philip Withnall <pwithnall@endlessos.org>

Fixes: #2275
This commit is contained in:
Philip Withnall 2021-01-04 19:14:25 +00:00
parent c537c93c6a
commit 2ca4d865b2

View File

@ -22,7 +22,8 @@
# Check whether the suggestions have common prefix (i.e. suggestions won't be
# shown and prefix will be completed first)
__has_common_prefix() {
__gio_has_common_prefix() {
local i
for (( i = 1; i < ${#COMPREPLY[@]}; i++ )); do
if [[ "${COMPREPLY[i-1]:${#cur}:1}" != "${COMPREPLY[i]:${#cur}:1}" ]]; then
return 1 # False
@ -49,6 +50,7 @@ __gio_location() {
# List volumes and mounts
local mounts=( )
local mount
while IFS=$'\n' read mount; do
# Do not care about local mounts
[[ "$mount" =~ ^"file:" ]] && continue
@ -58,12 +60,13 @@ __gio_location() {
done < <(gio mount -li | sed -n -r 's/^ *(default_location|activation_root)=(.*)$/\2/p' | sort -u)
# Workaround to unescape dir name (e.g. "\ " -> " ")
declare -a tmp="( ${dir} )"
unescaped_dir="${tmp[0]}"
local -a tmp="( ${dir} )"
local unescaped_dir="${tmp[0]}"
# List files
local files=()
local names=()
local name size type
while IFS=$'\t' read name size type; do
# Escape name properly
local escaped_name="$(printf "%q" "$name")"
@ -75,7 +78,7 @@ __gio_location() {
escaped_name="$escaped_name "
fi
path="$dir$escaped_name"
local path="$dir$escaped_name"
# Use only matching paths
if [[ "$path" =~ ^"$cur" ]]; then
@ -87,7 +90,7 @@ __gio_location() {
COMPREPLY=("${files[@]}" "${mounts[@]}")
# Workaround to show suggestions as basenames only
if ! __has_common_prefix; then
if ! __gio_has_common_prefix; then
COMPREPLY=("${mounts[@]} ${names[@]}")
# Workaround to prevent overwriting suggestions, it adds empty