Skip to content

Commit

Permalink
imp:print: support --invert [#2314]
Browse files Browse the repository at this point in the history
  • Loading branch information
simonmichael committed Jan 27, 2025
1 parent aef59f8 commit 3e838e4
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 2 deletions.
7 changes: 6 additions & 1 deletion hledger-lib/Hledger/Data/Transaction.hs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ module Hledger.Data.Transaction
, transactionMapPostings
, transactionMapPostingAmounts
, transactionAmounts
, transactionNegate
, partitionAndCheckConversionPostings
, transactionAddTags
, transactionAddHiddenAndMaybeVisibleTag
Expand Down Expand Up @@ -478,10 +479,14 @@ transactionMapPostings f t@Transaction{tpostings=ps} = t{tpostings=map f ps}
transactionMapPostingAmounts :: (MixedAmount -> MixedAmount) -> Transaction -> Transaction
transactionMapPostingAmounts f = transactionMapPostings (postingTransformAmount f)

-- | All posting amounts from this transactin, in order.
-- | All posting amounts from this transaction, in order.
transactionAmounts :: Transaction -> [MixedAmount]
transactionAmounts = map pamount . tpostings

-- | Flip the sign of this transaction's posting amounts.
transactionNegate :: Transaction -> Transaction
transactionNegate = transactionMapPostingAmounts negate

-- | The file path from which this transaction was parsed.
transactionFile :: Transaction -> FilePath
transactionFile Transaction{tsourcepos} = sourceName $ fst tsourcepos
Expand Down
4 changes: 3 additions & 1 deletion hledger-lib/Hledger/Reports/EntriesReport.hs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,9 @@ type EntriesReportItem = Transaction
-- | Select transactions for an entries report.
entriesReport :: ReportSpec -> Journal -> EntriesReport
entriesReport rspec@ReportSpec{_rsReportOpts=ropts} =
sortBy (comparing $ transactionDateFn ropts) . jtxns
sortBy (comparing $ transactionDateFn ropts)
. map (if invert_ ropts then transactionNegate else id)
. jtxns
. journalApplyValuationFromOpts (setDefaultConversionOp NoConversionOp rspec)
. filterJournalTransactions (filterQuery (not.queryIsDepth) $ _rsQuery rspec)

Expand Down
1 change: 1 addition & 0 deletions hledger/Hledger/Cli/Commands/Print.hs
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ printmode = hledgerCommandMode
,flagNone ["show-costs"] (setboolopt "show-costs")
"show transaction prices even with conversion postings"
,roundFlag
,flagNone ["invert"] (setboolopt "invert") "display all amounts with reversed sign"
,flagNone ["new"] (setboolopt "new")
"show only newer-dated transactions added in each file since last run"
,let arg = "DESC" in
Expand Down
4 changes: 4 additions & 0 deletions hledger/Hledger/Cli/Commands/Print.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ Flags:
(can unbalance transactions)
all - also round cost amounts to precision
(can unbalance transactions)
--invert display all amounts with reversed sign
--new show only newer-dated transactions added in each
file since last run
-m --match=DESC fuzzy search for one recent transaction with
Expand Down Expand Up @@ -124,6 +125,9 @@ There are some situations where print's output can become unparseable:
With `-B`/`--cost`, amounts with [costs](https://hledger.org/hledger.html#costs)
are shown converted to cost.

With `--invert`, posting amounts are shown with their sign flipped.
It could be useful if you have accidentally recorded some transactions with the wrong signs.

With `--new`, print shows only transactions it has not seen on a previous run.
This uses the same deduplication system as the [`import`](#import) command.
(See import's docs for details.)
Expand Down
18 changes: 18 additions & 0 deletions hledger/test/print/print.test
Original file line number Diff line number Diff line change
Expand Up @@ -38,3 +38,21 @@ $ hledger -f- print --depth 1
A:AA 0 A

>=

# ** 4. print --invert flips signs.
<
2025-01-01
a 1 A @ 2 B
b
c 0 C

$ hledger -f- print --invert -x
2025-01-01
a -1 A @ 2 B
b 2 B
c 0 C

>=



0 comments on commit 3e838e4

Please sign in to comment.