diff --git a/Ledger/AccountName.hs b/Ledger/AccountName.hs index b62cf9fd0..52dc339f9 100644 --- a/Ledger/AccountName.hs +++ b/Ledger/AccountName.hs @@ -75,3 +75,27 @@ accountNameTreeFrom accts = accountsFrom as = [Node a (accountsFrom $ subs a) | a <- as] subs = (subAccountNamesFrom accts) +-- | Elide an account name to fit in the specified width. +-- From the ledger 2.6 news: +-- +-- @ +-- What Ledger now does is that if an account name is too long, it will +-- start abbreviating the first parts of the account name down to two +-- letters in length. If this results in a string that is still too +-- long, the front will be elided -- not the end. For example: +-- +-- Expenses:Cash ; OK, not too long +-- Ex:Wednesday:Cash ; "Expenses" was abbreviated to fit +-- Ex:We:Afternoon:Cash ; "Expenses" and "Wednesday" abbreviated +-- ; Expenses:Wednesday:Afternoon:Lunch:Snack:Candy:Chocolate:Cash +-- ..:Af:Lu:Sn:Ca:Ch:Cash ; Abbreviated and elided! +-- @ +elideAccountName :: Int -> AccountName -> AccountName +elideAccountName width s = + elideLeft width $ accountNameFromComponents $ elideparts width [] $ accountNameComponents s + where + elideparts :: Int -> [String] -> [String] -> [String] + elideparts width done ss + | (length $ accountNameFromComponents $ done++ss) <= width = done++ss + | length ss > 1 = elideparts width (done++[take 2 $ head ss]) (tail ss) + | otherwise = done++ss diff --git a/Ledger/RawTransaction.hs b/Ledger/RawTransaction.hs index c74364705..a50507df2 100644 --- a/Ledger/RawTransaction.hs +++ b/Ledger/RawTransaction.hs @@ -10,6 +10,7 @@ where import Ledger.Utils import Ledger.Types import Ledger.Amount +import Ledger.AccountName instance Show RawTransaction where show = showLedgerTransaction @@ -17,14 +18,9 @@ instance Show RawTransaction where show = showLedgerTransaction showLedgerTransaction :: RawTransaction -> String showLedgerTransaction t = (showaccountname $ taccount t) ++ " " ++ (showamount $ tamount t) where - showaccountname = printf "%-22s" . elideRight 22 + showaccountname = printf "%-22s" . elideAccountName 22 showamount = printf "%12s" . showAmountOrZero -elideRight width s = - case length s > width of - True -> take (width - 2) s ++ ".." - False -> s - autofillTransactions :: [RawTransaction] -> [RawTransaction] autofillTransactions ts = case (length blanks) of diff --git a/Ledger/Transaction.hs b/Ledger/Transaction.hs index 7fdb0b7ab..3de529521 100644 --- a/Ledger/Transaction.hs +++ b/Ledger/Transaction.hs @@ -15,8 +15,7 @@ import Ledger.Amount instance Show Transaction where - show (Transaction eno d desc a amt) = - unwords [d,desc,a,show amt] + show (Transaction eno d desc a amt) = unwords [d,desc,a,show amt] -- | Convert a 'Entry' to two or more 'Transaction's. An id number -- is attached to the transactions to preserve their grouping - it should diff --git a/Ledger/Utils.hs b/Ledger/Utils.hs index 6a7ab7855..2305dd856 100644 --- a/Ledger/Utils.hs +++ b/Ledger/Utils.hs @@ -40,6 +40,16 @@ import Text.Regex import Text.ParserCombinators.Parsec (parse) +elideLeft width s = + case length s > width of + True -> ".." ++ (reverse $ take (width - 2) $ reverse s) + False -> s + +elideRight width s = + case length s > width of + True -> take (width - 2) s ++ ".." + False -> s + -- regexps instance Show Regex where show r = "a Regex"