|
| 1 | +#!/bin/bash |
| 2 | +# BASH COMPLETION SCRIPT FOR STANDARD_UNIX_NOTES notes(1) notebook(1) |
| 3 | +# Original PASSWORD_STORE version Copyright (C) 2012 - 2014 Jason A. Donenfeld <Jason@zx2c4.com> and |
| 4 | +# Brian Mattern <rephorm@rephorm.com>. All Rights Reserved. |
| 5 | + |
| 6 | +_notes_complete_entries () { |
| 7 | + if [ -n "$XDG_DATA_DIR" -a -z "$NOTESDIR" ] ; then |
| 8 | + # only use $XDG_DATA_DIR if NOTESDIR not set |
| 9 | + NOTESDIR="${XDG_DATA_DIR}/.notes" |
| 10 | + else |
| 11 | + # use NOTESDIR if set else default to ~/.notes |
| 12 | + NOTESDIR="${NOTESDIR:-${HOME}/.notes}" |
| 13 | + fi |
| 14 | + CONFIGFILE="${NOTESDIR}/config" |
| 15 | + INITIAL_NOTEBOOK="${NOTESDIR}/notes" |
| 16 | + DEFAULT_POINTER="${NOTESDIR}/DEFAULT" |
| 17 | + USE_POINTER="${NOTESDIR}/USE" |
| 18 | + |
| 19 | + #fixup original password store code to search inside USE_POINTER (current notebook) |
| 20 | + prefix="${USE_POINTER}" |
| 21 | + |
| 22 | + # code chunk below is fromthe original password store code |
| 23 | + prefix="${prefix%/}/" |
| 24 | + suffix=".gpg" |
| 25 | + autoexpand=${1:-0} |
| 26 | + |
| 27 | + local IFS=$'\n' |
| 28 | + local items=($(compgen -f $prefix$cur)) |
| 29 | + |
| 30 | + # Remember the value of the first item, to see if it is a directory. If |
| 31 | + # it is a directory, then don't add a space to the completion |
| 32 | + local firstitem="" |
| 33 | + # Use counter, can't use ${#items[@]} as we skip hidden directories |
| 34 | + local i=0 |
| 35 | + |
| 36 | + for item in ${items[@]}; do |
| 37 | + [[ $item =~ /\.[^/]*$ ]] && continue |
| 38 | + |
| 39 | + # if there is a unique match, and it is a directory with one entry |
| 40 | + # autocomplete the subentry as well (recursively) |
| 41 | + if [[ ${#items[@]} -eq 1 && $autoexpand -eq 1 ]]; then |
| 42 | + while [[ -d $item ]]; do |
| 43 | + local subitems=($(compgen -f "$item/")) |
| 44 | + local filtereditems=( ) |
| 45 | + for item2 in "${subitems[@]}"; do |
| 46 | + [[ $item2 =~ /\.[^/]*$ ]] && continue |
| 47 | + filtereditems+=( "$item2" ) |
| 48 | + done |
| 49 | + if [[ ${#filtereditems[@]} -eq 1 ]]; then |
| 50 | + item="${filtereditems[0]}" |
| 51 | + else |
| 52 | + break |
| 53 | + fi |
| 54 | + done |
| 55 | + fi |
| 56 | + |
| 57 | + # append / to directories |
| 58 | + [[ -d $item ]] && item="$item/" |
| 59 | + |
| 60 | + item="${item%$suffix}" |
| 61 | + COMPREPLY+=("${item#$prefix}") |
| 62 | + if [[ $i -eq 0 ]]; then |
| 63 | + firstitem=$item |
| 64 | + fi |
| 65 | + let i+=1 |
| 66 | + done |
| 67 | + |
| 68 | + # The only time we want to add a space to the end is if there is only |
| 69 | + # one match, and it is not a directory |
| 70 | + if [[ $i -gt 1 || ( $i -eq 1 && -d $firstitem ) ]]; then |
| 71 | + compopt -o nospace |
| 72 | + fi |
| 73 | +} |
| 74 | + |
| 75 | +_notebook_complete_folders () { |
| 76 | + if [ -n "$XDG_DATA_DIR" -a -z "$NOTESDIR" ] ; then |
| 77 | + # only use $XDG_DATA_DIR if NOTESDIR not set |
| 78 | + NOTESDIR="${XDG_DATA_DIR}/.notes" |
| 79 | + else |
| 80 | + # use NOTESDIR if set else default to ~/.notes |
| 81 | + NOTESDIR="${NOTESDIR:-${HOME}/.notes}" |
| 82 | + fi |
| 83 | + CONFIGFILE="${NOTESDIR}/config" |
| 84 | + INITIAL_NOTEBOOK="${NOTESDIR}/notes" |
| 85 | + DEFAULT_POINTER="${NOTESDIR}/DEFAULT" |
| 86 | + USE_POINTER="${NOTESDIR}/USE" |
| 87 | + |
| 88 | + prefix="${NOTESDIR}" |
| 89 | + prefix="${prefix%/}/" |
| 90 | + |
| 91 | + local IFS=$'\n' |
| 92 | + local items=($(compgen -d $prefix$cur)) |
| 93 | + for item in ${items[@]}; do |
| 94 | + |
| 95 | + case "$item" in |
| 96 | + "$USE_POINTER") |
| 97 | + ;; |
| 98 | + "$DEFAULT_POINTER") |
| 99 | + ;; |
| 100 | + *) |
| 101 | + #[[ $item == $prefix.* ]] && continue |
| 102 | + COMPREPLY+=("${item#$prefix}/") |
| 103 | + ;; |
| 104 | + esac |
| 105 | + done |
| 106 | +} |
| 107 | + |
| 108 | +_notes_complete_keys () { |
| 109 | + local IFS=$'\n' |
| 110 | + local GPG=gpg2 |
| 111 | + which $GPG >/dev/null || GPG=gpg |
| 112 | + # Extract names and email addresses from gpg --list-keys |
| 113 | + local keys="$($GPG --list-secret-keys --with-colons | cut -d : -f 10 | sort -u | sed '/^$/d')" |
| 114 | + COMPREPLY+=($(compgen -W "${keys}" -- ${cur})) |
| 115 | +} |
| 116 | + |
| 117 | +_notes() |
| 118 | +{ |
| 119 | + COMPREPLY=() |
| 120 | + local cur="${COMP_WORDS[COMP_CWORD]}" |
| 121 | + local commands="init config newkey help version ls list show find search add insert view cat import rename mv copy cp rm remove delete edit ${UNIX_NOTES_EXTENSION_COMMANDS[*]}" |
| 122 | + if [[ $COMP_CWORD -gt 1 ]]; then |
| 123 | + local lastarg="${COMP_WORDS[$COMP_CWORD-1]}" |
| 124 | + case "${COMP_WORDS[1]}" in |
| 125 | + view|cat|edit|insert|add|cp|copy|mv|rename|rm|remove|delete) |
| 126 | + _notes_complete_entries |
| 127 | + ;; |
| 128 | + import) |
| 129 | + ;; |
| 130 | + *) |
| 131 | + ;; |
| 132 | + esac |
| 133 | + |
| 134 | + # To add completion for an extension command define a function like this: |
| 135 | + # __unix_notes_extension_complete_<COMMAND>() { |
| 136 | + # COMPREPLY+=($(compgen -W "-o --option" -- ${cur})) |
| 137 | + # _notes_complete_entries 1 |
| 138 | + # } |
| 139 | + # |
| 140 | + # and add the command to the $UNIX_NOTES_EXTENSION_COMMANDS array |
| 141 | + if [[ " ${UNIX_NOTES_EXTENSION_COMMANDS[*]} " == *" ${COMP_WORDS[1]} "* ]] && type "__unix_notes_extension_complete_${COMP_WORDS[1]}" &> /dev/null; then |
| 142 | + "__unix_notes_extension_complete_${COMP_WORDS[1]}" |
| 143 | + fi |
| 144 | + else |
| 145 | + COMPREPLY+=($(compgen -W "${commands}" -- ${cur})) |
| 146 | + _notes_complete_entries 1 |
| 147 | + fi |
| 148 | +} |
| 149 | + |
| 150 | +_notebook() |
| 151 | +{ |
| 152 | + COMPREPLY=() |
| 153 | + local cur="${COMP_WORDS[COMP_CWORD]}" |
| 154 | + local commands="list ls show default use add insert rename mv copy cp delete rm remove ${UNIX_NOTES_EXTENSION_COMMANDS[*]}" |
| 155 | + if [[ $COMP_CWORD -gt 1 ]]; then |
| 156 | + local lastarg="${COMP_WORDS[$COMP_CWORD-1]}" |
| 157 | + case "${COMP_WORDS[1]}" in |
| 158 | + use|default|cp|copy|mv|rename|rm|remove|delete) |
| 159 | + _notebook_complete_folders |
| 160 | + ;; |
| 161 | + esac |
| 162 | + |
| 163 | + if [[ " ${UNIX_NOTES_EXTENSION_COMMANDS[*]} " == *" ${COMP_WORDS[1]} "* ]] && type "__unix_notes_extension_complete_${COMP_WORDS[1]}" &> /dev/null; then |
| 164 | + "__notes_extension_complete_${COMP_WORDS[1]}" |
| 165 | + fi |
| 166 | + else |
| 167 | + COMPREPLY+=($(compgen -W "${commands}" -- ${cur})) |
| 168 | + _notes_complete_entries 1 |
| 169 | + fi |
| 170 | +} |
| 171 | + |
| 172 | +complete -o filenames -F _notes notes |
| 173 | +complete -o filenames -F _notebook notebook |
| 174 | + |
0 commit comments