lib: more compact show instance for Amounts (#812)

Amount's default show instance hid important details, making eg test
failures hard to understand. Showing full detail required increasing
the debug level which was inconvenient.

Now it has a single show instance which shows more information, is
fairly compact, and is pretty-printable with pretty-show.
Ellipses (..) in the output indicate where fields are
- not shown in full detail, and/or
- shown in pseudo syntax (double quoted) to work with pretty-show.

ghci> usd 1
OLD:
Amount {acommodity="$", aquantity=1.00, ..}
NEW:
Amount {acommodity = "$", aquantity = 1.00, aprice = NoPrice, astyle = AmountStyle "L False 2 Just '.' Nothing..", amultiplier = False}

MixedAmount's show instance is unchanged, but showMixedAmountDebug
is affected by this change:

ghci> putStrLn $ showMixedAmountDebug $ Mixed [usd 1]
OLD:
Mixed [Amount {acommodity="$", aquantity=1.00, aprice=, astyle=AmountStyle {ascommodityside = L, ascommodityspaced = False, asprecision = 2, asdecimalpoint = Just '.', asdigitgroups = Nothing}}]
NEW:
Mixed [Amount {acommodity="$", aquantity=1.00, aprice=, astyle=AmountStyle "L False 2 Just '.' Nothing.."}]
This commit is contained in:
Simon Michael 2018-08-15 11:06:46 +01:00
parent f47a617dcc
commit e6181efe95
2 changed files with 18 additions and 9 deletions

View File

@ -150,14 +150,12 @@ amountstyle = AmountStyle L False 0 (Just '.') Nothing
-------------------------------------------------------------------------------
-- Amount
instance Show Amount where
show _a@Amount{..}
-- debugLevel < 2 = showAmountWithoutPrice a
-- debugLevel < 3 = showAmount a
| debugLevel < 6 =
printf "Amount {acommodity=%s, aquantity=%s, ..}" (show acommodity) (show aquantity)
| otherwise = --showAmountDebug a
printf "Amount {acommodity=%s, aquantity=%s, aprice=%s, astyle=%s}" (show acommodity) (show aquantity) (showPriceDebug aprice) (show astyle)
instance Show Price where
show NoPrice = "NoPrice"
show (UnitPrice a) = "\"@ " ++ showAmountWithoutPrice a ++ "..\""
show (TotalPrice a) = "\"@@ " ++ showAmountWithoutPrice a ++ "..\""
deriving instance Show Amount
instance Num Amount where
abs a@Amount{aquantity=q} = a{aquantity=abs q}

View File

@ -1,4 +1,5 @@
{-# LANGUAGE DeriveDataTypeable, StandaloneDeriving, DeriveGeneric, TypeSynonymInstances, FlexibleInstances, OverloadedStrings #-}
{-# LANGUAGE RecordWildCards #-}
{-|
Most data types are defined here to avoid import cycles.
@ -32,6 +33,7 @@ import Data.Text (Text)
import Data.Time.Calendar
import Data.Time.LocalTime
import System.Time (ClockTime(..))
import Text.Printf
import Hledger.Utils.Regex
@ -137,10 +139,19 @@ data AmountStyle = AmountStyle {
asprecision :: !Int, -- ^ number of digits displayed after the decimal point
asdecimalpoint :: Maybe Char, -- ^ character used as decimal point: period or comma. Nothing means "unspecified, use default"
asdigitgroups :: Maybe DigitGroupStyle -- ^ style for displaying digit groups, if any
} deriving (Eq,Ord,Read,Show,Typeable,Data,Generic)
} deriving (Eq,Ord,Read,Typeable,Data,Generic)
instance NFData AmountStyle
instance Show AmountStyle where
show AmountStyle{..} =
printf "AmountStyle \"%s %s %s %s %s..\""
(show ascommodityside)
(show ascommodityspaced)
(show asprecision)
(show asdecimalpoint)
(show asdigitgroups)
-- | A style for displaying digit groups in the integer part of a
-- floating point number. It consists of the character used to
-- separate groups (comma or period, whichever is not used as decimal