-
-
Notifications
You must be signed in to change notification settings - Fork 316
/
hledger-script-example.hs
executable file
·148 lines (118 loc) · 5.39 KB
/
hledger-script-example.hs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
#!/usr/bin/env stack
-- stack runghc --verbosity error --package hledger
-- This script is run by the "stack runghc" command above.
-- This command expects to be running in a copy of the hledger source code.
-- Its advantage is that it uses that latest hledger source version.
-- To show more progress output, change --verbosity to info.
-- The main haskell package needed for scripting is "hledger".
-- To make more packages available for import, add more --package options.
--
-- For more robustness, use the "stack script" command below instead.
-- This uses a released version of hledger, and does not need the hledger source code.
-- The version of hledger (and haskell libraries, and ghc) is determined by
-- the stackage snapshot (lts-X.Y or nightly-YYYY-MM-DD).
--
-- stack script --snapshot lts-22.27 --verbosity error --package hledger --package text
-- (https://www.stackage.org/lts-22.27 with hledger-1.32.3, ghc-9.6.5.)
--
-- stack script --snapshot nightly-2024-09-04 --verbosity error --package hledger --package hledger-lib --package text --compile
-- (https://www.stackage.org/nightly-2024-09-04 with hledger-1.34, ghc-9.8.2.
-- The --compile flag makes it compile the script to a faster binary.)
--
------------------------------------78----------------------------------------
{-# LANGUAGE OverloadedStrings, PackageImports #-}
import Hledger.Cli.Script
import qualified "text" Data.Text as T
import qualified "text" Data.Text.IO as T
cmdmode = hledgerCommandMode (unlines
-- Command name, then --help text, then _FLAGS; empty help lines get stripped:
["script-example"
,"This is an example of a (hledger-lib-using) hledger script."
,"Usage: hledger-script-example [OPTS] [ARGS]"
,"or: hledger script-example -- [OPTS] [ARGS]"
,"Save it under another name and customise it."
,"The hledger- name makes it appear in hledger's commands list."
,"Examples:"
,"$ hledger-script-example --help"
,"(this help)"
------------------------------------78----------------------------------------
,""
,"_FLAGS"
])
[] [generalflagsgroup1] [] ([], Just $ argsFlag "[ARGS]") -- or Nothing
main = do
opts@CliOpts{reportspec_=rspec} <- getHledgerCliOpts cmdmode
withJournalDo opts $ \j -> do
putStrLn "it worked! print something more useful here"
-- Examples:
-- See also bin/*.hs
-- Count transactions, possibly filtered by a query:
-- d <- getCurrentDay
-- let
-- q = _rsQuery rspec
-- ts = filter (q `matchesTransaction`) $ jtxns $ journalApplyValuationFromOpts rspec j
-- printf "File %s: %d transactions\n" (journalFilePath j) (length ts)
-- register-max:
-- withJournalDo opts $ \j -> do
-- let
-- postingReportItems = postingsReport rspec j
-- maxbal = fifth5 $ maximumBy (comparing fifth5) r
-- is = filter ((== maxbal).fifth5) r
-- mapM_ printItem is
--
-- printItem (_, _, _, p, bal) = do
-- let
-- d = postingDate p
-- mt = ptransaction p
-- desc = fmt 30 $ maybe "-" tdescription mt
-- 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
-- fmt w = formatText True (Just w) (Just w) . textElideRight w
-- fmta w = formatText False (Just w) Nothing
-- Using [s|...|] for multiline string literals (requires string-qq package and {-# LANGUAGE QuasiQuotes #-}):
-- cmdmode = hledgerCommandMode (unlines
-- -- Command name, then --help text, then _FLAGS; empty help lines get stripped:
-- [s| script-example
-- This is an example of a (hledger-lib-using) hledger script."
-- Usage: hledger-script-example [OPTS] [ARGS]"
-- or: hledger script-example -- [OPTS] [ARGS]"
-- Save it under another name and customise it."
-- The hledger- name makes it appear in hledger's commands list."
-- Examples:"
-- $ hledger-script-example --help"
-- (this help)"
--
-- _FLAGS
-- |]
-- ------------------------------------78----------------------------------------
-- [] [generalflagsgroup1] [] ([], Just $ argsFlag "[ARGS]") -- or Nothing
{-
More help:
This an example of an addon command (an executable named hledger-*).
It supports many of the usual hledger options; run it with -h/--help
to see them. When you want to create a new hledger command,
save this script under a new name, somewhere in $PATH, give it
execute permission, and start tweaking the code.
Requirements:
This is a stack script, best run or compiled with stack.
Once compiled it doesn't require stack.
If you prefer you can adapt it to be a cabal script,
or you can manually install the required haskell libraries
(see above) and then run/compile it just with ghc or runghc.
If run/compiled from inside the hledger source tree, it will use that hledger
version and the libs of the stackage resolver in stack.yaml.
If run/compiled from outside the hledger source tree, it will use the hledger
and libs of the resolver in ~/.stack/global-project/stack.yaml.
Or you can specify a --resolver in the stack command above.
Usage:
Executing this script will cause stack to run it in interpreted mode:
$ hledger-script-example.hs
Or you can compile first:
$ stack ghc hledger-script-example.hs --package hledger --package string-qq
$ hledger-script-example
Whether compiled or not, you can also run it as a hledger subcommand, if it is in $PATH:
$ hledger script-example
-}