diff --git a/hledger-lib/Hledger/Data/Transaction.hs b/hledger-lib/Hledger/Data/Transaction.hs index f0d6eed6a..4f2929e1e 100644 --- a/hledger-lib/Hledger/Data/Transaction.hs +++ b/hledger-lib/Hledger/Data/Transaction.hs @@ -302,11 +302,10 @@ commentSpace = (" "++) -- appropriately bracketed/parenthesised for the given posting type. showAccountName :: Maybe Int -> PostingType -> AccountName -> String showAccountName w = fmt - where - fmt RegularPosting = take w' . T.unpack - fmt VirtualPosting = parenthesise . reverse . take (w'-2) . reverse . T.unpack - fmt BalancedVirtualPosting = bracket . reverse . take (w'-2) . reverse . T.unpack - w' = fromMaybe 999999 w + where + fmt RegularPosting = maybe id take w . T.unpack + fmt VirtualPosting = parenthesise . maybe id (takeEnd . subtract 2) w . T.unpack + fmt BalancedVirtualPosting = bracket . maybe id (takeEnd . subtract 2) w . T.unpack parenthesise :: String -> String parenthesise s = "("++s++")" diff --git a/hledger-lib/Hledger/Utils/String.hs b/hledger-lib/Hledger/Utils/String.hs index 2e259e13f..2e1543b5d 100644 --- a/hledger-lib/Hledger/Utils/String.hs +++ b/hledger-lib/Hledger/Utils/String.hs @@ -1,6 +1,7 @@ -- | String formatting helpers, starting to get a bit out of control. module Hledger.Utils.String ( + takeEnd, -- * misc lowercase, uppercase, @@ -57,6 +58,14 @@ import Text.Printf (printf) import Hledger.Utils.Parse import Hledger.Utils.Regex + +-- | Take elements from the end of a list. +takeEnd n l = go (drop n l) l + where + go (_:xs) (_:ys) = go xs ys + go [] r = r + go _ [] = [] + lowercase, uppercase :: String -> String lowercase = map toLower uppercase = map toUpper @@ -86,7 +95,7 @@ stripbrackets = dropWhile (`elem` "([") . reverse . dropWhile (`elem` "])") . re elideLeft :: Int -> String -> String elideLeft width s = - if length s > width then ".." ++ reverse (take (width - 2) $ reverse s) else s + if length s > width then ".." ++ takeEnd (width - 2) s else s elideRight :: Int -> String -> String elideRight width s =