From 25e8685790014d680846b1f216e5ae0125492586 Mon Sep 17 00:00:00 2001 From: desktop Date: Mon, 30 Oct 2023 14:22:24 +0300 Subject: [PATCH] bash : archive : improve completion. --- .README.md | 1 + .linux/bash/module/archive.sh | 17 ++++++++++++----- .linux/bash/module/autocomplete.sh | 13 +++++++++++++ 3 files changed, 26 insertions(+), 5 deletions(-) diff --git a/.README.md b/.README.md index ba400ba..b7b8321 100644 --- a/.README.md +++ b/.README.md @@ -322,6 +322,7 @@ Command|Description ---|--- `_autocomplete `|Provide completion based on provided arguments separated by spaces. `_autocomplete_first `|Same as `_autocomplete` but works only once for first argument. +`_autocomplete_grep `|Ls autocomplete with specified grep filter. `_autocomplete_nested`|Provides nested completions, just like `sudo` does. ## Battery. diff --git a/.linux/bash/module/archive.sh b/.linux/bash/module/archive.sh index ff13547..f2e3cfc 100644 --- a/.linux/bash/module/archive.sh +++ b/.linux/bash/module/archive.sh @@ -1,3 +1,5 @@ +_ARCHIVE_PATTERN="_[[:alnum:]]{40}.tar.[xg]z" + # archive file with maximum compression and checksum. # usage: archive [FILES] archive() @@ -71,7 +73,7 @@ archive_check() # set dafult value to target all supported archives. if [[ "${targets}" = "" ]]; then - targets=(*_*.tar.*) + targets=(${_ARCHIVE_PATTERN}) total=${#targets[@]} fi @@ -122,7 +124,7 @@ unarchive() # set dafult value to target all supported archives. if [[ "${targets}" = "" ]]; then - targets=(*_*.tar.*) + targets=(${_ARCHIVE_PATTERN}) total=${#targets[@]} fi @@ -162,7 +164,7 @@ archive_name() # set dafult value to target all supported archives. if [[ "${targets}" = "" ]]; then - targets=(*_*.tar.*) + targets=(${_ARCHIVE_PATTERN}) total=${#targets[@]} fi @@ -217,7 +219,7 @@ _archive_name() local command="${COMP_WORDS[0]}" if [[ "${prev}" = "${command}" ]]; then - COMPREPLY=( $(compgen -W "$(ls *_*.tar.*)" -- ${cur}) ) + COMPREPLY=( $(compgen -W "$(ls | grep -E ${_ARCHIVE_PATTERN})" -- ${cur}) ) return 0 else local name="${prev%_*}" @@ -226,5 +228,10 @@ _archive_name() 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 diff --git a/.linux/bash/module/autocomplete.sh b/.linux/bash/module/autocomplete.sh index b03238c..6c6cc11 100644 --- a/.linux/bash/module/autocomplete.sh +++ b/.linux/bash/module/autocomplete.sh @@ -32,6 +32,19 @@ _autocomplete_first() 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() {