From 740d53bc517f7718a4373c9d16b66a8f204039f0 Mon Sep 17 00:00:00 2001 From: Federico Angelilli Date: Sun, 12 Nov 2023 17:13:31 +0100 Subject: Add bash completion --- compl/crypt.bash | 53 +++++++++++++++++++++++++++++++++++++++++++++++++++++ crypt.sh | 2 ++ 2 files changed, 55 insertions(+) create mode 100755 compl/crypt.bash diff --git a/compl/crypt.bash b/compl/crypt.bash new file mode 100755 index 0000000..8631050 --- /dev/null +++ b/compl/crypt.bash @@ -0,0 +1,53 @@ +# Bash completion +# +# Copy to /usr/share/bash-completion/completions/crypt + +_crypt() { + COMPREPLY=() + + local location="${CRYPT_LOCATION:-$HOME/.crypt}" + location="${location%/}/" + local cur="${COMP_WORDS[COMP_CWORD]}" + + local IFS=$'\n' + local suffix=".gpg" + + local firstitem="" + local i=0 item + local items=($(compgen -f $location$cur)) + + for item in ${items[@]}; do + [[ $item =~ /\.[^/]*$ ]] && continue + + if [[ ${#items[@]} -eq 1 ]]; then + while [[ -d $item ]]; do + local subitems=($(compgen -f "$item/")) + local filtereditems=( ) item2 + for item2 in "${subitems[@]}"; do + [[ $item2 =~ /\.[^/]*$ ]] && continue + filtereditems+=( "$item2" ) + done + if [[ ${#filtereditems[@]} -eq 1 ]]; then + item="${filtereditems[0]}" + else + break + fi + done + fi + + [[ -d $item ]] && item="$item/" + + item="${item%$suffix}" + COMPREPLY+=("${item#$location}") + if [[ $i -eq 0 ]]; then + firstitem=$item + fi + let i+=1 + done + + if [[ $i -gt 1 || ( $i -eq 1 && -d $firstitem ) ]]; then + compopt -o nospace + fi +} + +complete -o filenames -F _crypt crypt diff --git a/crypt.sh b/crypt.sh index c876bcd..b74792f 100755 --- a/crypt.sh +++ b/crypt.sh @@ -1,4 +1,6 @@ #!/usr/bin/env bash +# +# Copy to /usr/bin/crypt umask 077 set -o pipefail -- cgit v1.2.3