bash : archive : improve completion.

This commit is contained in:
Dmitry Voronin 2023-10-30 14:22:24 +03:00
parent 91d522a551
commit 25e8685790
3 changed files with 26 additions and 5 deletions

View file

@ -322,6 +322,7 @@ Command|Description
---|--- ---|---
`_autocomplete <ARGS>`|Provide completion based on provided arguments separated by spaces. `_autocomplete <ARGS>`|Provide completion based on provided arguments separated by spaces.
`_autocomplete_first <ARGS>`|Same as `_autocomplete` but works only once for first argument. `_autocomplete_first <ARGS>`|Same as `_autocomplete` but works only once for first argument.
`_autocomplete_grep <PATTERN>`|Ls autocomplete with specified grep filter.
`_autocomplete_nested`|Provides nested completions, just like `sudo` does. `_autocomplete_nested`|Provides nested completions, just like `sudo` does.
## Battery. ## Battery.

View file

@ -1,3 +1,5 @@
_ARCHIVE_PATTERN="_[[:alnum:]]{40}.tar.[xg]z"
# archive file with maximum compression and checksum. # archive file with maximum compression and checksum.
# usage: archive [FILES] # usage: archive [FILES]
archive() archive()
@ -71,7 +73,7 @@ archive_check()
# set dafult value to target all supported archives. # set dafult value to target all supported archives.
if [[ "${targets}" = "" ]]; then if [[ "${targets}" = "" ]]; then
targets=(*_*.tar.*) targets=(${_ARCHIVE_PATTERN})
total=${#targets[@]} total=${#targets[@]}
fi fi
@ -122,7 +124,7 @@ unarchive()
# set dafult value to target all supported archives. # set dafult value to target all supported archives.
if [[ "${targets}" = "" ]]; then if [[ "${targets}" = "" ]]; then
targets=(*_*.tar.*) targets=(${_ARCHIVE_PATTERN})
total=${#targets[@]} total=${#targets[@]}
fi fi
@ -162,7 +164,7 @@ archive_name()
# set dafult value to target all supported archives. # set dafult value to target all supported archives.
if [[ "${targets}" = "" ]]; then if [[ "${targets}" = "" ]]; then
targets=(*_*.tar.*) targets=(${_ARCHIVE_PATTERN})
total=${#targets[@]} total=${#targets[@]}
fi fi
@ -217,7 +219,7 @@ _archive_name()
local command="${COMP_WORDS[0]}" local command="${COMP_WORDS[0]}"
if [[ "${prev}" = "${command}" ]]; then if [[ "${prev}" = "${command}" ]]; then
COMPREPLY=( $(compgen -W "$(ls *_*.tar.*)" -- ${cur}) ) COMPREPLY=( $(compgen -W "$(ls | grep -E ${_ARCHIVE_PATTERN})" -- ${cur}) )
return 0 return 0
else else
local name="${prev%_*}" local name="${prev%_*}"
@ -226,5 +228,10 @@ _archive_name()
fi fi
} }
complete -f -X "!*_*.tar.*" archive_check unarchive _archive_grep()
{
_autocomplete_grep ${_ARCHIVE_PATTERN}
}
complete -F _archive_grep archive_check unarchive
complete -F _archive_name archive_name complete -F _archive_name archive_name

View file

@ -32,6 +32,19 @@ _autocomplete_first()
fi fi
} }
_autocomplete_grep()
{
COMPREPLY=()
local pattern="${1}"
local cur="${COMP_WORDS[COMP_CWORD]}"
local prev="${COMP_WORDS[COMP_CWORD-1]}"
local command="${COMP_WORDS[0]}"
COMPREPLY=( $(compgen -W "$(ls | grep -E ${pattern})" -- ${cur}) )
return 0
}
# autocomplete nested program. # autocomplete nested program.
_autocomplete_nested() _autocomplete_nested()
{ {