From 12a6435c519c1984e7b7a30537eba5b036b75c66 Mon Sep 17 00:00:00 2001 From: Stephen Morgan Date: Tue, 27 Oct 2020 16:20:38 +1100 Subject: [PATCH] lib: Add wrap convenience function. --- hledger-lib/Hledger/Data/Posting.hs | 14 +++++++------- hledger-lib/Hledger/Utils/Text.hs | 5 +++++ hledger/Hledger/Cli/Commands/Register.hs | 6 +++--- 3 files changed, 15 insertions(+), 10 deletions(-) diff --git a/hledger-lib/Hledger/Data/Posting.hs b/hledger-lib/Hledger/Data/Posting.hs index 0cced45f3..61fe8a244 100644 --- a/hledger-lib/Hledger/Data/Posting.hs +++ b/hledger-lib/Hledger/Data/Posting.hs @@ -165,11 +165,11 @@ showPosting p@Posting{paccount=a,pamount=amt,ptype=t} = where ledger3ishlayout = False acctnamewidth = if ledger3ishlayout then 25 else 22 - showaccountname = fitString (Just acctnamewidth) Nothing False False . bracket . T.unpack . elideAccountName width + showaccountname = T.unpack . fitText (Just acctnamewidth) Nothing False False . bracket . elideAccountName width (bracket,width) = case t of - BalancedVirtualPosting -> (\s -> "["++s++"]", acctnamewidth-2) - VirtualPosting -> (\s -> "("++s++")", acctnamewidth-2) - _ -> (id,acctnamewidth) + BalancedVirtualPosting -> (wrap "[" "]", acctnamewidth-2) + VirtualPosting -> (wrap "(" ")", acctnamewidth-2) + _ -> (id,acctnamewidth) showamount = fst . showMixed showAmount (Just 12) Nothing False @@ -274,9 +274,9 @@ accountNameWithoutPostingType a = case accountNamePostingType a of RegularPosting -> a accountNameWithPostingType :: PostingType -> AccountName -> AccountName -accountNameWithPostingType BalancedVirtualPosting a = "["<>accountNameWithoutPostingType a<>"]" -accountNameWithPostingType VirtualPosting a = "("<>accountNameWithoutPostingType a<>")" -accountNameWithPostingType RegularPosting a = accountNameWithoutPostingType a +accountNameWithPostingType BalancedVirtualPosting = wrap "[" "]" . accountNameWithoutPostingType +accountNameWithPostingType VirtualPosting = wrap "(" ")" . accountNameWithoutPostingType +accountNameWithPostingType RegularPosting = accountNameWithoutPostingType -- | Prefix one account name to another, preserving posting type -- indicators like concatAccountNames. diff --git a/hledger-lib/Hledger/Utils/Text.hs b/hledger-lib/Hledger/Utils/Text.hs index c6e47d3f1..0f96d5391 100644 --- a/hledger-lib/Hledger/Utils/Text.hs +++ b/hledger-lib/Hledger/Utils/Text.hs @@ -12,6 +12,7 @@ module Hledger.Utils.Text -- underline, -- stripbrackets, textUnbracket, + wrap, -- -- quoting quoteIfSpaced, textQuoteIfNeeded, @@ -87,6 +88,10 @@ textElideRight :: Int -> Text -> Text textElideRight width t = if T.length t > width then T.take (width - 2) t <> ".." else t +-- | Wrap a Text with the surrounding Text. +wrap :: Text -> Text -> Text -> Text +wrap start end x = start <> x <> end + -- -- | Clip and pad a string to a minimum & maximum width, and/or left/right justify it. -- -- Works on multi-line strings too (but will rewrite non-unix line endings). -- formatString :: Bool -> Maybe Int -> Maybe Int -> String -> String diff --git a/hledger/Hledger/Cli/Commands/Register.hs b/hledger/Hledger/Cli/Commands/Register.hs index 9e2259247..fa83c07a5 100644 --- a/hledger/Hledger/Cli/Commands/Register.hs +++ b/hledger/Hledger/Cli/Commands/Register.hs @@ -81,11 +81,11 @@ postingsReportItemAsCsvRecord (_, _, _, p, b) = [idx,date,code,desc,acct,amt,bal date = showDate $ postingDate p -- XXX csv should show date2 with --date2 code = maybe "" (T.unpack . tcode) $ ptransaction p desc = T.unpack $ maybe "" tdescription $ ptransaction p - acct = bracket $ T.unpack $ paccount p + acct = T.unpack . bracket $ paccount p where bracket = case ptype p of - BalancedVirtualPosting -> (\s -> "["++s++"]") - VirtualPosting -> (\s -> "("++s++")") + BalancedVirtualPosting -> wrap "[" "]" + VirtualPosting -> wrap "(" ")" _ -> id amt = showMixedAmountOneLineWithoutPrice False $ pamount p bal = showMixedAmountOneLineWithoutPrice False b