diff --git a/.config/bash/module/Archive.sh b/.config/bash/module/Archive.sh index c2b6c86..28d1dac 100644 --- a/.config/bash/module/Archive.sh +++ b/.config/bash/module/Archive.sh @@ -124,17 +124,20 @@ function archive_check() { # Delete old versions of archives. # All archives by default. -# Usage: archive_prune [NAME] +# Usage: archive_prune [NAMES] function archive_prune() { local IFS=$'\n' - local targets=(${@}) + local targets=() + for target in ${@}; do + targets+=($(ls ${target}* | grep -E ${_archive_pattern})) || return 2 + done [[ "${targets}" = "" ]] && targets=($(ls | grep -E ${_archive_pattern})) process() { local data=($(_archive_parse ${target})) local name=${data[0]} local time=${data[1]} - local copies=($(ls ${name}_*)) + local copies=($(ls ${name}* | grep -E ${_archive_pattern})) # Iterate each copy. for copy in ${copies[@]}; do @@ -150,6 +153,27 @@ function archive_prune() { _iterate_targets process ${targets[@]} } +# Delete old versions of an archive, keeping specified amount of versions. One version by default. +# Usage: archive_prune_versions [VERSIONS] +function archive_prune_versions() { + local IFS=$'\n' + local target=${1} + local versions=${2} + + [[ "${versions}" = "" ]] && versions=1 + + if [[ "${target}" = "" ]]; then + help archive_prune_versions + return 2 + fi + + local prune=($(ls ${target}* | grep -E ${_archive_pattern} | sort -r | sed -e "1,${versions}d")) + + for archive in ${prune[@]}; do + rm -- "${archive}" && echo "Prune: ${archive}" + done +} + # Delete specified or all archive files. # Usage: archive_rm [FILES] function archive_rm() { @@ -293,7 +317,8 @@ function _archive_name() { COMPREPLY=( $(compgen -W "$(ls | grep -E ${_archive_pattern})" -- ${cur}) ) return 0 else - local name="${prev%_*}" + local data=($(_archive_parse ${prev})) + local name="${data[0]}" COMPREPLY=( $(compgen -W "${name}" -- ${cur}) ) return 0 fi @@ -314,6 +339,34 @@ function _archive_date() { date +%Y%m%d%H%M } +# Get names of all archives. +function _archive_names() { + local IFS=$'\n' + local archives=($(ls | grep -E ${_archive_pattern})) + local names=() + for archive in ${archives[@]}; do + local data=($(_archive_parse ${archive})) + local name=${data[0]} + names+=(${name}) + done + + _autocomplete ${names[@]} +} + +# Get names of all archives once. +function _archive_names_first() { + local IFS=$'\n' + local archives=($(ls | grep -E ${_archive_pattern})) + local names=() + for archive in ${archives[@]}; do + local data=($(_archive_parse ${archive})) + local name=${data[0]} + names+=(${name}) + done + + _autocomplete_first ${names[@]} +} + # Check if file is an archive. function _is_archive() { local out=$(echo "${*}" | grep -E ${_archive_pattern}) @@ -324,3 +377,5 @@ function _is_archive() { complete -o filenames -F _archive_grep archive_check unarchive archive_rm complete -o filenames -F _archive_grep_fast archive_xz complete -o filenames -F _archive_name archive_name +complete -o filenames -F _archive_names archive_prune +complete -o filenames -F _archive_names_first archive_prune_versions diff --git a/.doc/Bash.md b/.doc/Bash.md index 5e4c010..8bb8004 100644 --- a/.doc/Bash.md +++ b/.doc/Bash.md @@ -24,7 +24,8 @@ Command|Description `archive_mt [DIRS]`|Archive using multiple threads. Uses 50% of free RAM. All directories by default. Supports .archiveignore exclude file. `archive_fast [DIRS]`|Archive directories with fast compression. All directories by default. Supports .archiveignore exclude file. `archive_check [FILES]`|Check archives integrity. Checks all archives by default. -`archive_prune [NAME]`|Delete old versions of archives. All archives by default. +`archive_prune [NAMES]`|Delete old versions of archives. All archives by default. +`archive_prune_versions [VERSIONS]`|Delete old versions of an archive, keeping specified amount of versions. One version by default. `archive_rm [FILES]`|Delete specified or all archive files. `archive_xz [FILES]`|Recompress previously created archive_fast with better compression. `archive_name [ARCHIVE] [NAME]`|Rename archives. If no name specified, it simplifies archive's name. If no archives specified, apply to all archives.