imp: areg: do not abbrev other accts in machine-readable output (#1995)

- Do not turn "other accounts" into a comma-delimited string when the
  report is constructed, but pass `AccountName`s up the chain. This
  requires modifying the `AccountTransactionsReportItem` type to contain
  `[AccountName]` rather than `Text`.

- Perform the account name summarization
  (`Hledger.Data.AccountName.accountSummarizedName`) closer to the
  actual rendering of the report, so that different report formats can
  choose summarization strategy.

- Continue to summarize as before for terminal/text output (ie.
  human-readable). Do not summarize any more for machine-readable output
  (csv/html/fods).
This commit is contained in:
savanto 2025-06-27 13:22:41 -05:00 committed by Simon Michael
parent d2692a5518
commit 230998136f
3 changed files with 16 additions and 19 deletions

View File

@ -27,7 +27,6 @@ import Data.List (mapAccumR, nub, partition, sortBy)
import Data.List.Extra (nubSort)
import Data.Maybe (catMaybes)
import Data.Ord (Down(..), comparing)
import Data.Text (Text)
import qualified Data.Text as T
import Data.Time.Calendar (Day)
@ -80,7 +79,7 @@ type AccountTransactionsReportItem =
Transaction -- the transaction, unmodified
,Transaction -- the transaction, as seen from the current account
,Bool -- is this a split (more than one posting to other accounts) ?
,Text -- a display string describing the other account(s), if any
,[AccountName] -- the other account(s), if any
,MixedAmount -- the amount posted to the current account(s) (or total amount posted)
,MixedAmount -- the register's running total or the current account(s)'s historical balance, after this transaction
)
@ -188,14 +187,14 @@ accountTransactionsReportItem reportq thisacctq signfn accttypefn bal (d, t)
-- 201407: I've lost my grip on this, let's just hope for the best
-- 201606: we now calculate change and balance from filtered postings, check this still works well for all callers XXX
| null reportps = (bal, Nothing) -- no matched postings in this transaction, skip it
| otherwise = (bal', Just (t, tacct{tdate=d}, numotheraccts > 1, otheracctstr, amt, bal'))
| otherwise = (bal', Just (t, tacct{tdate=d}, numotheraccts > 1, otheraccts, amt, bal'))
where
tacct@Transaction{tpostings=reportps} = filterTransactionPostingsExtra accttypefn reportq t -- TODO needs to consider --date2, #1731
(thisacctps, otheracctps) = partition (matchesPosting thisacctq) reportps
numotheraccts = length $ nub $ map paccount otheracctps
otheracctstr | thisacctq == None = summarisePostingAccounts reportps -- no current account ? summarise all matched postings
| numotheraccts == 0 = summarisePostingAccounts thisacctps -- only postings to current account ? summarise those
| otherwise = summarisePostingAccounts otheracctps -- summarise matched postings to other account(s)
otheraccts | thisacctq == None = summarisePostingAccounts reportps -- no current account ? summarise all matched postings
| numotheraccts == 0 = summarisePostingAccounts thisacctps -- only postings to current account ? summarise those
| otherwise = summarisePostingAccounts otheracctps -- summarise matched postings to other account(s)
-- 202302: Impact of t on thisacct - normally the sum of thisacctps,
-- but if they are null it probably means reportq is an account filter
-- and we should sum otheracctps instead.
@ -236,9 +235,8 @@ transactionRegisterDate wd reportq thisacctq t
-- | Generate a simplified summary of some postings' accounts.
-- To reduce noise, if there are both real and virtual postings, show only the real ones.
summarisePostingAccounts :: [Posting] -> Text
summarisePostingAccounts ps =
T.intercalate ", " . map accountSummarisedName . nub $ map paccount displayps
summarisePostingAccounts :: [Posting] -> [AccountName]
summarisePostingAccounts ps = map paccount displayps
where
realps = filter isReal ps
displayps | null realps = ps

View File

@ -42,6 +42,7 @@ where
import Brick.Widgets.List (listMoveTo, listSelectedElement, list)
import Data.List
import Data.Maybe
import qualified Data.Text as T
import Data.Time.Calendar (Day, diffDays)
import Safe
import qualified Data.Vector as V
@ -280,11 +281,11 @@ rsUpdate uopts d j rss@RSS{_rssAccount, _rssForceInclusive, _rssList=oldlist} =
-- pre-render the list items, helps calculate column widths
displayitems = map displayitem items'
where
displayitem (t, _, _issplit, otheracctsstr, change, bal) =
displayitem (t, _, _issplit, otheraccts, change, bal) =
RegisterScreenItem{rsItemDate = showDate $ transactionRegisterDate wd (_rsQuery rspec') thisacctq t
,rsItemStatus = tstatus t
,rsItemDescription = tdescription t
,rsItemOtherAccounts = otheracctsstr
,rsItemOtherAccounts = T.intercalate ", " . map accountSummarisedName $ nub otheraccts
-- _ -> "<split>" -- should do this if accounts field width < 30
,rsItemChangeAmount = showamt change
,rsItemBalanceAmount = showamt bal

View File

@ -20,7 +20,7 @@ module Hledger.Cli.Commands.Aregister (
) where
import Data.Default (def)
import Data.List (find)
import Data.List (find, nub)
import Data.Maybe (fromMaybe)
import Data.Text (Text)
import Data.Foldable (for_)
@ -157,12 +157,12 @@ accountTransactionsReportItemAsRecord ::
[Spr.Cell Spr.NumLines Text]
accountTransactionsReportItemAsRecord
fmt internals wd reportq thisacctq
(t@Transaction{tindex,tcode,tdescription}, _, _issplit, otheracctsstr, change, balance)
(t@Transaction{tindex,tcode,tdescription}, _, _issplit, otheraccts, change, balance)
= (optional internals [Spr.integerCell tindex]) ++
date :
(optional internals [cell tcode]) ++
[cell tdescription,
cell otheracctsstr,
cell $ T.intercalate ", " $ nub otheraccts,
amountCell change,
amountCell balance]
where
@ -244,11 +244,11 @@ accountTransactionsReportItemAsText :: CliOpts -> Query -> Query -> Int -> Int
accountTransactionsReportItemAsText
copts@CliOpts{reportspec_=ReportSpec{_rsReportOpts=ropts}}
reportq thisacctq preferredamtwidth preferredbalwidth
((t@Transaction{tdescription}, _, _issplit, otheracctsstr, _, _), amt, bal) =
((t@Transaction{tdescription}, _, _issplit, otheraccts, _, _), amt, bal) =
-- Transaction -- the transaction, unmodified
-- Transaction -- the transaction, as seen from the current account
-- Bool -- is this a split (more than one posting to other accounts) ?
-- String -- a display string describing the other account(s), if any
-- [AccountName] -- the other account(s), if any
-- MixedAmount -- the amount posted to the current account(s) (or total amount posted)
-- MixedAmount -- the register's running total or the current account(s)'s historical balance, after this transaction
table <> TB.singleton '\n'
@ -287,9 +287,7 @@ accountTransactionsReportItemAsText
(descwidth, acctwidth) = (w, remaining - 2 - w)
where w = fromMaybe ((remaining - 2) `div` 2) mdescwidth
-- gather content
accts = -- T.unpack $ elideAccountName acctwidth $ T.pack
otheracctsstr
accts = T.intercalate ", " . map accountSummarisedName $ nub otheraccts
-- tests