Rewrite ladd to allow multiple input files

This commit is contained in:
Felix Van der Jeugt 2020-04-15 00:29:26 +02:00
parent 6817791c1b
commit 36e72b06d1
No known key found for this signature in database
GPG Key ID: 58B209295023754D
3 changed files with 49 additions and 48 deletions

View File

@ -1,50 +1,54 @@
#!/bin/sh #!/bin/sh
# Copies a previous transaction to today. USAGE="
# $(basename $0) [-a add_file] query_files...
# 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
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 # fuzzy select a transaction title
selection="$(sed -n 's/....-..-..\( [*!]\)\? //p' "$ledger" | sort | uniq | fzf)" selection="$(sed -n 's/....-..-..\( [*!]\)\? //p' "${@:-$LEDGER_FILE}" | sort | uniq | fzf)"
[ -z "$selection" ] && clear && exit 0 # quit if escaped [ -z "$selection" ] && exit 0 # quit if escaped
cat <<HERE | ed --silent "$ledger" sed -n "/....-..-..\( [*!]\)\? ${selection}\$/,/^\$/p" "${@:-$LEDGER_FILE}" | # print matching transactions
# append a newline for easier searching sed '${/^$/d}' | # remove trailing empty line
\$a sed -n '/^$/{s/.*//;x;d};H;${x;p}' | # remove all before the last empty line
sed "s/^....-..-..\( [*!]\)\?/$(date +'%Y-%m-%d')/" | # replace date iwht today, remove marker
. sponge -a "$add_file" # append to the file, use sponge in case it's also an input file
# yank matching transaction (closed by empty line)
?^....-..-..\( [*!]\)\? ${selection}\$?;/^\$/y
# select line below last none-empty line
$
?^..*?
+
# put transaction
x
# replace date with today
?^\$?
+s/....-..-..\( [*!]\)\?/$(date +'%Y-%m-%d')/
# delete empty lines at end and quit
/^\$/,\$d
wq
HERE
# review change: open end of the file (if I know how) # review change: open end of the file (if I know how)
case "$EDITOR" in case "$EDITOR" in
vi*) "$EDITOR" +\$ "$ledger" ;; vi*) "$EDITOR" +\$ "$add_file" ;;
emacs) "$EDITOR" "$ledger" -f end-of-buffer ;; emacs) "$EDITOR" "$add_file" -f end-of-buffer ;;
*) "$EDITOR" "$ledger" ;; *) "$EDITOR" "$add_file" ;;
esac esac
clear

View File

@ -19,7 +19,7 @@ export PATH="$HOME/.local/bin:$CABAL_HOME/bin:$RBENV_HOME/shims:$CARGO_HOME/bin:
# Environmental settings # Environmental settings
export ESCDELAY=50 # for dvtm e.a. export ESCDELAY=50 # for dvtm e.a.
export EDITOR="vis" export EDITOR="vis"
export LEDGER_FILE="/data/documents/ledger/ledger.journal" export LEDGER_FILE="/data/documents/ledger/all.journal"
export LESSHISTFILE=- export LESSHISTFILE=-
export FZF_DEFAULT_COMMAND='(git ls-files -co --exclude-standard || rg --files) 2> /dev/null' export FZF_DEFAULT_COMMAND='(git ls-files -co --exclude-standard || rg --files) 2> /dev/null'
export LC_ALL=en_US.utf8 export LC_ALL=en_US.utf8

9
yashrc
View File

@ -11,13 +11,9 @@ if command --identify --builtin-command bindkey >/dev/null; then
fi fi
# arrange history # arrange history
HISTFILE= HISTFILE=~/.yash_history
HISTSIZE=5000 HISTSIZE=5000
HISTRMDUP=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 # Get ourselves a nice prompt
ks() { s="$?" ; $* ; return "$s" ; } ks() { s="$?" ; $* ; return "$s" ; }
@ -77,7 +73,8 @@ alias unlock='gpg-connect-agent <<<bye'
alias weechat="ssh -t weechat@Chatmachine abduco -A weechat" alias weechat="ssh -t weechat@Chatmachine abduco -A weechat"
alias agenda="khal list today 7d" alias agenda="khal list today 7d"
alias haccounts="hledger -I --alias '/.*:Accounts .*:(.*)/=Accounts:\1' bal accounts" alias haccounts="hledger -I --alias '/.*:Accounts [^:]*:(.*)/=Accounts:\1' bal accounts"
alias ladd='ladd -a /data/documents/ledger/2020/main.journal /data/documents/ledger/*/main.journal'
go() { go() {
dir="$(lr /home /data /etc -t '(name ~~ ".*" && prune || print) && type = d && !(name = ".git")' \ dir="$(lr /home /data /etc -t '(name ~~ ".*" && prune || print) && type = d && !(name = ".git")' \