From 523404ecb35fbadab7ac49ba1d151ce035b31ca0 Mon Sep 17 00:00:00 2001 From: desktop Date: Sun, 29 Oct 2023 22:04:26 +0300 Subject: [PATCH] bash : archive_rename : add autocompletion. --- document/linux/config/bash/module/archive.sh | 19 +++++++++++++++++ .../linux/config/bash/module/autocomplete.sh | 21 ++++++++----------- 2 files changed, 28 insertions(+), 12 deletions(-) diff --git a/document/linux/config/bash/module/archive.sh b/document/linux/config/bash/module/archive.sh index 2a2fcdb..52874a1 100644 --- a/document/linux/config/bash/module/archive.sh +++ b/document/linux/config/bash/module/archive.sh @@ -178,4 +178,23 @@ _archive_list() _autocomplete "$(ls *_*.tar.* 2> /dev/null)" } +_archive_rename() +{ + COMPREPLY=() + + local cur="${COMP_WORDS[COMP_CWORD]}" + local prev="${COMP_WORDS[COMP_CWORD-1]}" + local command="${COMP_WORDS[0]}" + + if [[ "${prev}" = "${command}" ]]; then + COMPREPLY=( $(compgen -W "$(ls -a)" -- ${cur}) ) + return 0 + else + local name="${prev%_*}" + COMPREPLY=( $(compgen -W "${name}" -- ${cur}) ) + return 0 + fi +} + complete -o plusdirs -F _archive_list archive_check unarchive +complete -F _archive_rename archive_rename diff --git a/document/linux/config/bash/module/autocomplete.sh b/document/linux/config/bash/module/autocomplete.sh index b3a3752..8f30690 100644 --- a/document/linux/config/bash/module/autocomplete.sh +++ b/document/linux/config/bash/module/autocomplete.sh @@ -4,13 +4,12 @@ _autocomplete() { local commands="$@" - local cur prev COMPREPLY=() - cur="${COMP_WORDS[COMP_CWORD]}" - prev="${COMP_WORDS[COMP_CWORD-1]}" - command="${COMP_WORDS[0]}" + local cur="${COMP_WORDS[COMP_CWORD]}" + local prev="${COMP_WORDS[COMP_CWORD-1]}" + local command="${COMP_WORDS[0]}" COMPREPLY=( $(compgen -W "${commands}" -- ${cur}) ) return 0 @@ -20,13 +19,12 @@ _autocomplete() _autocomplete_first() { local commands="$@" - local cur prev COMPREPLY=() - cur="${COMP_WORDS[COMP_CWORD]}" - prev="${COMP_WORDS[COMP_CWORD-1]}" - command="${COMP_WORDS[0]}" + local cur="${COMP_WORDS[COMP_CWORD]}" + local prev="${COMP_WORDS[COMP_CWORD-1]}" + local command="${COMP_WORDS[0]}" if [[ "${prev}" = "${command}" ]]; then COMPREPLY=( $(compgen -W "${commands}" -- ${cur}) ) @@ -38,13 +36,12 @@ _autocomplete_first() _autocomplete_first_ls() { local commands="$@" - local cur prev COMPREPLY=() - cur="${COMP_WORDS[COMP_CWORD]}" - prev="${COMP_WORDS[COMP_CWORD-1]}" - command="${COMP_WORDS[0]}" + local cur="${COMP_WORDS[COMP_CWORD]}" + local prev="${COMP_WORDS[COMP_CWORD-1]}" + local command="${COMP_WORDS[0]}" if [[ "${prev}" = "${command}" ]]; then COMPREPLY=( $(compgen -W "${commands}" -- ${cur}) )