diff options
-rw-r--r-- | .travis.yml | 4 | ||||
-rw-r--r-- | cholerab/ledger-spec.markdown | 12 | ||||
-rwxr-xr-x | ledger/lib/balance | 85 | ||||
-rwxr-xr-x | util/t/uriparse/parse-retard-uri | 8 | ||||
-rwxr-xr-x | util/t/uriparse/parse-url | 8 |
5 files changed, 106 insertions, 11 deletions
diff --git a/.travis.yml b/.travis.yml index 81eb2492..e62f843f 100644 --- a/.travis.yml +++ b/.travis.yml @@ -2,10 +2,12 @@ language: c notifications: irc: "chat.freenode.net#krebs_incoming" -script: "make -C util test | grep -q '^not ok' && exit 1" +script: "! ( make -C util test | grep -q '^not ok' )" before_install: - sudo apt-get install bc -qq - sudo apt-get install python -qq branches: only: - master +git: + submodules: false diff --git a/cholerab/ledger-spec.markdown b/cholerab/ledger-spec.markdown index 15a290bf..812bce33 100644 --- a/cholerab/ledger-spec.markdown +++ b/cholerab/ledger-spec.markdown @@ -1,8 +1,16 @@ -# Format +# Ledger Specification + +## Transaction Record + +### External Representation DATE DESTINATION-ACCOUNT SOURCE-ACCOUNT AMOUNT UNIT [COMMENT...] -# Example + where + - `DATE` has the form `YYYY-MM-DD` + - `AMOUNT` is a non-negative, decimal number + +### Example 2013-01-01 krebs-ml amazon 30 EUR C0DE-AAAA-BBBB-CCCC 2013-02-02 momo krebs-ml 50 EUR C0DE-AAAA-BBBB-CCCC diff --git a/ledger/lib/balance b/ledger/lib/balance new file mode 100755 index 00000000..77cb7f6d --- /dev/null +++ b/ledger/lib/balance @@ -0,0 +1,85 @@ +#! /bin/awk -f +# +# ledger balance calculator +# +# usage: +# [colorize=false] [scale=N] //ledger/lib/balance LEDGER_FILE... +# [colorize=false] [scale=N] //ledger/lib/balance < LEDGER_FILE +# +# description: +# The ledger balance calculator computes the balance of each account it +# encounters in the provided ledger files. +# +# example: +# //ledger/lib/balance < //cholerab/ledger-spec.markdown +# +# see also: +# //cholerab/ledger-spec.markdown (ledger file format) +# + +BEGIN { + colorize = ENVIRON["colorize"] == "" || ENVIRON["colorize"] == "true" + # TODO use bc for arbitrary precision arithmetic + scale = ENVIRON["scale"] +} + +/^[[:space:]]*[0-9]+-[0-9][0-9]-[0-9][0-9]/{ + tx($2, $3, $4, $5) +} + +END { + display_accounts() +} + +function tx (dst, src, amt, u) { + withdraw(src, amt, u) + deposit(dst, amt, u) +} + +function deposit (name, amt, u) { + accounts[name][u] += amt +} + +function withdraw (name, amt, u) { + accounts[name][u] -= amt +} + +function display_accounts() { + max_name_len = 0 + for (name in accounts) { + if (length(name) > max_name_len) { + max_name_len = length(name) + } + } + + max_balance_len = 0 + for (name in accounts) { + for (u in accounts[name]) { + n = length(int(accounts[name][u])) + if (n > max_balance_len) { + max_balance_len = n + } + } + } + if (scale > 0) { + max_balance_len += length(".") + scale + } + + for (name in accounts) { + for (u in accounts[name]) { + + fmt = "NAME BALANCE UNIT\n" + balance = accounts[name][u] + + if (colorize) { + sub("BALANCE", "[" (balance < 0 ? 31 : 32) "m&[m", fmt) + } + + sub("NAME", "%-" max_name_len "s", fmt) + sub("BALANCE", "%" max_balance_len "." scale "f", fmt) + sub("UNIT", "%s", fmt) + + printf fmt, name, balance, u + } + } +} diff --git a/util/t/uriparse/parse-retard-uri b/util/t/uriparse/parse-retard-uri index 0c149148..76feac41 100755 --- a/util/t/uriparse/parse-retard-uri +++ b/util/t/uriparse/parse-retard-uri @@ -5,9 +5,9 @@ trap "/bin/rm -f $tempfile" EXIT INT uriparse "http://'lolwut:\"khan@domain.tld/'''" > $tempfile . $tempfile -[ "$HOSTN" == "domain.tld" ] && \ - [ "$USERNAME" == "'lolwut" ] && \ - [ "$PASSWORD" == '"khan' ] && \ - [ "$URIPATH" == "/'''" ] +[ "$HOSTN" = "domain.tld" ] && \ + [ "$USERNAME" = "'lolwut" ] && \ + [ "$PASSWORD" = '"khan' ] && \ + [ "$URIPATH" = "/'''" ] diff --git a/util/t/uriparse/parse-url b/util/t/uriparse/parse-url index cf6f53fd..2ccf39c9 100755 --- a/util/t/uriparse/parse-url +++ b/util/t/uriparse/parse-url @@ -5,9 +5,9 @@ trap "/bin/rm -f $tempfile" EXIT INT uriparse "http://user:pass@domain.tld/path" > $tempfile . $tempfile -[ "$HOSTN" == "domain.tld" ] && \ - [ $USERNAME == "user" ] && \ - [ $PASSWORD == "pass" ] && \ - [ $URIPATH == "/path" ] +[ "$HOSTN" = "domain.tld" ] && \ + [ $USERNAME = "user" ] && \ + [ $PASSWORD = "pass" ] && \ + [ $URIPATH = "/path" ] |