Debian Codegraph @

Hledger/Cli/Commands/Accounts.hs

raw
1 {-|
2
3 The @accounts@ command lists account names:
4
5 - in flat mode (default), it lists the full names of accounts posted to by matched postings,
6 clipped to the specified depth, possibly with leading components dropped.
7
8 - in tree mode, it shows the indented short names of accounts posted to by matched postings,
9 and their parents, to the specified depth.
10
11 -}
12
13 {-# LANGUAGE MultiWayIf #-}
14 {-# LANGUAGE OverloadedStrings #-}
15 {-# LANGUAGE ScopedTypeVariables #-}
16 {-# LANGUAGE TemplateHaskell #-}
17
18 module Hledger.Cli.Commands.Accounts (
19 accountsmode
20 ,accounts
21 ) where
22
23 import Control.Monad (forM_)
24 import Data.List
25 import Data.Text qualified as T
26 import Data.Text.IO qualified as T
27 import System.Console.CmdArgs.Explicit as C
28
29 import Hledger
30 import Hledger.Cli.CliOptions
31
32
33 -- | Command line options for this command.
34 accountsmode = hledgerCommandMode
35 $(embedFileRelative "Hledger/Cli/Commands/Accounts.txt")
36 (
37 [flagNone ["used","u"] (setboolopt "used") "list accounts used"
38 ,flagNone ["declared","d"] (setboolopt "declared") "list accounts declared"
39 ,flagNone ["undeclared"] (setboolopt "undeclared") "list accounts used but not declared"
40 ,flagNone ["unused"] (setboolopt "unused") "list accounts declared but not used"
41 ,flagNone ["find"] (setboolopt "find") "list the first account matched by the first argument (a case-insensitive infix regexp)"
42 ,flagNone ["directives"] (setboolopt "directives") "show as account directives, for use in journals"
43 ,flagNone ["locations"] (setboolopt "locations") "also show where accounts were declared"
44 ,flagNone ["types"] (setboolopt "types") "also show account types when known"
45 ]
46 ++ flattreeflags False ++
47 [flagReq ["drop"] (\s opts -> Right $ setopt "drop" s opts) "N" "flat mode: omit N leading account name parts"]
48 )
49 cligeneralflagsgroups1
50 (hiddenflags ++
51 [flagNone ["positions"] (setboolopt "locations") "deprecated, use --locations instead"
52 ])
53 ([], Just $ argsFlag "[QUERY..]")
54
55 -- | The accounts command.
56 accounts :: CliOpts -> Journal -> IO ()
57 accounts opts@CliOpts{rawopts_=rawopts, reportspec_=ReportSpec{_rsQuery=query,_rsReportOpts=ropts}} j = do
58
59 -- 1. identify the accounts we'll show
60 let tree = tree_ ropts
61 directives = boolopt "directives" rawopts
62 locations = boolopt "locations" rawopts
63 types = boolopt "types" rawopts
64 -- Modified queries. These may not work with boolean queries (#2371).
65 -- a depth limit will clip and exclude account names later, but we don't want to exclude accounts at this stage
66 nodepthq = dbg4 "nodepthq" $ filterQuery (not . queryIsDepth) query
67 -- just the acct: part of the query will be reapplied later, after clipping
68 acctq = dbg4 "acctq" $ filterQuery queryIsAcct query
69 dep = dbg4 "depth" $ queryDepth $ filterQuery queryIsDepth query
70 -- when finding accounts used by postings, we remove tags that were declared on the posting,
71 -- so that a tag: query will match account tags and not posting tags.
72 matchedused = dbg5 "matchedused" $ nub $ map paccount $ journalPostings $
73 filterJournalPostings nodepthq $ journalPostingsKeepAccountTagsOnly j
74 matcheddeclared = dbg5 "matcheddeclared" $
75 nub $
76 filter (matchesAccountExtra (journalAccountType j) (journalInheritedAccountTags j) nodepthq) $
77 map fst $ jdeclaredaccounts j
78 matchedundeclared = dbg5 "matchedundeclared" $ nub $ matchedused \\ matcheddeclared
79 matchedunused = dbg5 "matchedunused" $ nub $ matcheddeclared \\ matchedused
80 found = dbg5 "matchedacct" $ findMatchedByArgument rawopts "account" $ journalAccountNamesDeclaredOrImplied j
81 matchedall = matcheddeclared ++ matchedused
82 accts = dbg5 "accts to show" $
83 case declarablesSelectorFromOpts opts of
84 Nothing -> matchedall
85 Just Used -> matchedused
86 Just Declared -> matcheddeclared
87 Just Undeclared -> matchedundeclared
88 Just Unused -> matchedunused
89 Just Find -> [found]
90
91 -- 2. sort them by declaration order (then undeclared accounts alphabetically)
92 -- within each group of siblings
93 sortedaccts = sortAccountNamesByDeclaration j tree accts
94
95 -- 2a. in tree mode, add parent accounts for tree structure context
96 acctswithparents =
97 if tree
98 then dbg4 "acctswithparents" $
99 sortAccountNamesByDeclaration j tree $ -- re-sort after adding parents
100 expandAccountNames sortedaccts -- add all parent accounts
101 else sortedaccts
102
103 -- 3. if there's a depth limit, depth-clip and remove any no longer useful items
104 clippedaccts =
105 dbg4 "clippedaccts" $
106 (if tree then id else filter (matchesAccount acctq)) $ -- in tree mode, keep parent accounts even if they don't match
107 nub $ -- clipping can leave duplicates (adjacent, hopefully)
108 filter (not . T.null) $ -- depth:0 can leave nulls
109 map (clipAccountName dep) $ -- clip at depth if specified
110 acctswithparents -- use expanded list instead of sortedaccts
111
112 -- 4. print what remains as a list or tree, maybe applying --drop in the former case.
113 -- Add various bits of info if enabled.
114 let
115 showKeyword = if directives then "account " else ""
116 -- some contortions here to show types nicely aligned
117 showName a = case accountlistmode_ ropts of
118 ALTree -> indent <> accountLeafName droppedName
119 ALFlat -> droppedName
120 where
121 indent = T.replicate (2 * (max 0 (accountNameLevel a - drop_ ropts) - 1)) " "
122 droppedName = accountNameDrop (drop_ ropts) a
123 showType a =
124 case (types, journalAccountType j a) of
125 (True, Just t) -> pad a <> " ; type: " <> T.pack (show t)
126 _ -> ""
127 showAcctDeclOrder a
128 | locations =
129 (if types then "," else pad a <> " ;") <>
130 case lookup a $ jdeclaredaccounts j of
131 Just adi ->
132 " declared at " <> (T.pack $ sourcePosPretty $ adisourcepos adi) <> -- TODO: hide the column number
133 ", overall declaration order " <> (T.pack $ show $ adideclarationorder adi)
134 Nothing -> " undeclared"
135 | otherwise = ""
136 pad a = T.replicate (maxwidth - T.length (showName a)) " "
137 maxwidth = maximum $ map (T.length . showName) clippedaccts
138
139 forM_ clippedaccts $ \a -> T.putStrLn $ showKeyword <> showName a <> showType a <> showAcctDeclOrder a