From 28fd7c65f25f8f3aaee26bb3d6f8dc9131bf8085 Mon Sep 17 00:00:00 2001 From: Stephen Morgan Date: Wed, 14 Apr 2021 11:15:54 +1000 Subject: [PATCH] lib: When displaying amounts on a single line, always display at least one amount, even if that would exceed the requested maximum width. Addresses #1526. --- hledger-lib/Hledger/Data/Amount.hs | 10 +++++++--- hledger/test/journal/numbers.test | 13 +++++++++++++ 2 files changed, 20 insertions(+), 3 deletions(-) diff --git a/hledger-lib/Hledger/Data/Amount.hs b/hledger-lib/Hledger/Data/Amount.hs index 43051fbe5..e9714517d 100644 --- a/hledger-lib/Hledger/Data/Amount.hs +++ b/hledger-lib/Hledger/Data/Amount.hs @@ -451,7 +451,9 @@ showAmountB opts a@Amount{astyle=style} = -- -- If a maximum width is given then: -- - If displayed on one line, it will display as many Amounts as can --- fit in the given width, and further Amounts will be elided. +-- fit in the given width, and further Amounts will be elided. There +-- will always be at least one amount displayed, even if this will +-- exceed the requested maximum width. -- - If displayed on multiple lines, any Amounts longer than the -- maximum width will be elided. showAmountsB :: AmountDisplayOpts -> [Amount] -> WideBuilder @@ -507,8 +509,10 @@ showAmountsOneLineB opts@AmountDisplayOpts{displayMaxWidth=mmax,displayMinWidth= addElide [] = [] addElide xs = maybeAppend (snd $ last xs) $ map fst xs -- Return the elements of the display list which fit within the maximum width - -- (including their elision strings) - takeFitting m = dropWhileRev (\(a,e) -> m < adTotal (fromMaybe a e)) + -- (including their elision strings). Always display at least one amount, + -- regardless of width. + takeFitting _ [] = [] + takeFitting m (x:xs) = x : dropWhileRev (\(a,e) -> m < adTotal (fromMaybe a e)) xs dropWhileRev p = foldr (\x xs -> if null xs && p x then [] else x:xs) [] -- Add the elision strings (if any) to each amount diff --git a/hledger/test/journal/numbers.test b/hledger/test/journal/numbers.test index 14ba6b351..1f68ea968 100644 --- a/hledger/test/journal/numbers.test +++ b/hledger/test/journal/numbers.test @@ -221,3 +221,16 @@ hledger bal -f - b -,100.0 EUR >>> >>>=1 + +# 20. long numbers in multi balance reports +hledger bal -YN -f - +<<< +2021-01-01 + (a) -0.12345678901234567890123456 EUR +>>> +Balance changes in 2021: + + || 2021 +===++=================================== + a || -0.12345678901234567890123456 EUR +>>>=0