better ghci helpers
This commit is contained in:
		
							parent
							
								
									3aa656ba69
								
							
						
					
					
						commit
						c5b23c5724
					
				
							
								
								
									
										28
									
								
								hledger.hs
									
									
									
									
									
								
							
							
						
						
									
										28
									
								
								hledger.hs
									
									
									
									
									
								
							| @ -15,7 +15,7 @@ This module includes some helpers for working with your ledger in ghci. Examples | |||||||
| 
 | 
 | ||||||
| > $ rm -f hledger.o | > $ rm -f hledger.o | ||||||
| > $ ghci hledger.hs | > $ ghci hledger.hs | ||||||
| > *Main> l <- myledger | > *Main> l <- ledger | ||||||
| > Ledger with 696 entries, 132 accounts | > Ledger with 696 entries, 132 accounts | ||||||
| > *Main> putStr $ drawTree $ treemap show $ accountnametree l | > *Main> putStr $ drawTree $ treemap show $ accountnametree l | ||||||
| > ... | > ... | ||||||
| @ -37,9 +37,9 @@ import qualified Data.Map as Map (lookup) | |||||||
| 
 | 
 | ||||||
| import Options | import Options | ||||||
| import Tests (hunit, quickcheck) | import Tests (hunit, quickcheck) | ||||||
| import Ledger |  | ||||||
| import Ledger.Parse (parseLedgerFile, parseError) | import Ledger.Parse (parseLedgerFile, parseError) | ||||||
| import Ledger.Utils hiding (test) | import Ledger.Utils hiding (test) | ||||||
|  | import Ledger hiding (rawledger) | ||||||
| 
 | 
 | ||||||
| 
 | 
 | ||||||
| main :: IO () | main :: IO () | ||||||
| @ -93,22 +93,26 @@ parseLedgerAndDo opts pats cmd = do | |||||||
| 
 | 
 | ||||||
| -- ghci helpers | -- ghci helpers | ||||||
| 
 | 
 | ||||||
| -- | get a Ledger from the file your LEDGER environment variable points to | -- | get a RawLedger from the file your LEDGER environment variable points to | ||||||
| -- or (WARNING) an empty one if there was a problem. | -- or (WARNING) an empty one if there was a problem. | ||||||
| myledger :: IO Ledger | rawledger :: IO RawLedger | ||||||
| myledger = do | rawledger = do | ||||||
|   parsed <- ledgerFilePath [] >>= parseLedgerFile |   parsed <- ledgerFilePath [] >>= parseLedgerFile | ||||||
|   let ledgerfile = either (\_ -> RawLedger [] [] [] "") id parsed |   return $ either (\_ -> RawLedger [] [] [] "") id parsed | ||||||
|   return $ cacheLedger ledgerfile (wildcard,wildcard) | 
 | ||||||
|  | -- | as above, and convert it to a cached Ledger | ||||||
|  | ledger :: IO Ledger | ||||||
|  | ledger = do | ||||||
|  |   l <- rawledger | ||||||
|  |   return $ cacheLedger l (wildcard,wildcard) | ||||||
| 
 | 
 | ||||||
| -- | get a Ledger from the given file path | -- | get a Ledger from the given file path | ||||||
| ledgerfromfile :: String -> IO Ledger | rawledgerfromfile :: String -> IO RawLedger | ||||||
| ledgerfromfile f = do | rawledgerfromfile f = do | ||||||
|   parsed <- ledgerFilePath [File f] >>= parseLedgerFile |   parsed <- ledgerFilePath [File f] >>= parseLedgerFile | ||||||
|   let ledgerfile = either (\_ -> RawLedger [] [] [] "") id parsed |   return $ either (\_ -> RawLedger [] [] [] "") id parsed | ||||||
|   return $ cacheLedger ledgerfile (wildcard,wildcard) |  | ||||||
| 
 | 
 | ||||||
| -- | get a named account from your ledger file | -- | get a named account from your ledger file | ||||||
| accountnamed :: AccountName -> IO Account | accountnamed :: AccountName -> IO Account | ||||||
| accountnamed a = myledger >>= (return . fromMaybe nullacct . Map.lookup a . accounts) | accountnamed a = ledger >>= (return . fromMaybe nullacct . Map.lookup a . accounts) | ||||||
| 
 | 
 | ||||||
|  | |||||||
		Loading…
	
		Reference in New Issue
	
	Block a user