feat: accounts: --types shows account types (#1820)

This commit is contained in:
Simon Michael 2022-02-01 18:06:19 -10:00
parent dbbd300aa2
commit 807717805a
4 changed files with 42 additions and 10 deletions

View File

@ -151,7 +151,16 @@ data AccountType =
| Expense | Expense
| Cash -- ^ a subtype of Asset - liquid assets to show in cashflow report | Cash -- ^ a subtype of Asset - liquid assets to show in cashflow report
| Conversion -- ^ a subtype of Equity - account in which to generate conversion postings for transaction prices | Conversion -- ^ a subtype of Equity - account in which to generate conversion postings for transaction prices
deriving (Show,Eq,Ord,Generic) deriving (Eq,Ord,Generic)
instance Show AccountType where
show Asset = "A"
show Liability = "L"
show Equity = "E"
show Revenue = "R"
show Expense = "X"
show Cash = "C"
show Conversion = "V"
-- not worth the trouble, letters defined in accountdirectivep for now -- not worth the trouble, letters defined in accountdirectivep for now
--instance Read AccountType --instance Read AccountType

View File

@ -27,6 +27,7 @@ import System.Console.CmdArgs.Explicit as C
import Hledger import Hledger
import Hledger.Cli.CliOptions import Hledger.Cli.CliOptions
import Control.Monad (forM_)
-- | Command line options for this command. -- | Command line options for this command.
@ -34,6 +35,7 @@ accountsmode = hledgerCommandMode
$(embedFileRelative "Hledger/Cli/Commands/Accounts.txt") $(embedFileRelative "Hledger/Cli/Commands/Accounts.txt")
([flagNone ["declared"] (setboolopt "declared") "show account names declared with account directives" ([flagNone ["declared"] (setboolopt "declared") "show account names declared with account directives"
,flagNone ["used"] (setboolopt "used") "show account names referenced by transactions" ,flagNone ["used"] (setboolopt "used") "show account names referenced by transactions"
,flagNone ["types"] (setboolopt "types") "also show accounts' types, when known"
] ]
++ flattreeflags False ++ ++ flattreeflags False ++
[flagReq ["drop"] (\s opts -> Right $ setopt "drop" s opts) "N" "flat mode: omit N leading account name parts" [flagReq ["drop"] (\s opts -> Right $ setopt "drop" s opts) "N" "flat mode: omit N leading account name parts"
@ -50,6 +52,7 @@ accounts CliOpts{rawopts_=rawopts, reportspec_=ReportSpec{_rsQuery=query,_rsRepo
let tree = tree_ ropts let tree = tree_ ropts
declared = boolopt "declared" rawopts declared = boolopt "declared" rawopts
used = boolopt "used" rawopts used = boolopt "used" rawopts
types = boolopt "types" rawopts
-- a depth limit will clip and exclude account names later, but we don't want to exclude accounts at this stage -- a depth limit will clip and exclude account names later, but we don't want to exclude accounts at this stage
nodepthq = dbg1 "nodepthq" $ filterQuery (not . queryIsDepth) query nodepthq = dbg1 "nodepthq" $ filterQuery (not . queryIsDepth) query
-- just the acct: part of the query will be reapplied later, after clipping -- just the acct: part of the query will be reapplied later, after clipping
@ -77,12 +80,20 @@ accounts CliOpts{rawopts_=rawopts, reportspec_=ReportSpec{_rsQuery=query,_rsRepo
map (clipAccountName depth) $ -- clip at depth if specified map (clipAccountName depth) $ -- clip at depth if specified
sortedaccts sortedaccts
-- 4. print what remains as a list or tree, maybe applying --drop in the former case -- 4. print what remains as a list or tree, maybe applying --drop in the former case.
mapM_ (T.putStrLn . render) clippedaccts -- With --types, also show the account type.
where let
render a = case accountlistmode_ ropts of -- some contortions here to show types nicely aligned
ALTree -> T.replicate indent " " <> accountLeafName droppedName showName a = case accountlistmode_ ropts of
ALTree -> indent <> accountLeafName droppedName
ALFlat -> droppedName ALFlat -> droppedName
where where
indent = 2 * (max 0 (accountNameLevel a - drop_ ropts) - 1) indent = T.replicate (2 * (max 0 (accountNameLevel a - drop_ ropts) - 1)) " "
droppedName = accountNameDrop (drop_ ropts) a droppedName = accountNameDrop (drop_ ropts) a
showType a
| types = spacer <> " ; type: " <> maybe "" (T.pack . show) (journalAccountType j a)
| otherwise = ""
where
spacer = T.replicate (maxwidth - T.length (showName a)) " "
maxwidth = maximum $ map (T.length . showName) clippedaccts
forM_ clippedaccts $ \a -> T.putStrLn $ showName a <> showType a

View File

@ -12,6 +12,9 @@ show the account hierarchy.
In flat mode you can add `--drop N` to omit the first few account name components. In flat mode you can add `--drop N` to omit the first few account name components.
Account names can be depth-clipped with `depth:N` or `--depth N` or `-N`. Account names can be depth-clipped with `depth:N` or `--depth N` or `-N`.
With `--types`, it also shows each account's type, if it's known.
(See Declaring accounts > Account types.)
Examples: Examples:
```shell ```shell

View File

@ -30,3 +30,12 @@ aa
$ hledger -f - accounts tag:foo $ hledger -f - accounts tag:foo
a:aa a:aa
# 6. Show account types.
<
account asset
2022-01-01
(unknown) 1
$ hledger -f - accounts --types
asset ; type: A
unknown ; type: