From 586c824cac956358accb23ddb7e84acbac813d9d Mon Sep 17 00:00:00 2001 From: Simon Michael Date: Thu, 27 May 2010 01:38:23 +0000 Subject: [PATCH] display non-simple commodity names with double-quotes This ensures print's output remains parseable. The other commands also double-quote them, for consistency, like c++ ledger. --- hledger-lib/Hledger/Data/Amount.hs | 7 ++++--- hledger-lib/Hledger/Data/Commodity.hs | 3 +++ tests/commodities.test | 17 ++++++++++++++--- 3 files changed, 21 insertions(+), 6 deletions(-) diff --git a/hledger-lib/Hledger/Data/Amount.hs b/hledger-lib/Hledger/Data/Amount.hs index eb57a72c5..8c4fadcbb 100644 --- a/hledger-lib/Hledger/Data/Amount.hs +++ b/hledger-lib/Hledger/Data/Amount.hs @@ -106,9 +106,10 @@ showAmount :: Amount -> String showAmount (Amount (Commodity {symbol="AUTO"}) _ _) = "" -- can appear in an error message showAmount a@(Amount (Commodity {symbol=sym,side=side,spaced=spaced}) _ pri) = case side of - L -> printf "%s%s%s%s" sym space quantity price - R -> printf "%s%s%s%s" quantity space sym price - where + L -> printf "%s%s%s%s" sym' space quantity price + R -> printf "%s%s%s%s" quantity space sym' price + where + sym' = quoteCommoditySymbolIfNeeded sym space = if spaced then " " else "" quantity = showAmount' a price = case pri of (Just pamt) -> " @ " ++ showMixedAmount pamt diff --git a/hledger-lib/Hledger/Data/Commodity.hs b/hledger-lib/Hledger/Data/Commodity.hs index d2cd07bda..1959eb866 100644 --- a/hledger-lib/Hledger/Data/Commodity.hs +++ b/hledger-lib/Hledger/Data/Commodity.hs @@ -16,6 +16,9 @@ import Data.Map ((!)) nonsimplecommoditychars = "0123456789-.@;\n \"" +quoteCommoditySymbolIfNeeded s | any (`elem` nonsimplecommoditychars) s = "\"" ++ s ++ "\"" + | otherwise = s + -- convenient amount and commodity constructors, for tests etc. unknown = Commodity {symbol="", side=L,spaced=False,comma=False,precision=0} diff --git a/tests/commodities.test b/tests/commodities.test index 117608ece..5a932f56a 100644 --- a/tests/commodities.test +++ b/tests/commodities.test @@ -7,7 +7,7 @@ bin/hledger -f- print b >>>2 /parse error.*unexpected "0"/ >>>= 1 -# 2. with quotes +# 2. with quotes, ok; quotes appear in print output bin/hledger -f- print <<< 2010-04-05 x @@ -15,6 +15,17 @@ bin/hledger -f- print b >>> 2010/04/05 x - a 10 DE0002635307 - b -10 DE0002635307 + a 10 "DE0002635307" + b -10 "DE0002635307" +# 3. and in other reports too, I guess +bin/hledger -f- balance +<<< +2010-04-05 x + a 10 "DE0002635307" + b +>>> + 10 "DE0002635307" a + -10 "DE0002635307" b +-------------------- + 0 "DE0002635307"