lib: Add wrap convenience function.

This commit is contained in:
Stephen Morgan 2020-10-27 16:20:38 +11:00
parent 646ee0bce5
commit 12a6435c51
3 changed files with 15 additions and 10 deletions

View File

@ -165,11 +165,11 @@ showPosting p@Posting{paccount=a,pamount=amt,ptype=t} =
where where
ledger3ishlayout = False ledger3ishlayout = False
acctnamewidth = if ledger3ishlayout then 25 else 22 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 (bracket,width) = case t of
BalancedVirtualPosting -> (\s -> "["++s++"]", acctnamewidth-2) BalancedVirtualPosting -> (wrap "[" "]", acctnamewidth-2)
VirtualPosting -> (\s -> "("++s++")", acctnamewidth-2) VirtualPosting -> (wrap "(" ")", acctnamewidth-2)
_ -> (id,acctnamewidth) _ -> (id,acctnamewidth)
showamount = fst . showMixed showAmount (Just 12) Nothing False showamount = fst . showMixed showAmount (Just 12) Nothing False
@ -274,9 +274,9 @@ accountNameWithoutPostingType a = case accountNamePostingType a of
RegularPosting -> a RegularPosting -> a
accountNameWithPostingType :: PostingType -> AccountName -> AccountName accountNameWithPostingType :: PostingType -> AccountName -> AccountName
accountNameWithPostingType BalancedVirtualPosting a = "["<>accountNameWithoutPostingType a<>"]" accountNameWithPostingType BalancedVirtualPosting = wrap "[" "]" . accountNameWithoutPostingType
accountNameWithPostingType VirtualPosting a = "("<>accountNameWithoutPostingType a<>")" accountNameWithPostingType VirtualPosting = wrap "(" ")" . accountNameWithoutPostingType
accountNameWithPostingType RegularPosting a = accountNameWithoutPostingType a accountNameWithPostingType RegularPosting = accountNameWithoutPostingType
-- | Prefix one account name to another, preserving posting type -- | Prefix one account name to another, preserving posting type
-- indicators like concatAccountNames. -- indicators like concatAccountNames.

View File

@ -12,6 +12,7 @@ module Hledger.Utils.Text
-- underline, -- underline,
-- stripbrackets, -- stripbrackets,
textUnbracket, textUnbracket,
wrap,
-- -- quoting -- -- quoting
quoteIfSpaced, quoteIfSpaced,
textQuoteIfNeeded, textQuoteIfNeeded,
@ -87,6 +88,10 @@ textElideRight :: Int -> Text -> Text
textElideRight width t = textElideRight width t =
if T.length t > width then T.take (width - 2) t <> ".." else 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. -- -- | 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). -- -- Works on multi-line strings too (but will rewrite non-unix line endings).
-- formatString :: Bool -> Maybe Int -> Maybe Int -> String -> String -- formatString :: Bool -> Maybe Int -> Maybe Int -> String -> String

View File

@ -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 date = showDate $ postingDate p -- XXX csv should show date2 with --date2
code = maybe "" (T.unpack . tcode) $ ptransaction p code = maybe "" (T.unpack . tcode) $ ptransaction p
desc = T.unpack $ maybe "" tdescription $ ptransaction p desc = T.unpack $ maybe "" tdescription $ ptransaction p
acct = bracket $ T.unpack $ paccount p acct = T.unpack . bracket $ paccount p
where where
bracket = case ptype p of bracket = case ptype p of
BalancedVirtualPosting -> (\s -> "["++s++"]") BalancedVirtualPosting -> wrap "[" "]"
VirtualPosting -> (\s -> "("++s++")") VirtualPosting -> wrap "(" ")"
_ -> id _ -> id
amt = showMixedAmountOneLineWithoutPrice False $ pamount p amt = showMixedAmountOneLineWithoutPrice False $ pamount p
bal = showMixedAmountOneLineWithoutPrice False b bal = showMixedAmountOneLineWithoutPrice False b