Skip to content

Commit

Permalink
;bin:justfile: make more chooser friendly, add help/pick/view
Browse files Browse the repository at this point in the history
  • Loading branch information
simonmichael committed Jul 7, 2023
1 parent 245178c commit 969dea7
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 23 deletions.
1 change: 1 addition & 0 deletions bin/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,7 @@ Add hledger options to customise reports.
<https://github.com/casey/just> is like [make](https://en.wikipedia.org/wiki/Make_(software)), but easier and more suitable for running commands.
It is a nice tool for organising financial reports and scripts!
More on [hledger and just](just.md).
Here is a [justfile](https://github.com/simonmichael/hledger/blob/master/bin/justfile)
reimplementing the `ft` and `tt` scripts more simply:
Expand Down
61 changes: 38 additions & 23 deletions bin/justfile
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,33 @@
# * financial reports/scripts, runnable with https://github.com/casey/just
# (like make but simpler and more suitable for running commands.)
# ** PREAMBLE ------------------------------------------------------------

PERIOD := "1/1..tomorrow"
TODAY := `date +%Y-%m-%d`
# XXX we don't quote HLEDGERARGS properly, so each one must be free of spaces

just := "just -f " + justfile()
@_help:
{{just}} -lu --list-heading=$'{{ file_name(justfile()) }} commands:\n'

# list the commands available
@help:
{{just}} -lu --list-heading=$'{{ file_name(justfile()) }} commands:\n\
HLEDGERARGS can be added to customise reports.\n\
NOTCHOOSABLE is a required dummy argument, write - for it. Eg: just browse -\n\
'

# interactively pick a command with the default chooser. Eg: just pick -
pick NOTCHOOSABLE:
{{just}} --choose

# interactively view command outputs with fzf and bkt. Eg: just view - --black
view NOTCHOOSABLE *FZFARGS:
{{just}} --choose --chooser="fzf --reverse --preview='bkt --ttl=15m --stale=15s -- just {}' {{FZFARGS}}"

# rerun the given command with watchexec whenever local files change
watch CMD:
watchexec -- {{just}} {{CMD}}

# XXX HLEDGERARGS are not quoted properly; each one should be free of spaces
watchexec -- {{just}} {{CMD}}

# ** IMPORT ------------------------------------------------------------

TODAY := `date +%Y-%m-%d`

# where to import most hledger transactions from
IMPORTFILES := '\
wf-bchecking.csv.rules \
Expand All @@ -29,21 +40,21 @@ IMPORTFILES := '\
'

# download auto-downloadable CSVs (paypal)
@get-csv:
@get-csv NOTCHOOSABLE:
paypaljson | paypaljson2csv > paypal.csv

# import new downloaded transactions to the main journal, dry run
@import-dry:
hledger import --dry-run {{IMPORTFILES}}

# import new downloaded transactions to the journal, logging and not printing errors
@import:
@import NOTCHOOSABLE:
date >>import.log
@hledger import {{IMPORTFILES}} 2>>import.log || echo "Failed, check import.log"
echo "Now use ledger-mode's M-q to align entries."

# show prices for main commodities (default: today's)
@get-prices *PRICEHISTFETCHOPTS :
@get-prices NOTCHOOSABLE *PRICEHISTFETCHOPTS :
(pricehist fetch -o ledger -s {{TODAY}} alphavantage EUR/USD {{PRICEHISTFETCHOPTS}} | sed -E 's/EUR/€/') &
(pricehist fetch -o ledger -s {{TODAY}} alphavantage GBP/USD {{PRICEHISTFETCHOPTS}} | sed -E 's/GBP/£/') &
(pricehist fetch -o ledger -s {{TODAY}} alphavantage JPY/USD {{PRICEHISTFETCHOPTS}} | sed -E 's/JPY/¥/')
Expand All @@ -52,6 +63,8 @@ IMPORTFILES := '\

# ** REPORTS ------------------------------------------------------------

PERIOD := "1/1..tomorrow"

# show balance sheet
bs *HLEDGERARGS :
hledger bs --layout bare --pretty --drop 1 -p {{PERIOD}} -E -5 {{HLEDGERARGS}}
Expand Down Expand Up @@ -106,18 +119,18 @@ forecast *HLEDGERARGS :

# show a draft month-end household adjustment transaction for last month
household *HLEDGERARGS :
env household "$($date -v-1m +%b)"
env household "$($date -v-1m +%b)"

# show consulting revenue
consulting *HLEDGERARGS :
hledger reg --invert 'revenues:(cw|ah)' -p {{PERIOD}} {{HLEDGERARGS}}

# estimated-tax *HLEDGERARGS :
# @echo "Federal estimated tax due for this year"
# $(HLEDGER) register liabilities:personal:tax:federal:$(YEAR) --width=130
# @echo State estimated tax due for this year:
# @$(HLEDGER) register liabilities:personal:tax:state:$(YEAR) --width=130
# @echo
# @echo "Federal estimated tax due for this year"
# $(HLEDGER) register liabilities:personal:tax:federal:$(YEAR) --width=130
# @echo State estimated tax due for this year:
# @$(HLEDGER) register liabilities:personal:tax:state:$(YEAR) --width=130
# @echo

# ** TIME REPORTS ------------------------------------------------------------

Expand All @@ -135,13 +148,15 @@ TIMELOGDATA := 'time-' + YEAR + '.timedot'
# (This is better than touching the timelog file itself, which confuses editors.)
#
# show time dashboard, redisplaying when timelog files change
tdash *HLEDGERARGS:
tdash NOTCHOOSABLE *HLEDGERARGS:
#!/usr/bin/env bash
set -euo pipefail
dir=$(dirname "$TIMELOG")
cd "$dir"
opts= #--poll=10 # <- uncomment to fix symlinked files being ignored
watchexec $opts --no-vcs-ignore --filter-file=<(hledger -f "$TIMELOG" files | sed -E "s|$dir/||g") -c -r {{just}} tstatus
opts= #--poll=10 # <- uncomment to fix symlinked files being ignored
watchexec $opts --no-vcs-ignore \
--filter-file=<(hledger -f "$TIMELOG" files | sed -E "s|$dir/||g") \
-c -r {{just}} tstatus {{HLEDGERARGS}}
# show time dashboard, redisplaying every minute with watch
# dash-1m *HLEDGERARGS:
Expand Down Expand Up @@ -169,9 +184,9 @@ tstatus *HLEDGERARGS:
# Show the current day/week/month budget status.
printf "Time plans:\n"
# calculate each period's budget from daily budget
hledger -f "$TIMELOG" bal -1 -p 'daily today' --budget=Daily | tail +2
hledger -f "$TIMELOG" bal -1 -p 'weekly this week' --budget=Daily | tail +2
hledger -f "$TIMELOG" bal -1 -p 'monthly this month' --budget=Daily | tail +2
hledger -f "$TIMELOG" bal -1 -p 'daily today' --budget=Daily {{HLEDGERARGS}} | tail +2
hledger -f "$TIMELOG" bal -1 -p 'weekly this week' --budget=Daily {{HLEDGERARGS}} | tail +2
hledger -f "$TIMELOG" bal -1 -p 'monthly this month' --budget=Daily {{HLEDGERARGS}} | tail +2
# or use each period's specific budget
# hledger -f "$TIMELOG" bal -p 'daily today' --budget=Daily -1 | tail +2
# hledger -f "$TIMELOG" bal -p 'weekly this week' --budget=Weekly -1 | tail +2
Expand Down

0 comments on commit 969dea7

Please sign in to comment.