From 258efdf83c08bcc46e39f315eafffd39f23b548c Mon Sep 17 00:00:00 2001 From: Simon Michael Date: Tue, 12 Sep 2017 19:28:01 -0700 Subject: [PATCH] cli: make prices a builtin command --- hledger/Hledger/Cli/Commands.hs | 2 + .../Hledger/Cli/Commands/Prices.hs | 57 +++++++++---------- hledger/doc/addons.m4.md | 5 -- hledger/doc/commands.m4.md | 3 + hledger/hledger.cabal | 1 + hledger/package.yaml | 1 + 6 files changed, 33 insertions(+), 36 deletions(-) rename bin/hledger-prices.hs => hledger/Hledger/Cli/Commands/Prices.hs (68%) diff --git a/hledger/Hledger/Cli/Commands.hs b/hledger/Hledger/Cli/Commands.hs index 97ba76553..8eabab1eb 100644 --- a/hledger/Hledger/Cli/Commands.hs +++ b/hledger/Hledger/Cli/Commands.hs @@ -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 diff --git a/bin/hledger-prices.hs b/hledger/Hledger/Cli/Commands/Prices.hs similarity index 68% rename from bin/hledger-prices.hs rename to hledger/Hledger/Cli/Commands/Prices.hs index d2be68b76..0e6f9e8ca 100755 --- a/bin/hledger-prices.hs +++ b/hledger/Hledger/Cli/Commands/Prices.hs @@ -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 diff --git a/hledger/doc/addons.m4.md b/hledger/doc/addons.m4.md index cd35017b1..e166ebe8d 100644 --- a/hledger/doc/addons.m4.md +++ b/hledger/doc/addons.m4.md @@ -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) diff --git a/hledger/doc/commands.m4.md b/hledger/doc/commands.m4.md index e96f7991d..a0028460a 100644 --- a/hledger/doc/commands.m4.md +++ b/hledger/doc/commands.m4.md @@ -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. diff --git a/hledger/hledger.cabal b/hledger/hledger.cabal index 22ea251e9..0eef43b89 100644 --- a/hledger/hledger.cabal +++ b/hledger/hledger.cabal @@ -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 diff --git a/hledger/package.yaml b/hledger/package.yaml index ba2561ad0..9a172dc13 100644 --- a/hledger/package.yaml +++ b/hledger/package.yaml @@ -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