From e6181efe95fde2d1371597ea1907fc53f8f9798d Mon Sep 17 00:00:00 2001 From: Simon Michael Date: Wed, 15 Aug 2018 11:06:46 +0100 Subject: [PATCH] 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.."}] --- hledger-lib/Hledger/Data/Amount.hs | 14 ++++++-------- hledger-lib/Hledger/Data/Types.hs | 13 ++++++++++++- 2 files changed, 18 insertions(+), 9 deletions(-) diff --git a/hledger-lib/Hledger/Data/Amount.hs b/hledger-lib/Hledger/Data/Amount.hs index 9651541ba..042101240 100644 --- a/hledger-lib/Hledger/Data/Amount.hs +++ b/hledger-lib/Hledger/Data/Amount.hs @@ -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} diff --git a/hledger-lib/Hledger/Data/Types.hs b/hledger-lib/Hledger/Data/Types.hs index 73f472d2b..f6a9b67e9 100644 --- a/hledger-lib/Hledger/Data/Types.hs +++ b/hledger-lib/Hledger/Data/Types.hs @@ -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