cli: Ensure register reports are correctly aligned for negative numbers.

This uses the new showMixed* function for the register report. This
fixes some misaligned negative numbers which appeared in one of the
earlier commits, and adds a test for it.
This commit is contained in:
Stephen Morgan 2020-09-21 15:35:42 +10:00
parent 162a936360
commit a838366c9d
2 changed files with 28 additions and 11 deletions

View File

@ -92,8 +92,8 @@ postingsReportItemAsCsvRecord (_, _, _, p, b) = [idx,date,code,desc,acct,amt,bal
postingsReportAsText :: CliOpts -> PostingsReport -> String
postingsReportAsText opts (_,items) = unlines $ map (postingsReportItemAsText opts amtwidth balwidth) items
where
amtwidth = maximumStrict $ 12 : map (strWidth . showMixedAmount . itemamt) items
balwidth = maximumStrict $ 12 : map (strWidth . showMixedAmount . itembal) items
amtwidth = maximumStrict $ map (snd . showMixed showAmount (Just 12) Nothing False . itemamt) items
balwidth = maximumStrict $ map (snd . showMixed showAmount (Just 12) Nothing False . itembal) items
itemamt (_,_,_,Posting{pamount=a},_) = a
itembal (_,_,_,_,a) = a
@ -130,15 +130,15 @@ postingsReportItemAsText opts preferredamtwidth preferredbalwidth (mdate, mendda
," "
,fitString (Just acctwidth) (Just acctwidth) True True acct
," "
,fitString (Just amtwidth) (Just amtwidth) True False amtfirstline
,amtfirstline
," "
,fitString (Just balwidth) (Just balwidth) True False balfirstline
,balfirstline
]
:
[concat [spacer
,fitString (Just amtwidth) (Just amtwidth) True False a
,a
," "
,fitString (Just balwidth) (Just balwidth) True False b
,b
]
| (a,b) <- zip amtrest balrest
]
@ -178,17 +178,16 @@ postingsReportItemAsText opts preferredamtwidth preferredbalwidth (mdate, mendda
BalancedVirtualPosting -> (\s -> "["++s++"]", acctwidth-2)
VirtualPosting -> (\s -> "("++s++")", acctwidth-2)
_ -> (id,acctwidth)
showamt = showMixedAmountWithoutPrice (color_ . rsOpts $ reportspec_ opts)
amt = showamt $ pamount p
bal = showamt b
amt = fst $ showMixed showAmountWithoutPrice (Just amtwidth) (Just amtwidth) (color_ . rsOpts $ reportspec_ opts) $ pamount p
bal = fst $ showMixed showAmountWithoutPrice (Just balwidth) (Just balwidth) (color_ . rsOpts $ reportspec_ opts) b
-- alternate behaviour, show null amounts as 0 instead of blank
-- amt = if null amt' then "0" else amt'
-- bal = if null bal' then "0" else bal'
(amtlines, ballines) = (lines amt, lines bal)
(amtlen, ballen) = (length amtlines, length ballines)
numlines = max 1 (max amtlen ballen)
(amtfirstline:amtrest) = take numlines $ amtlines ++ repeat "" -- posting amount is top-aligned
(balfirstline:balrest) = take numlines $ replicate (numlines - ballen) "" ++ ballines -- balance amount is bottom-aligned
(amtfirstline:amtrest) = take numlines $ amtlines ++ repeat (replicate amtwidth ' ') -- posting amount is top-aligned
(balfirstline:balrest) = take numlines $ replicate (numlines - ballen) (replicate balwidth ' ') ++ ballines -- balance amount is bottom-aligned
spacer = replicate (totalwidth - (amtwidth + 2 + balwidth)) ' '
-- tests

View File

@ -0,0 +1,18 @@
2019-1-1
(a) -1.0
2019-1-2
(a) 1
2019-1-3
(a) 1
2019-1-4
(a) 1
$ hledger -f- register --color always
2019-01-01 (a) -1.0 -1.0
2019-01-02 (a) 1.0 0
2019-01-03 (a) 1.0 1.0
2019-01-04 (a) 1.0 2.0
>=