Added Query function, cleaned up arg parser

This commit is contained in:
Vera Lewis
2024-06-28 18:43:42 -05:00
parent 6ff62ebd19
commit 6a7655326b

51
vns
View File

@@ -143,7 +143,7 @@ vns_printHelp (){
# printHelp # printHelp
# Prints help information to stdout # Prints help information to stdout
printf "%s" "usage: vns [-cedlp] <notebook/section/name>" printf "%s" "usage: vns [-cedlpqr] <notebook/section/name>"
printf "\\n%s" " vns -I GPG_RECIPIENT (GPG_RECIPIENTS...)" printf "\\n%s" " vns -I GPG_RECIPIENT (GPG_RECIPIENTS...)"
printf "\\n%s" " vns -h" printf "\\n%s" " vns -h"
printf "\\n%s" " vns git ..." printf "\\n%s" " vns git ..."
@@ -157,6 +157,7 @@ vns_printHelp (){
printf "\\n%s" " -l : List all notes in <notebook>" printf "\\n%s" " -l : List all notes in <notebook>"
printf "\\n%s" " -m : Merge two or more notes" printf "\\n%s" " -m : Merge two or more notes"
printf "\\n%s" " -p : Print note to console" printf "\\n%s" " -p : Print note to console"
printf "\\n%s" " -q : Query notes for expression"
printf "\\n%s" " -r : Rename/move a note" printf "\\n%s" " -r : Rename/move a note"
printf "\\n\\n" printf "\\n\\n"
@@ -430,6 +431,35 @@ vns_duplicate () {
vns_git add "$VNS_STORE/$2.gpg" vns_git add "$VNS_STORE/$2.gpg"
} }
vns_query () {
# query EXPR
# Iterate through notes and find all instances of regular expression EXPR
if [ "$#" -lt 1 ]; then
vns_report "usage: $(basename "$0") -q EXPR"
return
fi
local -r VNS_ULNE="$(tput setaf smul)"
local -r VNS_="$(tput setaf rmul)"
# For each note, grep for QUERY
find "$VNS_STORE" -name "*.gpg" -not -name ".*" | while read -r note; do
# Remove VNS_STORE path and file extension for reporting
note_name="$(sed -e "s#$VNS_STORE/##" -e "s/.gpg//" -e "s#:#\\:#g" <<< "$note")"
# Decrypt note and perform the actual query
local query="$(gpg -d "$note" 2>/dev/null | grep -C 1 --color=always -nE "$1" )"
# Report only matches
if [ -n "${query:-}" ]; then
printf "$(tput smul)%s$(tput rmul)\n" "$note_name"
printf "%s\n" "$query"
fi
done
}
vns_git () { vns_git () {
# Simple passthrough for executing git commands in the VNS store # Simple passthrough for executing git commands in the VNS store
git -C "$VNS_STORE" "$@" git -C "$VNS_STORE" "$@"
@@ -441,53 +471,46 @@ vns () {
vns_sanityCheck; vns_sanityCheck;
fi fi
case "$1" in primary_arg="$1"
"-c")
shift shift
case "$primary_arg" in
"-c")
vns_create "$@" vns_create "$@"
;; ;;
"-d") "-d")
shift
vns_rm "$@" vns_rm "$@"
;; ;;
"-e") "-e")
shift
vns_edit "$@" vns_edit "$@"
;; ;;
"-l") "-l")
shift
vns_list "$@" vns_list "$@"
;; ;;
"-h") "-h")
shift
vns_printHelp "$@" vns_printHelp "$@"
;; ;;
"-I") "-I")
shift
vns_init "$@" vns_init "$@"
;; ;;
"-i") "-i")
shift
vns_import "$@" vns_import "$@"
;; ;;
"-m") "-m")
shift
vns_merge "$@" vns_merge "$@"
;; ;;
"-p") "-p")
shift
vns_print "$@" vns_print "$@"
;; ;;
"-q")
vns_query "$@"
;;
"-r") "-r")
shift
vns_mv "$@" vns_mv "$@"
;; ;;
"-u") "-u")
shift
vns_duplicate "$@" vns_duplicate "$@"
;; ;;
"git") "git")
shift
vns_git "$@" vns_git "$@"
;; ;;
*) *)