From 807717805ad44f64c7ef50f445c5a8ed2a50925d Mon Sep 17 00:00:00 2001 From: Simon Michael Date: Tue, 1 Feb 2022 18:06:19 -1000 Subject: [PATCH] feat: accounts: --types shows account types (#1820) --- hledger-lib/Hledger/Data/Types.hs | 11 ++++++++- hledger/Hledger/Cli/Commands/Accounts.hs | 29 ++++++++++++++++-------- hledger/Hledger/Cli/Commands/Accounts.md | 3 +++ hledger/test/accounts.test | 9 ++++++++ 4 files changed, 42 insertions(+), 10 deletions(-) diff --git a/hledger-lib/Hledger/Data/Types.hs b/hledger-lib/Hledger/Data/Types.hs index fa0594c23..7cac48ec8 100644 --- a/hledger-lib/Hledger/Data/Types.hs +++ b/hledger-lib/Hledger/Data/Types.hs @@ -151,7 +151,16 @@ data AccountType = | Expense | 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 - 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 --instance Read AccountType diff --git a/hledger/Hledger/Cli/Commands/Accounts.hs b/hledger/Hledger/Cli/Commands/Accounts.hs index 82a71b078..960e50acd 100644 --- a/hledger/Hledger/Cli/Commands/Accounts.hs +++ b/hledger/Hledger/Cli/Commands/Accounts.hs @@ -27,6 +27,7 @@ import System.Console.CmdArgs.Explicit as C import Hledger import Hledger.Cli.CliOptions +import Control.Monad (forM_) -- | Command line options for this command. @@ -34,6 +35,7 @@ accountsmode = hledgerCommandMode $(embedFileRelative "Hledger/Cli/Commands/Accounts.txt") ([flagNone ["declared"] (setboolopt "declared") "show account names declared with account directives" ,flagNone ["used"] (setboolopt "used") "show account names referenced by transactions" + ,flagNone ["types"] (setboolopt "types") "also show accounts' types, when known" ] ++ flattreeflags False ++ [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 declared = boolopt "declared" 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 nodepthq = dbg1 "nodepthq" $ filterQuery (not . queryIsDepth) query -- 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 sortedaccts - -- 4. print what remains as a list or tree, maybe applying --drop in the former case - mapM_ (T.putStrLn . render) clippedaccts - where - render a = case accountlistmode_ ropts of - ALTree -> T.replicate indent " " <> accountLeafName droppedName - ALFlat -> droppedName - where - indent = 2 * (max 0 (accountNameLevel a - drop_ ropts) - 1) - droppedName = accountNameDrop (drop_ ropts) a + -- 4. print what remains as a list or tree, maybe applying --drop in the former case. + -- With --types, also show the account type. + let + -- some contortions here to show types nicely aligned + showName a = case accountlistmode_ ropts of + ALTree -> indent <> accountLeafName droppedName + ALFlat -> droppedName + where + indent = T.replicate (2 * (max 0 (accountNameLevel a - drop_ ropts) - 1)) " " + 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 diff --git a/hledger/Hledger/Cli/Commands/Accounts.md b/hledger/Hledger/Cli/Commands/Accounts.md index b89c81eac..f55337e20 100644 --- a/hledger/Hledger/Cli/Commands/Accounts.md +++ b/hledger/Hledger/Cli/Commands/Accounts.md @@ -12,6 +12,9 @@ show the account hierarchy. 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`. +With `--types`, it also shows each account's type, if it's known. +(See Declaring accounts > Account types.) + Examples: ```shell diff --git a/hledger/test/accounts.test b/hledger/test/accounts.test index 80cc4d3ec..a3c207914 100644 --- a/hledger/test/accounts.test +++ b/hledger/test/accounts.test @@ -30,3 +30,12 @@ aa $ hledger -f - accounts tag:foo a:aa +# 6. Show account types. +< +account asset +2022-01-01 + (unknown) 1 + +$ hledger -f - accounts --types +asset ; type: A +unknown ; type: