pashage.bash (3814B)
1 # completion file for bash 2 3 # Copyright (C) 2025 Natasha Kerensikova, based on password-store completion: 4 # Copyright (C) 2012 - 2014 Jason A. Donenfeld <Jason@zx2c4.com> and 5 # Brian Mattern <rephorm@rephorm.com>. All Rights Reserved. 6 # This file is licensed under the GPLv2+. Please see COPYING for more information. 7 8 _pashage_complete_entries () { 9 local prefix="${PASHAGE_DIR:-${PASSAGE_DIR:-${PASSWORD_STORE_DIR:-${HOME}/.passage/store}}}" 10 prefix="${prefix%/}/" 11 local suffix=".age" 12 local autoexpand=${1:-0} 13 14 local IFS=$'\n' 15 local items=($(compgen -f $prefix$cur)) 16 17 # Remember the value of the first item, to see if it is a directory. If 18 # it is a directory, then don't add a space to the completion 19 local firstitem="" 20 # Use counter, can't use ${#items[@]} as we skip hidden directories 21 local i=0 item 22 23 for item in ${items[@]}; do 24 [[ $item =~ /\.[^/]*$ ]] && continue 25 26 # if there is a unique match, and it is a directory with one entry 27 # autocomplete the subentry as well (recursively) 28 if [[ ${#items[@]} -eq 1 && $autoexpand -eq 1 ]]; then 29 while [[ -d $item ]]; do 30 local subitems=($(compgen -f "$item/")) 31 local filtereditems=( ) item2 32 for item2 in "${subitems[@]}"; do 33 [[ $item2 =~ /\.[^/]*$ ]] && continue 34 filtereditems+=( "$item2" ) 35 done 36 if [[ ${#filtereditems[@]} -eq 1 ]]; then 37 item="${filtereditems[0]}" 38 else 39 break 40 fi 41 done 42 fi 43 44 # append / to directories 45 [[ -d $item ]] && item="$item/" 46 47 item="${item%$suffix}" 48 COMPREPLY+=("${item#$prefix}") 49 if [[ $i -eq 0 ]]; then 50 firstitem=$item 51 fi 52 let i+=1 53 done 54 55 # The only time we want to add a space to the end is if there is only 56 # one match, and it is not a directory 57 if [[ $i -gt 1 || ( $i -eq 1 && -d $firstitem ) ]]; then 58 compopt -o nospace 59 fi 60 } 61 62 _pashage_complete_folders () { 63 local prefix="${PASHAGE_DIR:-${PASSAGE_DIR:-${PASSWORD_STORE_DIR:-${HOME}/.passage/store}}}" 64 prefix="${prefix%/}/" 65 66 local IFS=$'\n' 67 local items=($(compgen -d $prefix$cur)) 68 for item in ${items[@]}; do 69 [[ $item == $prefix.* ]] && continue 70 COMPREPLY+=("${item#$prefix}/") 71 done 72 } 73 74 _pashage() 75 { 76 COMPREPLY=() 77 local cur="${COMP_WORDS[COMP_CWORD]}" 78 local commands="copy cp delete edit find generate git gitconfig grep help init insert list ls move mv random re-encrypt reencrypt remove rm show version" 79 if [[ $COMP_CWORD -gt 1 ]]; then 80 local lastarg="${COMP_WORDS[$COMP_CWORD-1]}" 81 case "${COMP_WORDS[1]}" in 82 init) 83 if [[ $lastarg == "-p" || $lastarg == "--path" ]]; then 84 _pashage_complete_folders 85 compopt -o nospace 86 else 87 COMPREPLY+=($(compgen -W "-i --interactive -k --keep -p --path" -- ${cur})) 88 fi 89 ;; 90 ls|list|edit) 91 _pashage_complete_entries 92 ;; 93 show|-*) 94 COMPREPLY+=($(compgen -W "-c --clip -q --qrcode" -- ${cur})) 95 _pashage_complete_entries 1 96 ;; 97 insert) 98 COMPREPLY+=($(compgen -W "-e --echo -m --multiline -f --force" -- ${cur})) 99 _pashage_complete_entries 100 ;; 101 generate) 102 COMPREPLY+=($(compgen -W "-n --no-symbols -c --clip -f --force -i --in-place -m --multiline -q --qrcode -t --try" -- ${cur})) 103 _pashage_complete_entries 104 ;; 105 cp|copy|move|mv|rename) 106 COMPREPLY+=($(compgen -W "-f --force -e --reencrypt -i --interactive -k --keep" -- ${cur})) 107 _pashage_complete_entries 108 ;; 109 rm|remove|delete) 110 COMPREPLY+=($(compgen -W "-r --recursive -f --force" -- ${cur})) 111 _pashage_complete_entries 112 ;; 113 re-encrypt|reencrypt) 114 COMPREPLY+=($(compgen -W "-d --deep -i --interactive" -- ${cur})) 115 _pashage_complete_entries 116 ;; 117 git) 118 COMPREPLY+=($(compgen -W "init push pull config log reflog rebase" -- ${cur})) 119 ;; 120 esac 121 else 122 COMPREPLY+=($(compgen -W "${commands}" -- ${cur})) 123 _pashage_complete_entries 1 124 fi 125 } 126 127 complete -o filenames -F _pashage pashage