From eb75946e64e01588203e8cd6cfe74102e89c0a78 Mon Sep 17 00:00:00 2001 From: Simon Michael Date: Sat, 10 Oct 2015 15:09:42 -0700 Subject: [PATCH] print: always right-align amounts print now always right-aligns the amounts in an entry, even when they are wider than 12 characters. If there is a price, it's considered part of the amount for right-alignment. Maybe it would be nicer to put amounts and prices in separate columns ? That will get a little complicated, needs more discussion/design. Also some cleanup of postingAsLines. --- hledger-lib/Hledger/Data/Transaction.hs | 26 ++++++++------ tests/i18n/wide-char-layout.test | 45 ++++++++++++++++++++----- tests/misc/commodities.test | 2 +- tests/misc/precision.test | 6 ++-- tests/misc/prices.test | 2 +- 5 files changed, 58 insertions(+), 23 deletions(-) diff --git a/hledger-lib/Hledger/Data/Transaction.hs b/hledger-lib/Hledger/Data/Transaction.hs index 06ebfcec3..ad5df6cf2 100644 --- a/hledger-lib/Hledger/Data/Transaction.hs +++ b/hledger-lib/Hledger/Data/Transaction.hs @@ -175,19 +175,25 @@ postingAsLines elideamount ps p = postinglines ++ newlinecomments where - postinglines = map rstrip $ lines $ concatTopPadded [showacct p, " ", amount, samelinecomment] - amount = if elideamount then "" else showamt (pamount p) + postinglines = map rstrip $ lines $ concatTopPadded [account, " ", amount, samelinecomment] + + account = + indent $ + showstatus p ++ fitString (Just acctwidth) Nothing False True (showAccountName Nothing (ptype p) (paccount p)) + where + showstatus p = if pstatus p == Cleared then "* " else "" + acctwidth = maximum $ map (strWidth . paccount) ps + + -- currently prices are considered part of the amount string when right-aligning amounts + amount + | elideamount = "" + | otherwise = fitStringMulti (Just amtwidth) Nothing False False $ showMixedAmount $ pamount p + where + amtwidth = maximum $ 12 : map (strWidth . showMixedAmount . pamount) ps -- min. 12 for backwards compatibility + (samelinecomment, newlinecomments) = case renderCommentLines (pcomment p) of [] -> ("",[]) c:cs -> (c,cs) - showacct p = - indent $ - showstatus p ++ fitString (Just w) Nothing False True (showAccountName Nothing (ptype p) (paccount p)) - where - showstatus p = if pstatus p == Cleared then "* " else "" - w = maximum $ map (strWidth . paccount) ps - showamt = - padLeftWide 12 . showMixedAmount tests_postingAsLines = [ "postingAsLines" ~: do diff --git a/tests/i18n/wide-char-layout.test b/tests/i18n/wide-char-layout.test index 8f86da562..ffe2c3894 100644 --- a/tests/i18n/wide-char-layout.test +++ b/tests/i18n/wide-char-layout.test @@ -1,17 +1,46 @@ -# ALl output should preserve alignment etc. when showing wide characters. -# XXX For a better test, the sample journals should have commodity symbols. -# XXX How to test hledger-ui ? +# ALl output should preserve alignment etc. when showing wide characters +# (in apps and fonts which render wide chars as exactly double width). +# +# XXX add commodity symbols and prices to the sample journals +# XXX how to test hledger-ui ? # 1. -hledger -f chinese.journal print +# hledger -f chinese.journal print +# >>> +# 2014/01/01 transaction 1 +# 㐀 1 +# 㐀:㐁 -1 +# +# 2014/01/02 transaction 2 +# 㐀:㐁:㐂 1 +# 㐀:㐁:㐂:㐃 -1 +# +# 2014/01/03 transaction 3 +# 㐀:㐁:㐂:㐃:㐄 1 +# 㐀 -1 +# +# >>>=0 +hledger -f - print +<<< +2014-01-01 transaction 1 + 㐀 㐃㐃1 @ 2 㐂㐂㐂㐂㐂㐂㐂㐂㐂㐂㐂 + 㐀:㐁 ;㐃㐃-1 + +2014-01-02 transaction 2 + 㐀:㐁:㐂 USD 1 + 㐀:㐁:㐂:㐃 EUR -1 + +2014-01-03 transaction 3 + 㐀:㐁:㐂:㐃:㐄 1 + 㐀 -1 >>> 2014/01/01 transaction 1 - 㐀 1 - 㐀:㐁 -1 + 㐀 㐃㐃1 @ 2 㐂㐂㐂㐂㐂㐂㐂㐂㐂㐂㐂 + 㐀:㐁 -2 㐂㐂㐂㐂㐂㐂㐂㐂㐂㐂㐂 ; 㐃㐃-1 2014/01/02 transaction 2 - 㐀:㐁:㐂 1 - 㐀:㐁:㐂:㐃 -1 + 㐀:㐁:㐂 USD 1 @@ EUR 1 + 㐀:㐁:㐂:㐃 EUR -1 2014/01/03 transaction 3 㐀:㐁:㐂:㐃:㐄 1 diff --git a/tests/misc/commodities.test b/tests/misc/commodities.test index 850db75ee..3c7508a9e 100644 --- a/tests/misc/commodities.test +++ b/tests/misc/commodities.test @@ -15,7 +15,7 @@ hledger -f- print b >>> 2010/04/05 x - a 10 "DE 0002 635307" + a 10 "DE 0002 635307" b -10 "DE 0002 635307" >>>=0 diff --git a/tests/misc/precision.test b/tests/misc/precision.test index edc445e55..943ce2254 100644 --- a/tests/misc/precision.test +++ b/tests/misc/precision.test @@ -10,7 +10,7 @@ hledger -f - print >>> 2010/01/01 x A 55.3653 C @ 30.92189512 D - A -1712 D + A -1712 D >>>=0 @@ -140,7 +140,7 @@ hledger -f- print 2015/01/01 c C 10.00 @ D 15.2381 c C 11.00 @ D 15.2381 - d D -320.00 + d D -320.00 >>>=0 @@ -157,6 +157,6 @@ hledger -f- print 2015/01/01 e E 10.0000 @ F 15.2380952 e E 11.0000 @ F 15.2380952 - f F -320.000 + f F -320.000 >>>=0 diff --git a/tests/misc/prices.test b/tests/misc/prices.test index ae9a2ad46..aa62dca54 100644 --- a/tests/misc/prices.test +++ b/tests/misc/prices.test @@ -85,7 +85,7 @@ hledger -f - print >>> 2002/09/30 * 1a1a6305d06ce4b284dba0d267c23f69d70c20be c56a21d23a6535184e7152ee138c28974f14280c 866.231000 GGGGG @@ $17,783.72 - a35e82730cf91569c302b313780e5895f75a62b9 $-17,783.72 + a35e82730cf91569c302b313780e5895f75a62b9 $-17,783.72 >>>=0