cli: make prices a builtin command
This commit is contained in:
		
							parent
							
								
									4e6aa06b62
								
							
						
					
					
						commit
						258efdf83c
					
				| @ -23,6 +23,7 @@ module Hledger.Cli.Commands ( | ||||
|   ,module Hledger.Cli.Commands.Equity | ||||
|   ,module Hledger.Cli.Commands.Help | ||||
|   ,module Hledger.Cli.Commands.Incomestatement | ||||
|   ,module Hledger.Cli.Commands.Prices | ||||
|   ,module Hledger.Cli.Commands.Print | ||||
|   ,module Hledger.Cli.Commands.Register | ||||
|   ,module Hledger.Cli.Commands.Stats | ||||
| @ -55,6 +56,7 @@ import Hledger.Cli.Commands.Checkdupes | ||||
| import Hledger.Cli.Commands.Equity | ||||
| import Hledger.Cli.Commands.Help | ||||
| import Hledger.Cli.Commands.Incomestatement | ||||
| import Hledger.Cli.Commands.Prices | ||||
| import Hledger.Cli.Commands.Print | ||||
| import Hledger.Cli.Commands.Register | ||||
| import Hledger.Cli.Commands.Stats | ||||
|  | ||||
| @ -1,23 +1,21 @@ | ||||
| #!/usr/bin/env stack | ||||
| {- stack runghc --verbosity info | ||||
|    --package hledger | ||||
|    --package here | ||||
|    --package text | ||||
|    --package time | ||||
| -} | ||||
| 
 | ||||
| {-# OPTIONS_GHC -Wno-missing-signatures #-} | ||||
| {-# LANGUAGE QuasiQuotes #-} | ||||
| 
 | ||||
| module Hledger.Cli.Commands.Prices ( | ||||
|   pricesmode | ||||
|  ,prices | ||||
| )  | ||||
| where | ||||
| 
 | ||||
| import Data.Maybe | ||||
| import Data.List | ||||
| import Data.String.Here | ||||
| import Data.Time | ||||
| import qualified Data.Text as T | ||||
| import Hledger.Cli | ||||
| import Data.Time | ||||
| import Hledger | ||||
| import Hledger.Cli.CliOptions | ||||
| import System.Console.CmdArgs.Explicit | ||||
| 
 | ||||
| ------------------------------------------------------------------------------ | ||||
| cmdmode = hledgerCommandMode | ||||
| pricesmode = hledgerCommandMode | ||||
|   [here| prices | ||||
| Print all market prices from the journal. | ||||
|   |] | ||||
| @ -26,7 +24,21 @@ Print all market prices from the journal. | ||||
|   [generalflagsgroup1] | ||||
|   [] | ||||
|   ([], Nothing) | ||||
| ------------------------------------------------------------------------------ | ||||
| 
 | ||||
| prices opts j = do | ||||
|   -- XXX the original hledger-prices script always ignored assertions  | ||||
|   let cprices = concatMap postingCosts . allPostings $ j | ||||
|       icprices = concatMap postingCosts . mapAmount invertPrice . allPostings $ j | ||||
|       printPrices = mapM_ (putStrLn . showPrice) | ||||
|       forBoolOpt opt | boolopt opt $ rawopts_ opts = id | ||||
|                      | otherwise = const [] | ||||
|       allPrices = sortOn mpdate . concat $ | ||||
|           [ jmarketprices j | ||||
|           , forBoolOpt "costs" cprices | ||||
|           , forBoolOpt "inverted-costs" icprices | ||||
|           ] | ||||
|    | ||||
|   printPrices allPrices | ||||
| 
 | ||||
| showPrice :: MarketPrice -> String | ||||
| showPrice mp = unwords ["P", show $ mpdate mp, T.unpack . quoteCommoditySymbolIfNeeded $ mpcommodity mp, showAmountWithZeroCommodity $ mpamount mp] | ||||
| @ -70,20 +82,3 @@ mapAmount :: (Amount -> Amount) -> [Posting] -> [Posting] | ||||
| mapAmount f = map pf where | ||||
|     pf p = p { pamount = mf (pamount p) } | ||||
|     mf = mixed . map f . amounts | ||||
| 
 | ||||
| main :: IO () | ||||
| main = do | ||||
|     opts <- getHledgerCliOpts cmdmode | ||||
|     withJournalDo opts{ ignore_assertions_ = True } $ \_ j -> do | ||||
|         let cprices = concatMap postingCosts . allPostings $ j | ||||
|             icprices = concatMap postingCosts . mapAmount invertPrice . allPostings $ j | ||||
|             printPrices = mapM_ (putStrLn . showPrice) | ||||
|             forBoolOpt opt | boolopt opt $ rawopts_ opts = id | ||||
|                            | otherwise = const [] | ||||
|             allPrices = sortOn mpdate . concat $ | ||||
|                 [ jmarketprices j | ||||
|                 , forBoolOpt "costs" cprices | ||||
|                 , forBoolOpt "inverted-costs" icprices | ||||
|                 ] | ||||
| 
 | ||||
|         printPrices allPrices | ||||
| @ -89,11 +89,6 @@ is an old pie chart generator, in need of some love. | ||||
| [hledger-check.hs](https://github.com/simonmichael/hledger/blob/master/bin/hledger-check.hs) | ||||
| checks more powerful account balance assertions. | ||||
| 
 | ||||
| ### prices | ||||
| 
 | ||||
| [hledger-prices.hs](https://github.com/simonmichael/hledger/blob/master/bin/hledger-prices.hs) | ||||
| prints all prices from the journal. | ||||
| 
 | ||||
| ### print-unique | ||||
| 
 | ||||
| [hledger-print-unique.hs](https://github.com/simonmichael/hledger/blob/master/bin/hledger-print-unique.hs#L15) | ||||
|  | ||||
| @ -458,6 +458,9 @@ Normally incomestatement shows revenues/expenses per period, though | ||||
| as with [multicolumn balance reports](#multicolumn-balance-reports) | ||||
| you can alter the report mode with `--change`/`--cumulative`/`--historical`. | ||||
| 
 | ||||
| ## prices | ||||
| Print all market prices from the journal. | ||||
| 
 | ||||
| ## print | ||||
| Show transactions from the journal. | ||||
| 
 | ||||
|  | ||||
| @ -134,6 +134,7 @@ library | ||||
|       Hledger.Cli.Commands.Equity | ||||
|       Hledger.Cli.Commands.Help | ||||
|       Hledger.Cli.Commands.Incomestatement | ||||
|       Hledger.Cli.Commands.Prices | ||||
|       Hledger.Cli.Commands.Print | ||||
|       Hledger.Cli.Commands.Register | ||||
|       Hledger.Cli.Commands.Stats | ||||
|  | ||||
| @ -115,6 +115,7 @@ library: | ||||
|   - Hledger.Cli.Commands.Equity | ||||
|   - Hledger.Cli.Commands.Help | ||||
|   - Hledger.Cli.Commands.Incomestatement | ||||
|   - Hledger.Cli.Commands.Prices | ||||
|   - Hledger.Cli.Commands.Print | ||||
|   - Hledger.Cli.Commands.Register | ||||
|   - Hledger.Cli.Commands.Stats | ||||
|  | ||||
		Loading…
	
		Reference in New Issue
	
	Block a user