register: optimise postingsReportItemAsText a bit

Avoid concatTopPadded when rendering register output, making that
command faster when there's a lot of output.
This commit is contained in:
Simon Michael 2014-07-07 08:37:27 -07:00
parent 101719d3cb
commit b499ea85eb
2 changed files with 27 additions and 15 deletions

View File

@ -70,7 +70,12 @@ tests_postingsReportAsText = [
-- @ -- @
postingsReportItemAsText :: CliOpts -> PostingsReportItem -> String postingsReportItemAsText :: CliOpts -> PostingsReportItem -> String
postingsReportItemAsText opts (mdate, mdesc, p, b) = postingsReportItemAsText opts (mdate, mdesc, p, b) =
concatTopPadded [date, " ", desc, " ", acct, " ", amt, " ", bal] intercalate "\n" $
[printf ("%-"++datew++"s %-"++descw++"s %-"++acctw++"s %"++amtw++"s %"++balw++"s")
date desc acct amtfirstline balfirstline]
++
[printf (spacer ++ "%"++amtw++"s %"++balw++"s") a b | (a,b) <- zip amtrest balrest ]
where where
totalwidth = case widthFromOpts opts of totalwidth = case widthFromOpts opts of
Left _ -> defaultWidth -- shouldn't happen Left _ -> defaultWidth -- shouldn't happen
@ -85,17 +90,24 @@ postingsReportItemAsText opts (mdate, mdesc, p, b) =
| otherwise = (r', r'+1) | otherwise = (r', r'+1)
where r = remaining - 2 where r = remaining - 2
r' = r `div` 2 r' = r `div` 2
date = maybe (replicate datewidth ' ') (printf ("%-"++show datewidth++"s") . showDate) mdate [datew,descw,acctw,amtw,balw] = map show [datewidth,descwidth,acctwidth,amtwidth,balwidth]
desc = maybe (replicate descwidth ' ') (printf ("%-"++show descwidth++"s") . take descwidth . elideRight descwidth) mdesc
acct = printf ("%-"++(show acctwidth)++"s") a date = maybe "" showDate mdate
where desc = maybe "" (take descwidth . elideRight descwidth) mdesc
a = bracket $ elideAccountName awidth $ paccount p acct = parenthesise $ elideAccountName awidth $ paccount p
(bracket, awidth) = case ptype p of where
(parenthesise, awidth) = case ptype p of
BalancedVirtualPosting -> (\s -> "["++s++"]", acctwidth-2) BalancedVirtualPosting -> (\s -> "["++s++"]", acctwidth-2)
VirtualPosting -> (\s -> "("++s++")", acctwidth-2) VirtualPosting -> (\s -> "("++s++")", acctwidth-2)
_ -> (id,acctwidth) _ -> (id,acctwidth)
amt = padleft amtwidth $ showMixedAmountWithoutPrice $ pamount p amt = showMixedAmountWithoutPrice $ pamount p
bal = padleft balwidth $ showMixedAmountWithoutPrice b bal = showMixedAmountWithoutPrice b
(amtlines, ballines) = (lines amt, lines bal)
(amtlen, ballen) = (length amtlines, length ballines)
numlines = 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
spacer = replicate (totalwidth - (amtwidth + 2 + balwidth)) ' '
-- XXX -- XXX
-- showPostingWithBalanceForVty showtxninfo p b = postingsReportItemAsText defreportopts $ mkpostingsReportItem showtxninfo p b -- showPostingWithBalanceForVty showtxninfo p b = postingsReportItemAsText defreportopts $ mkpostingsReportItem showtxninfo p b

View File

@ -24,10 +24,10 @@ hledgerdev -f - register
c ; a euro and a dollar c ; a euro and a dollar
>>> >>>
2010/01/01 a EUR 1 EUR 1 2010/01/01 a EUR 1 EUR 1
EUR 1 b USD 1 EUR 1
b USD 1 USD 1 USD 1
EUR -1 c EUR -1
c USD -1 0 USD -1 0
>>>=0 >>>=0
# 3. balance # 3. balance