From f09ffe9b20c85ea2a4eec536ed8e068ede46b178 Mon Sep 17 00:00:00 2001 From: Simon Michael Date: Sat, 27 Sep 2025 09:21:31 -1000 Subject: [PATCH] ;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. --- bin/README.md | 30 ++++++++++++++++++++++++++++++ bin/hledger-dc | 30 ++++++++++++++++++++++++++++++ bin/hledger-timedothm | 28 ++++++++++++++++++++++++++++ 3 files changed, 88 insertions(+) create mode 100755 bin/hledger-dc create mode 100755 bin/hledger-timedothm diff --git a/bin/README.md b/bin/README.md index 1a23ae174..419e040e4 100644 --- a/bin/README.md +++ b/bin/README.md @@ -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 diff --git a/bin/hledger-dc b/bin/hledger-dc new file mode 100755 index 000000000..7ee5c9a3f --- /dev/null +++ b/bin/hledger-dc @@ -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, remove that. +# If it begins with Cr and contains no number, remove that. +# If it begins with Cr, 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 + diff --git a/bin/hledger-timedothm b/bin/hledger-timedothm new file mode 100755 index 000000000..85197764e --- /dev/null +++ b/bin/hledger-timedothm @@ -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