From 588cab14f3d972aca1737fffe84b405daae486a2 Mon Sep 17 00:00:00 2001 From: samahri Date: Fri, 18 Apr 2025 12:24:04 -0700 Subject: [PATCH] imp: add --location tag to print command [#2368] --- hledger/Hledger/Cli/Commands/Print.hs | 10 +++++++++- hledger/Hledger/Cli/Commands/Print.md | 1 + 2 files changed, 10 insertions(+), 1 deletion(-) diff --git a/hledger/Hledger/Cli/Commands/Print.hs b/hledger/Hledger/Cli/Commands/Print.hs index a2e85c589..eab9c574e 100644 --- a/hledger/Hledger/Cli/Commands/Print.hs +++ b/hledger/Hledger/Cli/Commands/Print.hs @@ -60,6 +60,7 @@ printmode = hledgerCommandMode flagReq ["match","m"] (\s opts -> Right $ setopt "match" s opts) arg ("fuzzy search for one recent transaction with description closest to "++arg) ,flagReq ["base-url"] (\s opts -> Right $ setopt "base-url" s opts) "URLPREFIX" "in html output, generate links to hledger-web, with this prefix. (Usually the base url shown by hledger-web; can also be relative.)" + ,flagNone ["l", "location"] (setboolopt "location") "add file/line number tags to print output" ,outputFormatFlag ["txt","beancount","csv","tsv","html","fods","json","sql"] ,outputFileFlag ]) @@ -102,7 +103,7 @@ amountStylesSetRoundingFromRawOpts rawopts styles = -- | Print journal transactions in standard format. print' :: CliOpts -> Journal -> IO () -print' opts j = do +print' opts@CliOpts{rawopts_=rawopts} j = do -- The print command should show all amounts with their original decimal places, -- but as part of journal reading the posting amounts have already been normalised -- according to commodity display styles, and currently it's not easy to avoid @@ -115,6 +116,7 @@ print' opts j = do -- & dbg9With (lbl "amounts before setting full precision".showJournalAmountsDebug) & journalMapPostingAmounts mixedAmountSetFullPrecision -- & dbg9With (lbl "amounts after setting full precision: ".showJournalAmountsDebug) + & if boolopt "location" rawopts then journalMapTransactions addLocationTag else id case maybestringopt "match" $ rawopts_ opts of Nothing -> printEntries opts j' @@ -363,3 +365,9 @@ postingToSpreadsheet fmt baseUrl query p = status = T.pack . show $ pstatus p account = showAccountName Nothing (ptype p) (paccount p) comment = T.strip $ pcomment p + +-- from hledger/bin/hledger-print-location.hs +addLocationTag :: Transaction -> Transaction +addLocationTag t = t{tcomment = tcomment t `commentAddTagNextLine` loctag} + where + loctag = ("location", T.pack . sourcePosPairPretty $ tsourcepos t) \ No newline at end of file diff --git a/hledger/Hledger/Cli/Commands/Print.md b/hledger/Hledger/Cli/Commands/Print.md index 018d9861b..1dd3c4ffa 100644 --- a/hledger/Hledger/Cli/Commands/Print.md +++ b/hledger/Hledger/Cli/Commands/Print.md @@ -29,6 +29,7 @@ Flags: txt, beancount, csv, tsv, html, fods, json, sql. -o --output-file=FILE write output to FILE. A file extension matching one of the above formats selects that format. + -l --location add file/line number tags to print output. ``` The print command displays full journal entries (transactions)