;bin: rename hledger-script-example.hs; use fewer imports and deps
This commit is contained in:
parent
b49b760497
commit
5ad1aab288
@ -1,79 +0,0 @@
|
||||
#!/usr/bin/env stack
|
||||
-- stack runghc --verbosity info --package hledger --package string-qq
|
||||
--resolver lts-20.1
|
||||
|
||||
{-
|
||||
hledger-addon-example - a hledger addon command template.
|
||||
|
||||
This an example of an addon command (an executable named hledger-*).
|
||||
By default it reads your default journal and prints the number of
|
||||
transactions. 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,
|
||||
keeping it executable, and start tweaking the code.
|
||||
|
||||
Requirements:
|
||||
|
||||
This is a stack script, requiring stack to run. hledger addons do not
|
||||
have to be stack scripts, but this one is, as they work well for this.
|
||||
If you prefer you can adapt it to be a cabal script, or you can
|
||||
install the required haskell libraries (see above) and then
|
||||
run/compile it with a suitable runghc/ghc command.
|
||||
|
||||
The script may require specific versions of the libraries.
|
||||
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 uncomment --resolver above to use another resolver.
|
||||
|
||||
Usage:
|
||||
|
||||
Executing this script will cause stack to run it in interpreted mode:
|
||||
|
||||
$ hledger-addon-example.hs
|
||||
|
||||
Or you can compile first:
|
||||
|
||||
$ stack ghc hledger-addon-example.hs --package hledger --package string-qq
|
||||
$ hledger-addon-example
|
||||
|
||||
Whether compiled or not, you can also run it as a hledger subcommand, if it is in $PATH:
|
||||
|
||||
$ hledger addon-example
|
||||
|
||||
-}
|
||||
|
||||
{-# LANGUAGE NamedFieldPuns #-}
|
||||
{-# LANGUAGE QuasiQuotes #-}
|
||||
{-# LANGUAGE RecordWildCards #-}
|
||||
|
||||
import Data.String.QQ (s)
|
||||
import Text.Printf
|
||||
import Hledger
|
||||
import Hledger.Cli
|
||||
|
||||
------------------------------------------------------------------------------
|
||||
cmdmode = hledgerCommandMode
|
||||
-- Command name and help text goes here. Current limitations:
|
||||
-- help text must be above _FLAGS, blank lines will not be displayed.
|
||||
[s| addon-example
|
||||
Print the number of transactions in the journal.
|
||||
|
||||
_FLAGS
|
||||
|]
|
||||
[]
|
||||
[generalflagsgroup1]
|
||||
[]
|
||||
([], Nothing) -- Just $ argsFlag "[QUERY]")
|
||||
------------------------------------------------------------------------------
|
||||
|
||||
main :: IO ()
|
||||
main = do
|
||||
opts@CliOpts{reportspec_=rspec} <- getHledgerCliOpts cmdmode
|
||||
withJournalDo opts $ \j -> do
|
||||
d <- getCurrentDay
|
||||
let
|
||||
q = _rsQuery rspec
|
||||
ts = filter (q `matchesTransaction`) $ jtxns $ journalApplyValuationFromOpts rspec j
|
||||
printf "File %s: %d transactions\n" (journalFilePath j) (length ts)
|
||||
134
bin/hledger-script-example.hs
Executable file
134
bin/hledger-script-example.hs
Executable file
@ -0,0 +1,134 @@
|
||||
#!/usr/bin/env stack
|
||||
-- stack runghc --verbosity error --package hledger
|
||||
-- stack runghc --verbosity error --package hledger --package hledger-lib --package text --package safe
|
||||
-- stack script --compile --resolver lts-20.8 --verbosity error --package hledger --package text
|
||||
-- stack script --compile --resolver lts-20.8 --verbosity error --package hledger --package hledger-lib --package text --package safe
|
||||
-- The topmost stack command above is used to run this script.
|
||||
-- stack script uses released hledger, stack runghc uses local hledger source.
|
||||
-- This script currently requires local hledger source, for Hledger.Cli.Script.
|
||||
------------------------------------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, requiring stack to run. hledger addons do not
|
||||
have to be stack scripts, but this one is, as they work well for this.
|
||||
If you prefer you can adapt it to be a cabal script, or you can
|
||||
install the required haskell libraries (see above) and then
|
||||
run/compile it with a suitable runghc/ghc command.
|
||||
The script may require specific versions of the libraries.
|
||||
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
|
||||
|
||||
-}
|
||||
Loading…
Reference in New Issue
Block a user