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.List.Extra (nubSort)
import Data.Maybe (catMaybes) import Data.Maybe (catMaybes)
import Data.Ord (Down(..), comparing) import Data.Ord (Down(..), comparing)
import Data.Text (Text)
import qualified Data.Text as T import qualified Data.Text as T
import Data.Time.Calendar (Day) import Data.Time.Calendar (Day)
@ -80,7 +79,7 @@ type AccountTransactionsReportItem =
Transaction -- the transaction, unmodified Transaction -- the transaction, unmodified
,Transaction -- the transaction, as seen from the current account ,Transaction -- the transaction, as seen from the current account
,Bool -- is this a split (more than one posting to other accounts) ? ,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 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 ,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 -- 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 -- 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 | 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 where
tacct@Transaction{tpostings=reportps} = filterTransactionPostingsExtra accttypefn reportq t -- TODO needs to consider --date2, #1731 tacct@Transaction{tpostings=reportps} = filterTransactionPostingsExtra accttypefn reportq t -- TODO needs to consider --date2, #1731
(thisacctps, otheracctps) = partition (matchesPosting thisacctq) reportps (thisacctps, otheracctps) = partition (matchesPosting thisacctq) reportps
numotheraccts = length $ nub $ map paccount otheracctps numotheraccts = length $ nub $ map paccount otheracctps
otheracctstr | thisacctq == None = summarisePostingAccounts reportps -- no current account ? summarise all matched postings otheraccts | thisacctq == None = summarisePostingAccounts reportps -- no current account ? summarise all matched postings
| numotheraccts == 0 = summarisePostingAccounts thisacctps -- only postings to current account ? summarise those | numotheraccts == 0 = summarisePostingAccounts thisacctps -- only postings to current account ? summarise those
| otherwise = summarisePostingAccounts otheracctps -- summarise matched postings to other account(s) | otherwise = summarisePostingAccounts otheracctps -- summarise matched postings to other account(s)
-- 202302: Impact of t on thisacct - normally the sum of thisacctps, -- 202302: Impact of t on thisacct - normally the sum of thisacctps,
-- but if they are null it probably means reportq is an account filter -- but if they are null it probably means reportq is an account filter
-- and we should sum otheracctps instead. -- and we should sum otheracctps instead.
@ -236,9 +235,8 @@ transactionRegisterDate wd reportq thisacctq t
-- | Generate a simplified summary of some postings' accounts. -- | Generate a simplified summary of some postings' accounts.
-- To reduce noise, if there are both real and virtual postings, show only the real ones. -- To reduce noise, if there are both real and virtual postings, show only the real ones.
summarisePostingAccounts :: [Posting] -> Text summarisePostingAccounts :: [Posting] -> [AccountName]
summarisePostingAccounts ps = summarisePostingAccounts ps = map paccount displayps
T.intercalate ", " . map accountSummarisedName . nub $ map paccount displayps
where where
realps = filter isReal ps realps = filter isReal ps
displayps | null realps = ps displayps | null realps = ps

View File

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

View File

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