    {-|

    The @commodities@ command lists commodity/currency symbols.

    -}

    {-# LANGUAGE OverloadedStrings #-}
    {-# LANGUAGE TemplateHaskell #-}

    module Hledger.Cli.Commands.Commodities (
      commoditiesmode
     ,commodities
    ) where

    import Control.Monad
    import Data.List.Extra (nubSort)
    import qualified Data.Map as M
    import qualified Data.Text.IO as T

    import Hledger
    import Hledger.Cli.CliOptions

    -- | Command line options for this command.
    commoditiesmode :: Mode RawOpts
    commoditiesmode = CommandDoc
    -> [Flag RawOpts]
    -> [(CommandDoc, [Flag RawOpts])]
    -> [Flag RawOpts]
    -> ([Arg RawOpts], Maybe (Arg RawOpts))
    -> Mode RawOpts
    hledgerCommandMode
      $(embedFileRelative "Hledger/Cli/Commands/Commodities.txt")
      []
      [(CommandDoc, [Flag RawOpts])
    generalflagsgroup2]
      []
      ([], Maybe (Arg RawOpts)
    forall a. Maybe a
    Nothing)

    commodities :: CliOpts -> Journal -> IO ()
    commodities :: CliOpts -> Journal -> IO ()
    commodities _copts :: CliOpts
    _copts j :: Journal
    j = do
      let cs :: [CommoditySymbol]
    cs = (CommoditySymbol -> Bool) -> [CommoditySymbol] -> [CommoditySymbol]
    forall a. (a -> Bool) -> [a] -> [a]
    filter (CommoditySymbol -> CommoditySymbol -> Bool
    forall a. Eq a => a -> a -> Bool
    /= "AUTO") ([CommoditySymbol] -> [CommoditySymbol])
    -> [CommoditySymbol] -> [CommoditySymbol]
    forall a b. (a -> b) -> a -> b
    $
               [CommoditySymbol] -> [CommoditySymbol]
    forall a. Ord a => [a] -> [a]
    nubSort ([CommoditySymbol] -> [CommoditySymbol])
    -> [CommoditySymbol] -> [CommoditySymbol]
    forall a b. (a -> b) -> a -> b
    $ Map CommoditySymbol Commodity -> [CommoditySymbol]
    forall k a. Map k a -> [k]
    M.keys (Journal -> Map CommoditySymbol Commodity
    jcommodities Journal
    j) [CommoditySymbol] -> [CommoditySymbol] -> [CommoditySymbol]
    forall a. [a] -> [a] -> [a]
    ++ Map CommoditySymbol AmountStyle -> [CommoditySymbol]
    forall k a. Map k a -> [k]
    M.keys (Journal -> Map CommoditySymbol AmountStyle
    jinferredcommodities Journal
    j)
      [CommoditySymbol] -> (CommoditySymbol -> IO ()) -> IO ()
    forall (t :: * -> *) (m :: * -> *) a b.
    (Foldable t, Monad m) =>
    t a -> (a -> m b) -> m ()
    forM_ [CommoditySymbol]
    cs CommoditySymbol -> IO ()
    T.putStrLn