hledger/bin/hledger-print-location.hs

51 lines
1.7 KiB
Haskell
Raw Normal View History

#!/usr/bin/env stack
-- stack runghc --verbosity info --package hledger --package string-qq
2021-01-12 22:07:29 +03:00
-- Run from inside the hledger source tree, or compile with compile.sh.
-- See hledger-check-fancyassertions.hs.
2020-08-15 19:56:53 +03:00
{-
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
2020-08-15 19:56:53 +03:00
import Hledger.Cli
------------------------------------------------------------------------------
cmdmode = hledgerCommandMode
[s| print-location
Like print, but adds tags showing the file path and location of transactions.
_FLAGS
2020-08-15 19:56:53 +03:00
|]
[]
[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)
2020-08-15 19:56:53 +03:00
-- Like showSourcePosPair in Hledger.Data.Transaction, but show just the starting line number.
showSourcePosPairLine :: (SourcePos, SourcePos) -> String
showSourcePosPairLine (SourcePos f line _, _) = f ++ ":" ++ show line