hledger/bin/hledger-register-max.hs

57 lines
2.0 KiB
Haskell
Raw Normal View History

2023-01-29 19:43:42 +03:00
#!/usr/bin/env stack
2024-06-15 19:13:57 +03:00
-- stack runghc
-- (requires hledger > 1.34)
-- -- stack script --compile --resolver nightly-2024-MM-DD --verbosity error --package hledger-lib --package hledger --package text --package safe
2023-01-29 19:43:42 +03:00
-- hledger-register-max - runs "hledger register" and prints the posting with largest running total/balance.
2023-01-29 19:43:42 +03:00
-- Usage:
-- hledger-register-max [REGISTERARGS]
-- hledger register-max -- [REGISTERARGS]
2023-01-29 21:14:15 +03:00
-- For historical balances, add -H. To see the smallest, add --invert. For value, add -V --infer-market-prices, etc.
2023-01-29 19:43:42 +03:00
-- Examples:
-- $ hledger-register-max -f examples/bcexample.hledger -H checking
-- 2013-01-03 Hoogle | Payroll Assets:US:BofA:Checking 1350.60 USD 8799.22 USD
-- $ hledger register-max -- -f examples/bcexample.hledger income --invert
-- 2014-10-09 Hoogle | Payroll Income:US:Hoogle:Vacation 4.62 VACHR 52000.00 IRAUSD, 365071.44 USD, 337.26 VACHR
2023-01-29 19:43:42 +03:00
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE PackageImports #-}
import Control.Monad
import Data.List
import Data.Maybe
import Data.Ord
import qualified "text" Data.Text as T
import qualified Data.Text.IO as T
import Safe
2023-01-29 19:43:42 +03:00
import System.Environment
2024-06-15 19:13:57 +03:00
import Hledger.Cli.Script
2023-01-29 19:43:42 +03:00
-- XXX needs --help, see hledger-addon-example.hs
2023-01-29 19:43:42 +03:00
main = do
args <- getArgs
opts <- argsToCliOpts ("register" : args) []
2023-01-29 19:43:42 +03:00
withJournalDo opts $ \j -> do
let
r = postingsReport (reportspec_ opts) j
2024-06-15 19:13:57 +03:00
getamt = pamount.fourth5
maxbal = getamt $ maximumBy (comparing getamt) r
is = filter ((== maxbal).getamt) r
mapM_ printItem is
printItem (_, _, _, p, bal) = do
let
d = postingDate p
mt = ptransaction p
desc = fmt 30 $ maybe "-" tdescription mt
2023-01-29 21:14:15 +03:00
acct = fmt 30 $ paccount p
amt = fmta 12 $ T.pack $ showMixedAmountOneLine $ pamount p
baltxt = fmta 12 $ T.pack $ showMixedAmountOneLine bal
T.putStrLn $ T.unwords [showDate d, desc, "", acct, "", amt, " ", baltxt]
where
2023-01-29 21:14:15 +03:00
fmt w = formatText True (Just w) (Just w) . textElideRight w
fmta w = formatText False (Just w) Nothing