diff --git a/local/bin/ladd b/local/bin/ladd index 9b44913..09a6dac 100755 --- a/local/bin/ladd +++ b/local/bin/ladd @@ -1,50 +1,54 @@ #!/bin/sh -# Copies a previous transaction to today. -# -# Select a previous transaction by fuzzy matching on the titles. This -# transaction is then copied to the end of the journal, uncleared and -# set to the current date. Finally, it opens the end of the file in -# $EDITOR. -# -# Bonus over `hledger add`: interactive matching it easier, and -# transaction formatting is retained because it's just a copy. -# -# Depends on: -# - fzf (https://github.com/junegunn/fzf) -# - ed +USAGE=" +$(basename $0) [-a add_file] query_files... -ledger="${1:-$LEDGER_FILE}" +Copies a previous transaction to today. + +Select a previous transaction by fuzzy matching on the titles. This +transaction is then copied to the end of the journal, uncleared and set +to the current date. Finally, it opens the end of the file in $EDITOR. + +Bonus over 'hledger add': interactive matching it easier, and +transaction formatting is retained because it's just a copy. + + add_file The transaction is appended to this file. + Default (if set): \$LEDGER_ADD_FILE + Default (otherwise): \$LEDGER_FILE + + query_files A list of files to query for previous transactions. + Default: \$LEDGER_FILE + +Depends on: +- fzf (https://github.com/junegunn/fzf) +- sponge (https://joeyh.name/code/moreutils/) + +If you split files by year, it may be useful to alias (in 2020): + + alias ladd='ladd -a .../2020/main.journal .../*/main.journal' +" + +add_file="${LEDGER_ADD_FILE:-$LEDGER_FILE}" +while getopts a: f; do + case "$f" in + a) add_file="$OPTARG";; + \?) echo "$USAGE"; exit 1;; + esac +done +shift $((OPTIND - 1)) # fuzzy select a transaction title -selection="$(sed -n 's/....-..-..\( [*!]\)\? //p' "$ledger" | sort | uniq | fzf)" -[ -z "$selection" ] && clear && exit 0 # quit if escaped +selection="$(sed -n 's/....-..-..\( [*!]\)\? //p' "${@:-$LEDGER_FILE}" | sort | uniq | fzf)" +[ -z "$selection" ] && exit 0 # quit if escaped -cat </dev/null; then fi # arrange history -HISTFILE= +HISTFILE=~/.yash_history HISTSIZE=5000 HISTRMDUP=5000 -histstart=$(mktemp) -history -r "$HOME/.yash_history" -history -w "$histstart" -trap "history -w - | diff --old-line-format='' '$histstart' - > '$HOME/.yash_history' && rm '$histstart'" EXIT # Get ourselves a nice prompt ks() { s="$?" ; $* ; return "$s" ; } @@ -77,7 +73,8 @@ alias unlock='gpg-connect-agent <<