completion: Make filename completions smarter

Many flatpak commands only work on *.flatpak or *.flatpakref files, so
the bash auto completion showing every file is distracting and
unnecessary. This commit makes flatpak only show relevant files when
possible by using the "-G globpattern" compgen option.
This commit is contained in:
Matthew Leeds 2017-09-09 02:33:22 -07:00 committed by Alexander Larsson
parent 2f01d550a0
commit 037a13cde0
8 changed files with 28 additions and 21 deletions

View File

@ -544,7 +544,7 @@ flatpak_complete_build_bundle (FlatpakCompletion *completion)
break;
case 2: /* FILENAME */
flatpak_complete_file (completion);
flatpak_complete_file (completion, "__FLATPAK_BUNDLE_FILE");
break;
}

View File

@ -264,7 +264,7 @@ flatpak_complete_build_import (FlatpakCompletion *completion)
break;
case 2: /* FILENAME */
flatpak_complete_file (completion);
flatpak_complete_file (completion, "__FLATPAK_BUNDLE_FILE");
break;
}

View File

@ -237,7 +237,7 @@ flatpak_complete_document_export (FlatpakCompletion *completion)
flatpak_complete_options (completion, global_entries);
flatpak_complete_options (completion, options);
flatpak_complete_file (completion);
flatpak_complete_file (completion, "__FLATPAK_FILE");
break;
}

View File

@ -148,7 +148,7 @@ flatpak_complete_document_info (FlatpakCompletion *completion)
flatpak_complete_options (completion, global_entries);
flatpak_complete_options (completion, options);
flatpak_complete_file (completion);
flatpak_complete_file (completion, "__FLATPAK_FILE");
break;
}

View File

@ -112,7 +112,7 @@ flatpak_complete_document_unexport (FlatpakCompletion *completion)
flatpak_complete_options (completion, global_entries);
flatpak_complete_options (completion, options);
flatpak_complete_file (completion);
flatpak_complete_file (completion, "__FLATPAK_FILE");
break;
}

View File

@ -5668,10 +5668,11 @@ is_word_separator (char c)
}
void
flatpak_complete_file (FlatpakCompletion *completion)
flatpak_complete_file (FlatpakCompletion *completion,
const char *file_type)
{
flatpak_completion_debug ("completing FILE");
g_print ("%s\n", "__FLATPAK_FILE");
g_print ("%s\n", file_type);
}
void
@ -5928,7 +5929,7 @@ flatpak_complete_options (FlatpakCompletion *completion,
}
else if (strcmp (e->arg_description, "FILE") == 0)
{
flatpak_complete_file (completion);
flatpak_complete_file (completion, "__FLATPAK_FILE");
}
else
flatpak_complete_word (completion, "%s", prefix);

View File

@ -673,7 +673,8 @@ void flatpak_complete_partial_ref (FlatpakCompletion *completion,
const char *only_arch,
FlatpakDir *dir,
const char *remote);
void flatpak_complete_file (FlatpakCompletion *completion);
void flatpak_complete_file (FlatpakCompletion *completion,
const char *file_type);
void flatpak_complete_dir (FlatpakCompletion *completion);
void flatpak_complete_options (FlatpakCompletion *completion,
GOptionEntry *entries);

View File

@ -11,19 +11,24 @@ __flatpak() {
COMPREPLY=()
for i in "${!RES[@]}"; do
if [[ "${RES[$i]}" = "__FLATPAK_FILE" ]]; then
if [[ "${cur}" = "=" ]]; then
CUR=""
else
CUR="${cur}"
fi
COMPREPLY=("${COMPREPLY[@]}" $(compgen -f -- "${CUR}") )
declare -a COMPGEN_OPTS=('-f')
elif [[ "${RES[$i]}" = "__FLATPAK_BUNDLE_FILE" ]]; then
declare -a COMPGEN_OPTS=('-f' '-G' '*.flatpak')
elif [[ "${RES[$i]}" = "__FLATPAK_BUNDLE_OR_REF_FILE" ]]; then
declare -a COMPGEN_OPTS=('-f' '-G' '*.flatpak@(|ref)')
elif [[ "${RES[$i]}" = "__FLATPAK_DIR" ]]; then
if [[ "${cur}" = "=" ]]; then
CUR=""
else
CUR="${cur}"
fi
COMPREPLY=("${COMPREPLY[@]}" $(compgen -d -- "${CUR}") )
declare -a COMPGEN_OPTS=('-d')
else
declare -a COMPGEN_OPTS=()
fi
if [[ ${#COMPGEN_OPTS[@]} -ne 0 ]]; then
if [[ "${cur}" = "=" ]]; then
CUR=""
else
CUR="${cur}"
fi
COMPREPLY=("${COMPREPLY[@]}" $(compgen ${COMPGEN_OPTS[@]} -- "${CUR}") )
else
COMPREPLY=("${COMPREPLY[@]}" "${RES[$i]}")
fi