configuration/local/bin/ladd

62 lines
2.0 KiB
Plaintext
Raw Permalink Normal View History

#!/bin/sh
USAGE="
$(basename $0) [-a add_file] query_files...
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.
2024-07-24 08:35:41 +02:00
add_file The transaction is appended to this file. Using -
will output the transaction to stdout instead.
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:
2020-12-04 14:49:01 +01:00
- skim (https://github.com/lotabout/skim)
- 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))
2019-11-16 19:27:53 +01:00
# fuzzy select a transaction title
2024-07-24 08:35:41 +02:00
selection="$(sed -n 's/....-..-..\( [*!]\)\? //p' "${@:-$LEDGER_FILE}" | sort | uniq | sk | sed 's_/_\\/_g')"
[ -z "$selection" ] && exit 0 # quit if escaped
sed -n "/....-..-..\( [*!]\)\? ${selection}\$/,/^\$/p" "${@:-$LEDGER_FILE}" | # print matching transactions
sed '${/^$/d}' | # remove trailing empty line
sed -n '/^$/{s/.*//;x;d};H;${x;p}' | # remove all before the last empty line
2020-04-15 12:34:59 +02:00
sed "s/^....-..-..\( [*!]\)\?/$(date +'%Y-%m-%d')/" | # replace date with today, remove marker
2024-07-24 08:35:41 +02:00
if [ "$add_file" = '-' ]; then
cat
else
sponge -a "$add_file" # append to the file, use sponge in case it's also an input file
fi
2019-11-16 19:50:27 +01:00
# review change: open end of the file (if I know how)
2024-07-24 08:35:41 +02:00
if [ "$add_file" != '-' ]; then
case "$EDITOR" in
vi*|nvi*) "$EDITOR" +\$ "$add_file" ;;
emacs) "$EDITOR" "$add_file" -f end-of-buffer ;;
*) "$EDITOR" "$add_file" ;;
esac
fi