| 1 |
{-| |
| 2 |
|
| 3 |
The @commodities@ command lists commodity/currency symbols. |
| 4 |
|
| 5 |
-} |
| 6 |
|
| 7 |
{-# LANGUAGE OverloadedStrings #-} |
| 8 |
{-# LANGUAGE TemplateHaskell #-} |
| 9 |
|
| 10 |
module Hledger.Cli.Commands.Commodities ( |
| 11 |
commoditiesmode |
| 12 |
,commodities |
| 13 |
) where |
| 14 |
|
| 15 |
import Data.Map qualified as M |
| 16 |
import Data.Set qualified as S |
| 17 |
import Data.Text.IO qualified as T |
| 18 |
import System.Console.CmdArgs.Explicit |
| 19 |
|
| 20 |
import Hledger |
| 21 |
import Hledger.Cli.CliOptions |
| 22 |
import Data.List.Extra (nubSort) |
| 23 |
import Data.List ((\\)) |
| 24 |
|
| 25 |
|
| 26 |
-- | Command line options for this command. |
| 27 |
commoditiesmode = hledgerCommandMode |
| 28 |
$(embedFileRelative "Hledger/Cli/Commands/Commodities.txt") |
| 29 |
[flagNone ["used"] (setboolopt "used") "list commodities used" |
| 30 |
,flagNone ["declared"] (setboolopt "declared") "list commodities declared" |
| 31 |
,flagNone ["undeclared"] (setboolopt "undeclared") "list commodities used but not declared" |
| 32 |
,flagNone ["unused"] (setboolopt "unused") "list commodities declared but not used" |
| 33 |
,flagNone ["find"] (setboolopt "find") "list the first commodity matched by the first argument (a case-insensitive infix regexp)" |
| 34 |
] |
| 35 |
[generalflagsgroup2] |
| 36 |
confflags |
| 37 |
([], Just $ argsFlag "[QUERY..]") |
| 38 |
|
| 39 |
commodities :: CliOpts -> Journal -> IO () |
| 40 |
commodities opts@CliOpts{rawopts_=rawopts, reportspec_=ReportSpec{_rsQuery=query}} j = do |
| 41 |
let |
| 42 |
filt = filter (matchesCommodity query) |
| 43 |
used = dbg5 "used" $ S.toList $ journalCommoditiesFromPriceDirectives j <> journalCommoditiesFromTransactions j |
| 44 |
declared' = dbg5 "declared" $ M.keys $ jdeclaredcommodities j |
| 45 |
unused = dbg5 "unused" $ declared' \\ used |
| 46 |
undeclared = dbg5 "undeclared" $ used \\ declared' |
| 47 |
all' = dbg5 "all" $ nubSort $ concat [ |
| 48 |
journalCommoditiesDeclared j |
| 49 |
,map pdcommodity $ jpricedirectives j -- gets the first symbol from P directives |
| 50 |
,map acommodity (S.toList $ journalAmounts j) -- includes the second symbol from P directives |
| 51 |
] |
| 52 |
found = dbg5 "found" $ findMatchedByArgument rawopts "commodity" all' |
| 53 |
|
| 54 |
mapM_ T.putStrLn $ |
| 55 |
case declarablesSelectorFromOpts opts of |
| 56 |
Nothing -> filt all' |
| 57 |
Just Used -> filt used |
| 58 |
Just Declared -> filt declared' |
| 59 |
Just Undeclared -> filt undeclared |
| 60 |
Just Unused -> filt unused |
| 61 |
Just Find -> [found] |