From 35e2e942287baeed7c780dc2a2d7d3995dfeb3d7 Mon Sep 17 00:00:00 2001 From: Simon Michael Date: Mon, 25 Sep 2017 06:17:46 -1000 Subject: [PATCH] bal/bs/cf/is: --sort-amount sorts by amount instead of account name --- hledger-lib/Hledger/Reports/BalanceReport.hs | 2 ++ hledger-lib/Hledger/Reports/MultiBalanceReports.hs | 8 +++++++- hledger-lib/Hledger/Reports/ReportOptions.hs | 3 +++ hledger/Hledger/Cli/Commands/Balance.hs | 1 + hledger/Hledger/Cli/CompoundBalanceCommand.hs | 10 +++++----- hledger/doc/balance.m4.md | 3 +++ hledger/doc/commands.m4.md | 9 +++++++++ 7 files changed, 30 insertions(+), 6 deletions(-) diff --git a/hledger-lib/Hledger/Reports/BalanceReport.hs b/hledger-lib/Hledger/Reports/BalanceReport.hs index 1e8f6db97..7f4abcfe4 100644 --- a/hledger-lib/Hledger/Reports/BalanceReport.hs +++ b/hledger-lib/Hledger/Reports/BalanceReport.hs @@ -92,6 +92,7 @@ balanceReport opts q j = (items, total) dbg1 "accts" $ take 1 $ clipAccountsAndAggregate (queryDepth q) $ flattenAccounts accts | flat_ opts = dbg1 "accts" $ + maybesort $ filterzeros $ filterempty $ drop 1 $ clipAccountsAndAggregate (queryDepth q) $ flattenAccounts accts @@ -107,6 +108,7 @@ balanceReport opts q j = (items, total) filterempty = filter (\a -> anumpostings a > 0 || not (isZeroMixedAmount (balance a))) prunezeros = if empty_ opts then id else fromMaybe nullacct . pruneAccounts (isZeroMixedAmount . balance) markboring = if no_elide_ opts then id else markBoringParentAccounts + maybesort = if sort_amount_ opts then sortBy (flip $ comparing balance) else id items = dbg1 "items" $ map (balanceReportItem opts q) accts' total | not (flat_ opts) = dbg1 "total" $ sum [amt | (_,_,indent,amt) <- items, indent == 0] | otherwise = dbg1 "total" $ diff --git a/hledger-lib/Hledger/Reports/MultiBalanceReports.hs b/hledger-lib/Hledger/Reports/MultiBalanceReports.hs index d6f3a6200..8ad27283c 100644 --- a/hledger-lib/Hledger/Reports/MultiBalanceReports.hs +++ b/hledger-lib/Hledger/Reports/MultiBalanceReports.hs @@ -169,7 +169,13 @@ multiBalanceReport opts q j = MultiBalanceReport (displayspans, items, totalsrow [(a, map snd abs) | abs@((a,_):_) <- transpose acctBalChangesPerSpan] -- never null, or used when null... items :: [MultiBalanceReportRow] = - dbg1 "items" + dbg1 "items" $ + (if sort_amount_ opts && accountlistmode_ opts /= ALTree + then sortBy (flip $ comparing $ + -- sort by average when that is displayed, instead of total. + -- Usually equivalent, but perhaps not in future. + if average_ opts then sixth6 else fifth6) + else id) $ [(a, accountLeafName a, accountNameLevel a, displayedBals, rowtot, rowavg) | (a,changes) <- acctBalChanges , let displayedBals = case balancetype_ opts of diff --git a/hledger-lib/Hledger/Reports/ReportOptions.hs b/hledger-lib/Hledger/Reports/ReportOptions.hs index 193108c53..1f20dd61e 100644 --- a/hledger-lib/Hledger/Reports/ReportOptions.hs +++ b/hledger-lib/Hledger/Reports/ReportOptions.hs @@ -97,6 +97,7 @@ data ReportOpts = ReportOpts { ,no_total_ :: Bool ,value_ :: Bool ,pretty_tables_ :: Bool + ,sort_amount_ :: Bool ,color_ :: Bool } deriving (Show, Data, Typeable) @@ -126,6 +127,7 @@ defreportopts = ReportOpts def def def + def rawOptsToReportOpts :: RawOpts -> IO ReportOpts rawOptsToReportOpts rawopts = checkReportOpts <$> do @@ -153,6 +155,7 @@ rawOptsToReportOpts rawopts = checkReportOpts <$> do ,row_total_ = boolopt "row-total" rawopts' ,no_total_ = boolopt "no-total" rawopts' ,value_ = boolopt "value" rawopts' + ,sort_amount_ = boolopt "sort-amount" rawopts' ,pretty_tables_ = boolopt "pretty-tables" rawopts' ,color_ = color } diff --git a/hledger/Hledger/Cli/Commands/Balance.hs b/hledger/Hledger/Cli/Commands/Balance.hs index 18777335c..9190ba420 100644 --- a/hledger/Hledger/Cli/Commands/Balance.hs +++ b/hledger/Hledger/Cli/Commands/Balance.hs @@ -282,6 +282,7 @@ balancemode = (defCommandMode $ ["balance"] ++ aliases) { -- also accept but don ,flagNone ["no-elide"] (\opts -> setboolopt "no-elide" opts) "don't squash boring parent accounts (in tree mode)" ,flagReq ["format"] (\s opts -> Right $ setopt "format" s opts) "FORMATSTR" "use this custom line format (in simple reports)" ,flagNone ["pretty-tables"] (\opts -> setboolopt "pretty-tables" opts) "use unicode when displaying tables" + ,flagNone ["sort-amount","S"] (\opts -> setboolopt "sort-amount" opts) "sort by amount/total/average (in flat mode)" ] ++ outputflags ,groupHidden = [] diff --git a/hledger/Hledger/Cli/CompoundBalanceCommand.hs b/hledger/Hledger/Cli/CompoundBalanceCommand.hs index 7fd5a890d..71f8f44ae 100644 --- a/hledger/Hledger/Cli/CompoundBalanceCommand.hs +++ b/hledger/Hledger/Cli/CompoundBalanceCommand.hs @@ -65,6 +65,7 @@ compoundBalanceCommandMode CompoundBalanceCommandSpec{..} = (defCommandMode $ cb ,flagNone ["no-elide"] (\opts -> setboolopt "no-elide" opts) "don't squash boring parent accounts (in tree mode)" ,flagReq ["format"] (\s opts -> Right $ setopt "format" s opts) "FORMATSTR" "use this custom line format (in simple reports)" ,flagNone ["pretty-tables"] (\opts -> setboolopt "pretty-tables" opts) "use unicode when displaying tables" + ,flagNone ["sort-amount","S"] (\opts -> setboolopt "sort-amount" opts) "sort by amount/total/average (in flat mode)" ,outputFormatFlag ,outputFileFlag ] @@ -175,11 +176,6 @@ compoundBalanceCommand CompoundBalanceCommandSpec{..} opts@CliOpts{command_=cmd, "csv" -> printCSV (compoundBalanceReportAsCsv ropts cbr) ++ "\n" _ -> compoundBalanceReportAsText ropts' cbr --- | Render a multi-column balance report as plain text suitable for console output. --- Add the second table below the first, discarding its column headings. -concatTables (Table hLeft hTop dat) (Table hLeft' _ dat') = - Table (T.Group DoubleLine [hLeft, hLeft']) hTop (dat ++ dat') - -- | Run one subreport for a compound balance command in single-column mode. -- Currently this returns the plain text rendering of the subreport, and its total. -- The latter is wrapped in a Sum for easy monoidal combining. @@ -290,6 +286,10 @@ compoundBalanceReportAsText ropts (title, subreports, (coltotals, grandtotal, gr -- tweak the layout t = Table (T.Group SingleLine [Header title, lefthdrs]) tophdrs ([]:cells) +-- | Add the second table below the first, discarding its column headings. +concatTables (Table hLeft hTop dat) (Table hLeft' _ dat') = + Table (T.Group DoubleLine [hLeft, hLeft']) hTop (dat ++ dat') + -- | Render a compound balance report as CSV. {- Eg: ghci> :main -f examples/sample.journal bs -Y -O csv -AT diff --git a/hledger/doc/balance.m4.md b/hledger/doc/balance.m4.md index 22c3829f9..150672429 100644 --- a/hledger/doc/balance.m4.md +++ b/hledger/doc/balance.m4.md @@ -44,6 +44,9 @@ txt, csv. `--pretty-tables` : Use unicode to display prettier tables. +`--sort-amount` +: Sort by amount (total row amount, or by average if that is displayed), instead of account name (in flat mode) + The balance command displays accounts and balances. It is hledger's most featureful and versatile command. diff --git a/hledger/doc/commands.m4.md b/hledger/doc/commands.m4.md index f6d10d4cc..cbcf039fb 100644 --- a/hledger/doc/commands.m4.md +++ b/hledger/doc/commands.m4.md @@ -206,6 +206,9 @@ Show a balance sheet. Alias: bs. `--format=LINEFORMAT` : in single-column balance reports: use this custom line format +`--sort-amount` +: Sort by amount (total row amount, or by average if that is displayed), instead of account name (in flat mode) + This command displays a simple [balance sheet](http://en.wikipedia.org/wiki/Balance_sheet). It currently assumes that you have top-level accounts named `asset` and `liability` (plural forms also @@ -313,6 +316,9 @@ Show a cashflow statement. Alias: cf. `--format=LINEFORMAT` : in single-column balance reports: use this custom line format +`--sort-amount` +: Sort by amount (total row amount, or by average if that is displayed), instead of account name (in flat mode) + This command displays a simple [cashflow statement](http://en.wikipedia.org/wiki/Cash_flow_statement) It shows the change in all "cash" (ie, liquid assets) accounts for the @@ -437,6 +443,9 @@ Show an income statement. Alias: is. `--format=LINEFORMAT` : in single-column balance reports: use this custom line format +`--sort-amount` +: Sort by amount (total row amount, or by average if that is displayed), instead of account name (in flat mode) + This command displays a simple [income statement](http://en.wikipedia.org/wiki/Income_statement). It currently assumes that you have top-level accounts named `income` (or