accounts: more query support, --flat/--drop/--depth support

This commit is contained in:
Simon Michael 2014-04-07 12:13:36 -07:00
parent efad3a767d
commit 7b4b99fae0
2 changed files with 23 additions and 8 deletions

View File

@ -1,7 +1,12 @@
{-| {-|
The @accounts@ command lists the full names of the (query-restricted) The @accounts@ command lists account names:
accounts posted to . This is similar to ledger accounts -E.
- in flat mode (default), it lists the full names of accounts posted to by matched postings,
clipped to the specified depth, possibly with leading components dropped.
- in tree mode, it shows the indented short names of accounts posted to by matched postings,
and their parents, to the specified depth.
-} -}
@ -23,10 +28,12 @@ import Hledger.Cli.Options
-- | Command line options for this command. -- | Command line options for this command.
accountsmode = (defCommandMode $ ["accounts"] ++ aliases ++ hiddenaliases) { accountsmode = (defCommandMode $ ["accounts"] ++ aliases ++ hiddenaliases) {
modeHelp = "show names of accounts posted to" `withAliases` aliases modeHelp = "show account names" `withAliases` aliases
,modeGroupFlags = C.Group { ,modeGroupFlags = C.Group {
groupUnnamed = [ groupUnnamed = [
flagReq ["drop"] (\s opts -> Right $ setopt "drop" s opts) "N" "with --flat, omit this many leading account name components" flagNone ["flat"] (\opts -> setboolopt "flat" opts) "show full account names, as a list (default)"
,flagNone ["tree"] (\opts -> setboolopt "tree" opts) "show short account names, as a tree"
,flagReq ["drop"] (\s opts -> Right $ setopt "drop" s opts) "N" "in flat mode, omit this many leading account name components"
] ]
,groupHidden = [] ,groupHidden = []
,groupNamed = [generalflagsgroup1] ,groupNamed = [generalflagsgroup1]
@ -39,6 +46,14 @@ accounts :: CliOpts -> Journal -> IO ()
accounts CliOpts{reportopts_=ropts} j = do accounts CliOpts{reportopts_=ropts} j = do
d <- getCurrentDay d <- getCurrentDay
let q = queryFromOpts d ropts let q = queryFromOpts d ropts
mapM_ putStrLn $ filter (q `matchesAccount`) $ nub $ sort $ map paccount $ journalPostings j nodepthq = dbg "nodepthq" $ filterQuery (not . queryIsDepth) q
depth = dbg "depth" $ queryDepth $ filterQuery queryIsDepth q
ps = dbg "ps" $ journalPostings $ filterJournalPostings nodepthq j
as = dbg "as" $ nub $ filter (not . null) $ map (clipAccountName depth) $ sort $ map paccount ps
as' | tree_ ropts = expandAccountNames as
| otherwise = as
render a | tree_ ropts = replicate (2 * (accountNameLevel a - 1)) ' ' ++ accountLeafName a
| otherwise = accountNameDrop (drop_ ropts) a
mapM_ (putStrLn . render) as'
tests_Hledger_Cli_Accounts = TestList [] tests_Hledger_Cli_Accounts = TestList []

View File

@ -265,9 +265,9 @@ balancemode = (defCommandMode $ ["balance"] ++ aliases ++ ["bal"]) { -- also acc
groupUnnamed = [ groupUnnamed = [
flagNone ["cumulative"] (\opts -> setboolopt "cumulative" opts) "with a reporting interval, show accumulated totals starting from 0" flagNone ["cumulative"] (\opts -> setboolopt "cumulative" opts) "with a reporting interval, show accumulated totals starting from 0"
,flagNone ["historical","H"] (\opts -> setboolopt "historical" opts) "with a reporting interval, show accurate historical ending balances" ,flagNone ["historical","H"] (\opts -> setboolopt "historical" opts) "with a reporting interval, show accurate historical ending balances"
,flagNone ["flat"] (\opts -> setboolopt "flat" opts) "show full account names, as a list" ,flagNone ["flat"] (\opts -> setboolopt "flat" opts) "show full account names, as a list (default in multicolumn reports)"
,flagNone ["tree"] (\opts -> setboolopt "tree" opts) "show short account names, as a tree" ,flagNone ["tree"] (\opts -> setboolopt "tree" opts) "show short account names, as a tree (default in simple reports)"
,flagReq ["drop"] (\s opts -> Right $ setopt "drop" s opts) "N" "with --flat, omit this many leading account name components" ,flagReq ["drop"] (\s opts -> Right $ setopt "drop" s opts) "N" "in flat mode, omit this many leading account name components"
,flagReq ["format"] (\s opts -> Right $ setopt "format" s opts) "FORMATSTR" "use this custom line format" ,flagReq ["format"] (\s opts -> Right $ setopt "format" s opts) "FORMATSTR" "use this custom line format"
,flagNone ["no-elide"] (\opts -> setboolopt "no-elide" opts) "no eliding at all, stronger than --empty" ,flagNone ["no-elide"] (\opts -> setboolopt "no-elide" opts) "no eliding at all, stronger than --empty"
,flagNone ["no-total"] (\opts -> setboolopt "no-total" opts) "don't show the final total" ,flagNone ["no-total"] (\opts -> setboolopt "no-total" opts) "don't show the final total"