;bin: add hledger-dc & hledger-timedothm, based on today's #hledger chats

Examples of preprocessing input and postprocessing output
to test new input syntax or output formats.
This commit is contained in:
Simon Michael 2025-09-27 09:21:31 -10:00
parent a107ca5cce
commit f09ffe9b20
3 changed files with 88 additions and 0 deletions

View File

@ -312,6 +312,16 @@ $ hledger pijul status
$ hledger pijul record [MSG]
```
### hledger-dc
[`hledger-dc`](https://github.com/simonmichael/hledger/blob/master/bin/hledger-dc)
reads journal files which use a Dr/Cr notation instead of/in addition to amount signs:
```journal
2025-01-01 salary
Cr revenues 800 USD
Dr assets
```
### hledger-edit
The [hledger-utils python package](https://pypi.org/project/hledger-utils/) provides
@ -375,6 +385,26 @@ $ hledger lots list
[hledger-report1.sh](https://github.com/simonmichael/hledger/blob/master/bin/hledger-report1.sh)
is a custom compound report done in shell. See also hledger-report1.hs.
### hledger-timedothm
[`hledger-timedothm`](https://github.com/simonmichael/hledger/blob/master/bin/hledger-timedothm)
reads timedot files which support HOURS:MINUTES notation:
```timedot
2025-09-27
time ..
time 0.5
time 30m
time 0:30 ; new H:M syntax
```
and it displays amounts in that notation:
```cli
$ hledger timedothm sample.timedothm reg -w80
2025-09-27 (time) 0:30 0:30
(time) 0:30 1:00
(time) 0:30 1:30
(time) 0:30 2:00
```
## hledger haskell scripts

30
bin/hledger-dc Executable file
View File

@ -0,0 +1,30 @@
#!/usr/bin/env bash
# hledger-dc JOURNALFILE CMD [ARGS]
# run a hledger command on a journal file which also supports Dr/Cr before the account name,
# instead of/in addition to amount signs.
# If a posting begins with Dr<WHITESPACE>, remove that.
# If it begins with Cr<WHITESPACE> and contains no number, remove that.
# If it begins with Cr<WHITESPACE>, remove that and add a minus sign before the next number, and if that leaves a double minus, cancel those out.
dc2sign() { sed -E -e 's/^(\s+)Dr\s+/\1/' -e 's/^(\s+)Cr\s+([^0-9]+)$/\1\2/' -e 's/^(\s+)Cr\s+([^0-9]+)([0-9])/\1\2-\3/' -e 's/--([0-9])/\1/'; }
dc2sign <"$1" | hledger -f- "${@:2}"
# Example:
#
# $ cat sample.journaldc
# 2025-01-01 salary
# Cr revenues 800 USD
# Dr assets
# $ hledger -f sample.journaldc print
# 2025-01-01 salary
# Cr revenues 800 USD
# Dr assets
# $ hledger dc sample.journaldc print
# 2025-01-01 salary
# revenues -800 USD
# assets

28
bin/hledger-timedothm Executable file
View File

@ -0,0 +1,28 @@
#!/usr/bin/env bash
# hledger-timedothm TIMEDOTHMFILE CMD [ARGS]
# run a hledger command on a timedot file which also supports H:M syntax,
# and report all time amounts in H:M format.
# convert H:M to decimal numbers
hm2dec() { perl -pe 's#(\d+):(\d{2})#sprintf("%.2f", $1 + ($2 / 60))#ge'; }
# convert decimal numbers to H:M
dec2hm() { perl -pe 's/(\d+\.\d+)/sprintf("%d:%02d", int($1), ($1 - int($1)) * 60 + 0.5)/ge'; }
hm2dec <"$1" | hledger -f timedot:- "${@:2}" | dec2hm
# Example:
#
# $ cat sample.timedothm
# 2025-09-27
# time ..
# time 0.5
# time 30m
# time 0:30 ; new H:M syntax
#
# $ hledger timedothm sample.timedothm reg -w80
# 2025-09-27 (time) 0:30 0:30
# (time) 0:30 1:00
# (time) 0:30 1:30
# (time) 0:30 2:00