imp: add --location tag to print command [#2368]

;fix: delete hledger-print-location, move doc to Print.hs>"print,other features"
This commit is contained in:
samahri 2025-04-18 12:24:04 -07:00 committed by Simon Michael
parent 588cab14f3
commit 6bf9369cda
4 changed files with 12 additions and 66 deletions

View File

@ -366,20 +366,6 @@ It has the same structure as most of the add-ons here:
- providing command line help
- accepting common hledger options
### hledger-print-location
[`hledger-print-location.hs`](https://github.com/simonmichael/hledger/blob/master/bin/hledger-print-location.hs)
is a variant of hledger's `print` command
that adds the file and line number to every transaction, as a tag:
```cli
$ hledger print-location -f hledger/examples/sample.journal desc:eat
2008/06/03 * eat & shop
; location: /Users/simon/src/hledger/examples/sample.journal:30
expenses:food $1
expenses:supplies $1
assets:cash
```
### hledger-swap-dates
[`hledger-swap-dates.hs`](https://github.com/simonmichael/hledger/blob/master/bin/hledger-swap-dates.hs)

View File

@ -1,49 +0,0 @@
#!/usr/bin/env stack
-- stack runghc --verbosity info --package hledger --package string-qq
-- Run from inside the hledger source tree, or compile with compile.sh.
-- See hledger-check-fancyassertions.hs.
{-
Quick script that adds file/line number tags to print output.
cf https://www.reddit.com/r/plaintextaccounting/comments/ddzn8o/finding_corresponding_journal_files_from_hledger/
$ hledger print-location -f examples/sample.journal desc:eat
2008/06/03 * eat & shop
; location: /Users/simon/src/hledger/examples/sample.journal:30
expenses:food $1
expenses:supplies $1
assets:cash
-}
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE QuasiQuotes #-}
import Data.String.QQ (s)
import qualified Data.Text as T
import Hledger.Cli.Script
------------------------------------------------------------------------------
cmdmode = hledgerCommandMode
[s| print-location
Like print, but adds tags showing the file path and location of transactions.
|]
[]
[generalflagsgroup1]
[]
([], Just $ argsFlag "[QUERY]")
------------------------------------------------------------------------------
main :: IO ()
main = do
opts <- getHledgerCliOpts cmdmode
withJournalDo opts $ \j ->
print' opts j{jtxns = map addLocationTag $ jtxns j}
addLocationTag :: Transaction -> Transaction
addLocationTag t = t{tcomment = tcomment t `commentAddTagNextLine` loctag}
where
loctag = ("location", T.pack . sourcePosPairPretty $ tsourcepos t)
-- Like showSourcePosPair in Hledger.Data.Transaction, but show just the starting line number.
showSourcePosPairLine :: (SourcePos, SourcePos) -> String
showSourcePosPairLine (SourcePos f line _, _) = f ++ ":" ++ show line

View File

@ -60,7 +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"
,flagNone ["location"] (setboolopt "location") "add file/line number tags to print output"
,outputFormatFlag ["txt","beancount","csv","tsv","html","fods","json","sql"]
,outputFileFlag
])
@ -366,7 +366,6 @@ postingToSpreadsheet fmt baseUrl query 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

View File

@ -29,7 +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.
--location add file/line number tags to print output.
```
The print command displays full journal entries (transactions)
@ -139,6 +139,16 @@ DESC should contain at least two characters.
If there is no similar-enough match,
no transaction will be shown and the program exit code will be non-zero.
With `--location`, print adds the file and line number to every transaction, as a tag:
```cli
$ hledger print-location -f hledger/examples/sample.journal desc:eat
2008/06/03 * eat & shop
; location: /Users/simon/src/hledger/examples/sample.journal:30
expenses:food $1
expenses:supplies $1
assets:cash
```
### print output format