Debian Codegraph @

Hledger/Cli/Commands/Tags.hs

raw
1 {-# LANGUAGE OverloadedStrings #-}
2 {-# LANGUAGE TemplateHaskell #-}
3 {-# LANGUAGE TupleSections #-}
4
5 module Hledger.Cli.Commands.Tags (
6 tagsmode
7 ,tags
8 )
9 where
10
11 import Control.Monad.Fail qualified as Fail
12 import Data.List.Extra (nubSort)
13 import Data.Text qualified as T
14 import Data.Text.IO qualified as T
15 import Safe
16 import System.Console.CmdArgs.Explicit
17
18 import Hledger
19 import Hledger.Cli.CliOptions
20 import Data.Function ((&))
21 import Data.Maybe (fromMaybe)
22 import Data.List (find)
23
24
25 tagsmode = hledgerCommandMode
26 $(embedFileRelative "Hledger/Cli/Commands/Tags.txt")
27 [
28 flagNone ["used"] (setboolopt "used") "list tags used"
29 ,flagNone ["declared"] (setboolopt "declared") "list tags declared"
30 ,flagNone ["undeclared"] (setboolopt "undeclared") "list tags used but not declared"
31 ,flagNone ["unused"] (setboolopt "unused") "list tags declared but not used"
32 ,flagNone ["find"] (setboolopt "find") "list the first tag whose name is matched by the first argument (a case-insensitive infix regexp)"
33 ,flagNone ["values"] (setboolopt "values") "list tag values instead of tag names"
34 ,flagNone ["parsed"] (setboolopt "parsed") "show them in the order they were parsed (mostly), including duplicates"
35 ]
36 cligeneralflagsgroups1
37 hiddenflags
38 ([], Just $ argsFlag "[TAGREGEX [QUERY..]]")
39
40 tags :: CliOpts -> Journal -> IO ()
41 tags opts@CliOpts{rawopts_=rawopts, reportspec_=rspec@ReportSpec{_rsQuery=_q, _rsReportOpts=ropts}} j = do
42 let today = _rsDay rspec
43 args = listofstringopt "args" rawopts
44 -- For convenience/power, the first argument is a tag name regex,
45 -- separate from the main query arguments: hledger tags [TAGREGEX [QUERYARGS..]]
46 -- So we have to re-parse the query here. Overcomplicated ?
47 mtagpat <- mapM (either Fail.fail pure . toRegexCI . T.pack) $ headMay args
48 let
49 values = boolopt "values" rawopts
50 parsed = boolopt "parsed" rawopts
51 empty = empty_ ropts
52 querystr = map T.pack $ drop 1 args
53 query <- either usageError (return . fst) $ parseQueryList today querystr
54 let
55 q = simplifyQuery $ And [queryFromFlags ropts, query]
56 txns = filter (q `matchesTransaction`) $ jtxns $ journalApplyValuationFromOpts rspec j
57 accts =
58 -- also search for tags in matched account declarations,
59 -- unless there is a query for something transaction-specific, like date: or amt:.
60 if dbg5 "queryIsTransactionRelated" $ queryIsTransactionRelated $ dbg4 "q" q
61 then []
62 else filter (matchesAccountExtra (journalAccountType j) (journalInheritedAccountTags j) q) $
63 map fst $ jdeclaredaccounts j
64 -- bit of a mess.
65 used = dbg5 "used" $ concatMap (journalAccountTags j) accts ++ concatMap transactionAllTags txns
66 declared' = dbg5 "declared'" $ map (,"") $ journalTagsDeclared j
67 filtereddeclared = dbg5 "filtereddeclared'" $ filter (q `matchesTag`) declared'
68 (usednames, declarednames) = (map fst used, map fst filtereddeclared)
69 unused = dbg5 "unused" $ filter (not . (`elem` usednames) . fst) filtereddeclared
70 undeclared = dbg5 "undeclared" $ filter (not . (`elem` declarednames) . fst) used
71 all' = dbg5 "all''" $ filtereddeclared <> used
72 found = dbg5 "found" $ foundtag
73 where
74 -- First find the name, then the first occurrence of that tag.
75 -- So that --values and --parsed still work with --find (in some reasonably stable way).
76 alltags = declared' <> used
77 allnames = dbg5 "allnames" $ nubSort $ map fst alltags
78 foundname = dbg5 "foundname" $ findMatchedByArgument rawopts "tag name" allnames
79 foundtag = find ((==foundname).fst) alltags
80 & fromMaybe (error' "tags: could not find a tag's first occurrence") -- PARTIAL: should not happen because allnames and alltags correspond
81
82 tags' =
83 case declarablesSelectorFromOpts opts of
84 Nothing -> all'
85 Just Used -> used
86 Just Declared -> declared'
87 Just Undeclared -> undeclared
88 Just Unused -> unused
89 Just Find -> [found]
90
91 results =
92 (if parsed then id else nubSort)
93 [ r
94 | (t,v) <- tags'
95 , maybe True (`regexMatchText` t) mtagpat
96 , let r = if values then v else t
97 , not (values && T.null v && not empty)
98 ]
99
100 mapM_ T.putStrLn results