ledger 2.6-style account name eliding
This commit is contained in:
		
							parent
							
								
									7dea3bc201
								
							
						
					
					
						commit
						529393ae49
					
				| @ -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 | ||||
|  | ||||
| @ -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 | ||||
|  | ||||
| @ -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 | ||||
|  | ||||
| @ -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" | ||||
|  | ||||
		Loading…
	
		Reference in New Issue
	
	Block a user