{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE TemplateHaskell #-}
module Hledger.Cli.Commands (
commands
,mainmode
,runUnitTests
,builtinCommands
,builtinCommandNames
,addonCommandNames
,knownAddonCommandNames
,findBuiltinCommand
,knownCommands
,CommandCategories(..)
,commandCategories
,printCommandsList
,tests_Hledger_Cli
,module Hledger.Cli.Commands.Accounts
,module Hledger.Cli.Commands.Activity
,module Hledger.Cli.Commands.Add
,module Hledger.Cli.Commands.Aregister
,module Hledger.Cli.Commands.Balance
,module Hledger.Cli.Commands.Balancesheet
,module Hledger.Cli.Commands.Balancesheetequity
,module Hledger.Cli.Commands.Cashflow
,module Hledger.Cli.Commands.Check
,module Hledger.Cli.Commands.Close
,module Hledger.Cli.Commands.Codes
,module Hledger.Cli.Commands.Commodities
,module Hledger.Cli.Commands.Descriptions
,module Hledger.Cli.Commands.Diff
,module Hledger.Cli.Commands.Files
,module Hledger.Cli.Commands.Get
,module Hledger.Cli.Commands.Help
,module Hledger.Cli.Commands.Holdings
,module Hledger.Cli.Commands.Import
,module Hledger.Cli.Commands.Incomestatement
,module Hledger.Cli.Commands.Init
,module Hledger.Cli.Commands.Notes
,module Hledger.Cli.Commands.Payees
,module Hledger.Cli.Commands.Prices
,module Hledger.Cli.Commands.Print
,module Hledger.Cli.Commands.Register
,module Hledger.Cli.Commands.Rewrite
,module Hledger.Cli.Commands.Roi
,module Hledger.Cli.Commands.Run
,module Hledger.Cli.Commands.Setup
,module Hledger.Cli.Commands.Stats
,module Hledger.Cli.Commands.Tags
,module Hledger.Cli.Commands.Transactions
)
where
import Data.Char (isSpace, toLower)
import Data.Either (isRight)
import Data.List
import Data.List.Extra (groupSortOn, nubOrdOn, nubSort)
import Data.Text (Text)
import Data.Text qualified as T
import Data.Time.Calendar
import Safe (headErr, headMay, maximumDef)
import System.Console.CmdArgs.Explicit as C
import System.Environment (withArgs)
import System.FilePath (dropExtension, takeBaseName, takeExtension)
import Test.Tasty (defaultMain)
import Text.Megaparsec
import Text.Megaparsec.Char
import Hledger
import Hledger.Cli.CliOptions
import Hledger.Cli.Conf (CommandAlias, CommandLine, confAliases, getConf')
import Hledger.Cli.Commands.Accounts
import Hledger.Cli.Commands.Activity
import Hledger.Cli.Commands.Add
import Hledger.Cli.Commands.Aregister
import Hledger.Cli.Commands.Balance
import Hledger.Cli.Commands.Balancesheet
import Hledger.Cli.Commands.Balancesheetequity
import Hledger.Cli.Commands.Cashflow
import Hledger.Cli.Commands.Check
import Hledger.Cli.Commands.Close
import Hledger.Cli.Commands.Codes
import Hledger.Cli.Commands.Commodities
import Hledger.Cli.Commands.Descriptions
import Hledger.Cli.Commands.Diff
import Hledger.Cli.Commands.Files
import Hledger.Cli.Commands.Get
import Hledger.Cli.Commands.Help
import Hledger.Cli.Commands.Holdings
import Hledger.Cli.Commands.Import
import Hledger.Cli.Commands.Incomestatement
import Hledger.Cli.Commands.Init
import Hledger.Cli.Commands.Notes
import Hledger.Cli.Commands.Payees
import Hledger.Cli.Commands.Prices
import Hledger.Cli.Commands.Print
import Hledger.Cli.Commands.Register
import Hledger.Cli.Commands.Rewrite
import Hledger.Cli.Commands.Roi
import Hledger.Cli.Commands.Run
import Hledger.Cli.Commands.Setup
import Hledger.Cli.Commands.Stats
import Hledger.Cli.Commands.Tags
import Hledger.Cli.Commands.Transactions
import Hledger.Cli.Utils (tests_Cli_Utils, openBrowserOn)
import Hledger.Cli.Commands.Quickref (showQuickref)
import Hledger.Cli.DocFiles (runTldrForPage)
import Data.Functor ((<&>), void)
builtinCommands :: [(Mode RawOpts, CliOpts -> Journal -> IO ())]
builtinCommands :: [(Mode RawOpts, CliOpts -> Journal -> IO ())]
builtinCommands = [
(Mode RawOpts
accountsmode , CliOpts -> Journal -> IO ()
accounts)
,(Mode RawOpts
activitymode , CliOpts -> Journal -> IO ()
activity)
,(Mode RawOpts
addmode , CliOpts -> Journal -> IO ()
add)
,(Mode RawOpts
aregistermode , CliOpts -> Journal -> IO ()
aregister)
,(Mode RawOpts
balancemode , CliOpts -> Journal -> IO ()
balance)
,(Mode RawOpts
balancesheetequitymode , CliOpts -> Journal -> IO ()
balancesheetequity)
,(Mode RawOpts
balancesheetmode , CliOpts -> Journal -> IO ()
balancesheet)
,(Mode RawOpts
cashflowmode , CliOpts -> Journal -> IO ()
cashflow)
,(Mode RawOpts
checkmode , CliOpts -> Journal -> IO ()
check)
,(Mode RawOpts
closemode , CliOpts -> Journal -> IO ()
close)
,(Mode RawOpts
codesmode , CliOpts -> Journal -> IO ()
codes)
,(Mode RawOpts
commoditiesmode , CliOpts -> Journal -> IO ()
commodities)
,(Mode RawOpts
descriptionsmode , CliOpts -> Journal -> IO ()
descriptions)
,(Mode RawOpts
diffmode , CliOpts -> Journal -> IO ()
diff)
,(Mode RawOpts
filesmode , CliOpts -> Journal -> IO ()
files)
,(Mode RawOpts
getmode , CliOpts -> Journal -> IO ()
getcmd)
,(Mode RawOpts
helpmode , CliOpts -> Journal -> IO ()
help)
,(Mode RawOpts
holdingsmode , CliOpts -> Journal -> IO ()
holdings)
,(Mode RawOpts
importmode , CliOpts -> Journal -> IO ()
importcmd)
,(Mode RawOpts
incomestatementmode , CliOpts -> Journal -> IO ()
incomestatement)
,(Mode RawOpts
initmode , CliOpts -> Journal -> IO ()
initcmd)
,(Mode RawOpts
notesmode , CliOpts -> Journal -> IO ()
notes)
,(Mode RawOpts
payeesmode , CliOpts -> Journal -> IO ()
payees)
,(Mode RawOpts
pricesmode , CliOpts -> Journal -> IO ()
prices)
,(Mode RawOpts
printmode , CliOpts -> Journal -> IO ()
print')
,(Mode RawOpts
registermode , CliOpts -> Journal -> IO ()
register)
,(Mode RawOpts
rewritemode , CliOpts -> Journal -> IO ()
rewrite)
,(Mode RawOpts
roimode , CliOpts -> Journal -> IO ()
roi)
,(Mode RawOpts
runmode , CliOpts -> Journal -> IO ()
runOrReplStub)
,(Mode RawOpts
replmode , CliOpts -> Journal -> IO ()
runOrReplStub)
,(Mode RawOpts
setupmode , CliOpts -> Journal -> IO ()
setup)
,(Mode RawOpts
statsmode , CliOpts -> Journal -> IO ()
stats)
,(Mode RawOpts
tagsmode , CliOpts -> Journal -> IO ()
tags)
,(Mode RawOpts
transactionsmode , CliOpts -> Journal -> IO ()
transactions)
]
mainmode :: [String] -> Mode RawOpts
mainmode [String]
addons = Mode RawOpts
defMode {
modeNames = [progname ++ " [COMMAND]"]
,modeArgs = ([], Just $ argsFlag "[ARGS]")
,modeHelp = unlines ["hledger's main command line interface. Run with no ARGS to see a quick reference, or `hledger help commands` to list commands."]
,modeGroupModes = Group {
groupUnnamed = [
]
,groupNamed = [
]
,groupHidden = map fst builtinCommands ++ map addonCommandMode addons
}
,modeGroupFlags = Group {
groupNamed = cligeneralflagsgroups1
,groupUnnamed = confflags
,groupHidden = hiddenflagsformainmode
}
,modeHelpSuffix = []
}
help :: CliOpts -> Journal -> IO ()
help :: CliOpts -> Journal -> IO ()
help CliOpts
opts Journal
_
| String -> RawOpts -> Bool
boolopt String
"help-l" (CliOpts -> RawOpts
rawopts_ CliOpts
opts) = CliOpts -> Maybe String -> IO ()
manual CliOpts
opts (Maybe String -> IO ()) -> Maybe String -> IO ()
forall a b. (a -> b) -> a -> b
$ [String] -> Maybe String
forall a. [a] -> Maybe a
headMay ([String] -> Maybe String) -> [String] -> Maybe String
forall a b. (a -> b) -> a -> b
$ case [String]
args of
String
"manual":[String]
rest -> [String]
rest
[String]
_ -> [String]
args
| Bool
otherwise = case [String]
args of
[] -> IO ()
showQuickref
String
"quickref":[String]
_ -> IO ()
showQuickref
String
"commands":[String]
_ -> CliOpts -> Journal -> IO ()
commands CliOpts
opts Journal
nulljournal
String
"usage":[String]
rest ->
let usagemode :: Mode RawOpts
usagemode = case [String]
rest of
String
c:[String]
_ | Just (Mode RawOpts
m,CliOpts -> Journal -> IO ()
_) <- String -> Maybe (Mode RawOpts, CliOpts -> Journal -> IO ())
findBuiltinCommand String
c -> Mode RawOpts
m
[String]
_ -> [String] -> Mode RawOpts
mainmode []
in String -> IO ()
runPager (String -> IO ()) -> String -> IO ()
forall a b. (a -> b) -> a -> b
$ Mode RawOpts -> String
forall a. Mode a -> String
showModeUsage Mode RawOpts
usagemode String -> String -> String
forall a. [a] -> [a] -> [a]
++ String
"\n"
String
"manual":[String]
rest -> CliOpts -> Maybe String -> IO ()
manual CliOpts
opts ([String] -> Maybe String
forall a. [a] -> Maybe a
headMay [String]
rest)
String
"examples":[String]
rest -> (String -> IO ()) -> [String] -> IO ()
forall (t :: * -> *) (m :: * -> *) a b.
(Foldable t, Monad m) =>
(a -> m b) -> t a -> m ()
mapM_ String -> IO ()
runTldrForPage ([String] -> IO ()) -> [String] -> IO ()
forall a b. (a -> b) -> a -> b
$
if [String] -> Bool
forall a. [a] -> Bool
forall (t :: * -> *) a. Foldable t => t a -> Bool
null [String]
rest then [String
"hledger"] else (String -> String) -> [String] -> [String]
forall a b. (a -> b) -> [a] -> [b]
map (String
"hledger-"String -> String -> String
forall a. Semigroup a => a -> a -> a
<>) [String]
rest
String
"home":[String]
_ -> IO ExitCode -> IO ()
forall (f :: * -> *) a. Functor f => f a -> f ()
void (IO ExitCode -> IO ()) -> IO ExitCode -> IO ()
forall a b. (a -> b) -> a -> b
$ String -> IO ExitCode
openBrowserOn String
"https://hledger.org"
String
"install":[String]
_ -> IO ExitCode -> IO ()
forall (f :: * -> *) a. Functor f => f a -> f ()
void (IO ExitCode -> IO ()) -> IO ExitCode -> IO ()
forall a b. (a -> b) -> a -> b
$ String -> IO ExitCode
openBrowserOn String
"https://hledger.org/install.html"
String
"relnotes":[String]
_ -> IO ExitCode -> IO ()
forall (f :: * -> *) a. Functor f => f a -> f ()
void (IO ExitCode -> IO ()) -> IO ExitCode -> IO ()
forall a b. (a -> b) -> a -> b
$ String -> IO ExitCode
openBrowserOn String
"https://hledger.org/relnotes.html"
String
"docs":[String]
_ -> IO ExitCode -> IO ()
forall (f :: * -> *) a. Functor f => f a -> f ()
void (IO ExitCode -> IO ()) -> IO ExitCode -> IO ()
forall a b. (a -> b) -> a -> b
$ String -> IO ExitCode
openBrowserOn String
"https://hledger.org/doc.html"
String
"support":[String]
_ -> IO ExitCode -> IO ()
forall (f :: * -> *) a. Functor f => f a -> f ()
void (IO ExitCode -> IO ()) -> IO ExitCode -> IO ()
forall a b. (a -> b) -> a -> b
$ String -> IO ExitCode
openBrowserOn String
"https://hledger.org/support.html"
String
"sponsor":[String]
_ -> IO ExitCode -> IO ()
forall (f :: * -> *) a. Functor f => f a -> f ()
void (IO ExitCode -> IO ()) -> IO ExitCode -> IO ()
forall a b. (a -> b) -> a -> b
$ String -> IO ExitCode
openBrowserOn String
"https://hledger.org/sponsor.html"
[String]
_ -> CliOpts -> Maybe String -> IO ()
manual CliOpts
opts ([String] -> Maybe String
forall a. [a] -> Maybe a
headMay [String]
args)
where
args :: [String]
args = String -> RawOpts -> [String]
listofstringopt String
"args" (CliOpts -> RawOpts
rawopts_ CliOpts
opts)
_banner_slant :: [String]
_banner_slant = Int -> [String] -> [String]
forall a. Int -> [a] -> [a]
drop Int
1 [String
""
,String
" __ __ __ "
,String
" / /_ / /__ ____/ /___ ____ _____"
,String
" / __ \\/ / _ \\/ __ / __ `/ _ \\/ ___/"
,String
" / / / / / __/ /_/ / /_/ / __/ / "
,String
"/_/ /_/_/\\___/\\__,_/\\__, /\\___/_/ "
,String
" /____/ "
]
_banner_smslant :: [String]
_banner_smslant = Int -> [String] -> [String]
forall a. Int -> [a] -> [a]
drop Int
1 [String
""
,String
" __ __ __ "
,String
" / / / /__ ___/ /__ ____ ____"
,String
" / _ \\/ / -_) _ / _ `/ -_) __/"
,String
"/_//_/_/\\__/\\_,_/\\_, /\\__/_/ "
,String
" /___/ "
]
_banner_speed :: [String]
_banner_speed = Int -> [String] -> [String]
forall a. Int -> [a] -> [a]
drop Int
1 [String
""
,String
"______ ______ _________ "
,String
"___ /____ /__________ /______ _____________"
,String
"__ __ \\_ /_ _ \\ __ /__ __ `/ _ \\_ ___/"
,String
"_ / / / / / __/ /_/ / _ /_/ // __/ / "
,String
"/_/ /_//_/ \\___/\\__,_/ _\\__, / \\___//_/ "
,String
" /____/ "
]
data CommandCategories = CommandCategories {
CommandCategories -> Bool
showbuiltins_ :: Bool
,CommandCategories -> Bool
showaddons_ :: Bool
,CommandCategories -> Bool
showaliases_ :: Bool
}
commandCategories :: RawOpts -> CommandCategories
commandCategories :: RawOpts -> CommandCategories
commandCategories RawOpts
rawopts
| Bool -> Bool
not (Bool
builtins Bool -> Bool -> Bool
|| Bool
addons Bool -> Bool -> Bool
|| Bool
cmdaliases) = Bool -> Bool -> Bool -> CommandCategories
CommandCategories Bool
True Bool
True Bool
True
| Bool
otherwise = Bool -> Bool -> Bool -> CommandCategories
CommandCategories Bool
builtins Bool
addons Bool
cmdaliases
where
builtins :: Bool
builtins = String -> RawOpts -> Bool
boolopt String
"builtins" RawOpts
rawopts
addons :: Bool
addons = String -> RawOpts -> Bool
boolopt String
"addons" RawOpts
rawopts
cmdaliases :: Bool
cmdaliases = String -> RawOpts -> Bool
boolopt String
"aliases" RawOpts
rawopts
commandsListSections :: [(String, [String])]
commandsListSections :: [(String, [String])]
commandsListSections = [
(String
"USER INTERFACES",
[String
" help (h) show documentation and examples"
,String
" repl run multiple commands from an interactive prompt"
,String
" run run multiple commands from a file or command line"
,String
"+ui run a terminal UI"
,String
"+web run a web UI"
])
,(String
"ENTERING DATA",
[String
" add add transactions using terminal prompts"
,String
" get fetch transaction and price data for import"
,String
"+iadd add transactions using a TUI (hledger-iadd)"
,String
" import add new transactions from other files, eg CSV files"
])
,(String
"REPORTS",
[String
" accounts (acc) account names"
,String
" activity transaction or posting counts, as a bar chart"
,String
" aregister (areg) an account's transactions & running balance"
,String
" balance (bal) balance changes, end balances, gains, budgets & more"
,String
" balancesheet (bs) assets, liabilities and net worth"
,String
" balancesheetequity (bse) assets, liabilities and equity"
,String
"+bar simple balance/change bar charts for hledger 1"
,String
" cashflow (cf) changes in liquid assets"
,String
" codes transaction codes"
,String
" commodities (comm) commodity/currency symbols"
,String
" descriptions (desc) transaction descriptions"
,String
" files input files used"
,String
" holdings investment holdings and performance"
,String
" incomestatement (is) revenues and expenses"
,String
" notes the note part of transaction descriptions"
,String
" payees the payee part of transaction descriptions"
,String
"+plot advanced matplotlib charts, as gui/svg/png/pdf"
,String
" prices market prices"
,String
" print transaction entries, or a whole exported journal"
,String
" register (reg) matched postings & running total, across accounts"
,String
" roi return on investments"
,String
" stats journal statistics"
,String
" tags tag names"
,String
" transactions (tx) transactions, in compact one-line format"
])
,(String
"MAINTENANCE",
[String
" setup check your hledger installation"
,String
" init create starter journal files"
,String
" check check that your journal is valid"
,String
"+check-tagfiles check that files referenced in tag values exist"
,String
" diff compare an account's transactions in two journals"
,String
"+edit edit matched transactions with $EDITOR"
,String
"+git simple version control of the journal with git"
,String
"+jj simple version control of the journal with jj"
])
,(String
"GENERATING DATA",
[String
" close generate transactions to zero/restore/assert balances"
,String
"+interest generate transactions transferring accrued interest"
,String
" rewrite generate postings by rule, like print --auto"
])
]
commandsListCommandNames :: Bool -> [String]
commandsListCommandNames :: Bool -> [String]
commandsListCommandNames Bool
addonsonly =
[ String
cmdname
| (String
_, [String]
cmds) <- [(String, [String])]
commandsListSections
, Char
prefixchar:String
cmd <- [String]
cmds
, Char
prefixchar Char -> String -> Bool
forall a. Eq a => a -> [a] -> Bool
forall (t :: * -> *) a. (Foldable t, Eq a) => a -> t a -> Bool
`elem` Char
'+'Char -> String -> String
forall a. a -> [a] -> [a]
:[Char
' '|Bool -> Bool
not Bool
addonsonly]
, String
cmdname:[String]
_ <- [String -> [String]
words String
cmd]
]
commandsList :: CommandCategories -> [String] -> [(CommandAlias,CommandLine)] -> [String]
commandsList :: CommandCategories -> [String] -> [(String, String)] -> [String]
commandsList CommandCategories
cats [String]
installedaddons [(String, String)]
cmdaliases =
String -> String
titleLine String
"HLEDGER COMMANDS"
String -> [String] -> [String]
forall a. a -> [a] -> [a]
: ((String, [String]) -> [String])
-> [(String, [String])] -> [String]
forall (t :: * -> *) a b. Foldable t => (a -> [b]) -> t a -> [b]
concatMap (String, [String]) -> [String]
forall {t :: * -> *}. Foldable t => (String, t String) -> [String]
rendersection [(String, [String])]
commandsListSections
[String] -> [String] -> [String]
forall a. [a] -> [a] -> [a]
++ [String]
otheraddonssection
[String] -> [String] -> [String]
forall a. [a] -> [a] -> [a]
++ [String]
aliasessection
where
rendersection :: (String, t String) -> [String]
rendersection (String
heading, t String
cmds) = case (String -> [String]) -> t String -> [String]
forall (t :: * -> *) a b. Foldable t => (a -> [b]) -> t a -> [b]
concatMap String -> [String]
selectcommand t String
cmds of
[] -> []
[String]
cmds' -> String -> String
bold' String
heading String -> [String] -> [String]
forall a. a -> [a] -> [a]
: [String]
cmds'
selectcommand :: String -> [String]
selectcommand (Char
prefixchar:String
cmd) = case Char
prefixchar of
Char
' ' | CommandCategories -> Bool
showbuiltins_ CommandCategories
cats -> [Char
' 'Char -> String -> String
forall a. a -> [a] -> [a]
:String
cmd]
Char
'+' | CommandCategories -> Bool
showaddons_ CommandCategories
cats, String -> Bool
isinstalled String
cmdname -> [Char
' 'Char -> String -> String
forall a. a -> [a] -> [a]
:String
cmd]
Char
_ -> []
where cmdname :: String
cmdname = (Char -> Bool) -> String -> String
forall a. (a -> Bool) -> [a] -> [a]
takeWhile (Bool -> Bool
not(Bool -> Bool) -> (Char -> Bool) -> Char -> Bool
forall b c a. (b -> c) -> (a -> b) -> a -> c
.Char -> Bool
isSpace) String
cmd
selectcommand [] = []
isinstalled :: String -> Bool
isinstalled String
cmdname
| String
cmdname String -> [String] -> Bool
forall a. Eq a => a -> [a] -> Bool
forall (t :: * -> *) a. (Foldable t, Eq a) => a -> t a -> Bool
`elem` [String]
installedaddons = Bool
True
| Bool
otherwise = (Bool -> String) -> Bool -> Bool
forall a. (a -> String) -> a -> a
dbg9With (String -> Bool -> String
forall a b. a -> b -> a
const (String -> Bool -> String) -> String -> Bool -> String
forall a b. (a -> b) -> a -> b
$ String
"hiding uninstalled addon: " String -> String -> String
forall a. Semigroup a => a -> a -> a
<> String
cmdname) Bool
False
otheraddonssection :: [String]
otheraddonssection
| Bool -> Bool
not (Bool -> Bool) -> Bool -> Bool
forall a b. (a -> b) -> a -> b
$ CommandCategories -> Bool
showaddons_ CommandCategories
cats = []
| Bool
otherwise = String -> String
bold' String
"OTHER ADDONS" String -> [String] -> [String]
forall a. a -> [a] -> [a]
: (String -> String) -> [String] -> [String]
forall a b. (a -> b) -> [a] -> [b]
map (Char
' 'Char -> String -> String
forall a. a -> [a] -> [a]
:) (String -> [String]
lines (String -> [String]) -> String -> [String]
forall a b. (a -> b) -> a -> b
$ Int -> [String] -> String
multicol Int
79 [String]
otheraddons)
where otheraddons :: [String]
otheraddons = [String]
installedaddons [String] -> [String] -> [String]
forall a. Eq a => [a] -> [a] -> [a]
\\ [String]
knownAddonCommandNames
aliasessection :: [String]
aliasessection
| Bool -> Bool
not (CommandCategories -> Bool
showaliases_ CommandCategories
cats) Bool -> Bool -> Bool
|| [(String, String)] -> Bool
forall a. [a] -> Bool
forall (t :: * -> *) a. Foldable t => t a -> Bool
null [(String, String)]
cmdaliases = []
| Bool
otherwise = String -> String
bold' String
"ALIASES" String -> [String] -> [String]
forall a. a -> [a] -> [a]
:
[String
" " String -> String -> String
forall a. Semigroup a => a -> a -> a
<> Int -> String -> String
padright Int
aliasw String
a String -> String -> String
forall a. Semigroup a => a -> a -> a
<> String
" = " String -> String -> String
forall a. Semigroup a => a -> a -> a
<> String
def | (String
a,String
def) <- [(String, String)]
cmdaliases]
where
aliasw :: Int
aliasw = Int -> [Int] -> Int
forall a. Ord a => a -> [a] -> a
maximumDef Int
0 ([Int] -> Int) -> [Int] -> Int
forall a b. (a -> b) -> a -> b
$ ((String, String) -> Int) -> [(String, String)] -> [Int]
forall a b. (a -> b) -> [a] -> [b]
map (String -> Int
forall a. [a] -> Int
forall (t :: * -> *) a. Foldable t => t a -> Int
length(String -> Int)
-> ((String, String) -> String) -> (String, String) -> Int
forall b c a. (b -> c) -> (a -> b) -> a -> c
.(String, String) -> String
forall a b. (a, b) -> a
fst) [(String, String)]
cmdaliases
padright :: Int -> String -> String
padright Int
w String
s = String
s String -> String -> String
forall a. Semigroup a => a -> a -> a
<> Int -> Char -> String
forall a. Int -> a -> [a]
replicate (Int
w Int -> Int -> Int
forall a. Num a => a -> a -> a
- String -> Int
forall a. [a] -> Int
forall (t :: * -> *) a. Foldable t => t a -> Int
length String
s) Char
' '
commands :: CliOpts -> Journal -> IO ()
commands :: CliOpts -> Journal -> IO ()
commands CliOpts
opts Journal
_ = do
installedaddons <- if CommandCategories -> Bool
showaddons_ CommandCategories
cats then IO [String]
addonCommandNames else [String] -> IO [String]
forall a. a -> IO a
forall (m :: * -> *) a. Monad m => a -> m a
return []
cmdaliases <-
if not $ showaliases_ cats then return [] else do
(conf,_) <- getConf' $ rawopts_ opts
return $ reverse $ nubOrdOn fst $ reverse $ confAliases conf
printCommandsList cats installedaddons cmdaliases
where
cats :: CommandCategories
cats = RawOpts -> CommandCategories
commandCategories (RawOpts -> CommandCategories) -> RawOpts -> CommandCategories
forall a b. (a -> b) -> a -> b
$ CliOpts -> RawOpts
rawopts_ CliOpts
opts
printCommandsList :: CommandCategories -> [String] -> [(CommandAlias,CommandLine)] -> IO ()
printCommandsList :: CommandCategories -> [String] -> [(String, String)] -> IO ()
printCommandsList CommandCategories
cats [String]
installedaddons [(String, String)]
cmdaliases =
Int -> IO () -> IO ()
forall a b. a -> b -> b
seq ([String] -> Int
forall a. [a] -> Int
forall (t :: * -> *) a. Foldable t => t a -> Int
length ([String] -> Int) -> [String] -> Int
forall a b. (a -> b) -> a -> b
$ String -> [String] -> [String]
forall a. Show a => String -> a -> a
dbg8 String
"uninstalledknownaddons" ([String] -> [String]) -> [String] -> [String]
forall a b. (a -> b) -> a -> b
$ [String]
knownaddons [String] -> [String] -> [String]
forall a. Eq a => [a] -> [a] -> [a]
\\ [String]
installedaddons) (IO () -> IO ()) -> IO () -> IO ()
forall a b. (a -> b) -> a -> b
$
Int -> IO () -> IO ()
forall a b. a -> b -> b
seq ([String] -> Int
forall a. [a] -> Int
forall (t :: * -> *) a. Foldable t => t a -> Int
length ([String] -> Int) -> [String] -> Int
forall a b. (a -> b) -> a -> b
$ String -> [String] -> [String]
forall a. Show a => String -> a -> a
dbg8 String
"installedknownaddons" ([String] -> [String]) -> [String] -> [String]
forall a b. (a -> b) -> a -> b
$ [String]
knownaddons [String] -> [String] -> [String]
forall a. Eq a => [a] -> [a] -> [a]
`intersect` [String]
installedaddons) (IO () -> IO ()) -> IO () -> IO ()
forall a b. (a -> b) -> a -> b
$
Int -> IO () -> IO ()
forall a b. a -> b -> b
seq ([String] -> Int
forall a. [a] -> Int
forall (t :: * -> *) a. Foldable t => t a -> Int
length ([String] -> Int) -> [String] -> Int
forall a b. (a -> b) -> a -> b
$ String -> [String] -> [String]
forall a. Show a => String -> a -> a
dbg8 String
"installedunknownaddons" ([String] -> [String]) -> [String] -> [String]
forall a b. (a -> b) -> a -> b
$ [String]
installedaddons [String] -> [String] -> [String]
forall a. Eq a => [a] -> [a] -> [a]
\\ [String]
knownaddons) (IO () -> IO ()) -> IO () -> IO ()
forall a b. (a -> b) -> a -> b
$
String -> IO ()
runPager (String -> IO ()) -> String -> IO ()
forall a b. (a -> b) -> a -> b
$ [String] -> String
unlines ([String] -> String) -> [String] -> String
forall a b. (a -> b) -> a -> b
$ CommandCategories -> [String] -> [(String, String)] -> [String]
commandsList CommandCategories
cats [String]
installedaddons [(String, String)]
cmdaliases
where
knownaddons :: [String]
knownaddons = [String]
knownAddonCommandNames
knownCommands :: [String]
knownCommands :: [String]
knownCommands = [String] -> [String]
forall a. Ord a => [a] -> [a]
nubSort ([String] -> [String]) -> [String] -> [String]
forall a b. (a -> b) -> a -> b
$ Bool -> [String]
commandsListCommandNames Bool
False
builtinCommandNames :: [String]
builtinCommandNames :: [String]
builtinCommandNames = ((Mode RawOpts, CliOpts -> Journal -> IO ()) -> [String])
-> [(Mode RawOpts, CliOpts -> Journal -> IO ())] -> [String]
forall (t :: * -> *) a b. Foldable t => (a -> [b]) -> t a -> [b]
concatMap (Mode RawOpts -> [String]
forall a. Mode a -> [String]
modeNames (Mode RawOpts -> [String])
-> ((Mode RawOpts, CliOpts -> Journal -> IO ()) -> Mode RawOpts)
-> (Mode RawOpts, CliOpts -> Journal -> IO ())
-> [String]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (Mode RawOpts, CliOpts -> Journal -> IO ()) -> Mode RawOpts
forall a b. (a, b) -> a
fst) [(Mode RawOpts, CliOpts -> Journal -> IO ())]
builtinCommands
findBuiltinCommand :: String -> Maybe (Mode RawOpts, CliOpts -> Journal -> IO ())
findBuiltinCommand :: String -> Maybe (Mode RawOpts, CliOpts -> Journal -> IO ())
findBuiltinCommand String
cmdname = ((Mode RawOpts, CliOpts -> Journal -> IO ()) -> Bool)
-> [(Mode RawOpts, CliOpts -> Journal -> IO ())]
-> Maybe (Mode RawOpts, CliOpts -> Journal -> IO ())
forall (t :: * -> *) a. Foldable t => (a -> Bool) -> t a -> Maybe a
find (String -> [String] -> Bool
forall a. Eq a => a -> [a] -> Bool
forall (t :: * -> *) a. (Foldable t, Eq a) => a -> t a -> Bool
elem String
cmdname ([String] -> Bool)
-> ((Mode RawOpts, CliOpts -> Journal -> IO ()) -> [String])
-> (Mode RawOpts, CliOpts -> Journal -> IO ())
-> Bool
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Mode RawOpts -> [String]
forall a. Mode a -> [String]
modeNames (Mode RawOpts -> [String])
-> ((Mode RawOpts, CliOpts -> Journal -> IO ()) -> Mode RawOpts)
-> (Mode RawOpts, CliOpts -> Journal -> IO ())
-> [String]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (Mode RawOpts, CliOpts -> Journal -> IO ()) -> Mode RawOpts
forall a b. (a, b) -> a
fst) [(Mode RawOpts, CliOpts -> Journal -> IO ())]
builtinCommands
knownAddonCommandNames :: [String]
knownAddonCommandNames :: [String]
knownAddonCommandNames = [String] -> [String]
forall a. Ord a => [a] -> [a]
nubSort ([String] -> [String]) -> [String] -> [String]
forall a b. (a -> b) -> a -> b
$ Bool -> [String]
commandsListCommandNames Bool
True
addonCommandNames :: IO [String]
addonCommandNames :: IO [String]
addonCommandNames = IO [String]
installedAddonCommandNames IO [String] -> ([String] -> [String]) -> IO [String]
forall (f :: * -> *) a b. Functor f => f a -> (a -> b) -> f b
<&> (String -> Bool) -> [String] -> [String]
forall a. (a -> Bool) -> [a] -> [a]
filter (Bool -> Bool
not (Bool -> Bool) -> (String -> Bool) -> String -> Bool
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (String -> [String] -> Bool
forall a. Eq a => a -> [a] -> Bool
forall (t :: * -> *) a. (Foldable t, Eq a) => a -> t a -> Bool
`elem` [String]
builtinCommandNames) (String -> Bool) -> (String -> String) -> String -> Bool
forall b c a. (b -> c) -> (a -> b) -> a -> c
. String -> String
dropExtension)
installedAddonCommandNames :: IO [String]
installedAddonCommandNames :: IO [String]
installedAddonCommandNames = do
as1 <- IO [String]
hledgerExecutablesInPath
let as2 = (String -> String) -> [String] -> [String]
forall a b. (a -> b) -> [a] -> [b]
map String -> String
forall a. [a] -> [a]
stripPrognamePrefix [String]
as1
let as3 = (String -> String) -> [String] -> [[String]]
forall b a. Ord b => (a -> b) -> [a] -> [[a]]
groupSortOn String -> String
takeBaseName [String]
as2
let as4 = ([String] -> [String]) -> [[String]] -> [String]
forall (t :: * -> *) a b. Foldable t => (a -> [b]) -> t a -> [b]
concatMap [String] -> [String]
dropRedundantSourceVersion [[String]]
as3
return as4
stripPrognamePrefix :: [a] -> [a]
stripPrognamePrefix = Int -> [a] -> [a]
forall a. Int -> [a] -> [a]
drop (String -> Int
forall a. [a] -> Int
forall (t :: * -> *) a. Foldable t => t a -> Int
length String
progname Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
1)
dropRedundantSourceVersion :: [String] -> [String]
dropRedundantSourceVersion [String
f,String
g]
| (Char -> Char) -> String -> String
forall a b. (a -> b) -> [a] -> [b]
map Char -> Char
toLower (String -> String
takeExtension String
f) String -> [String] -> Bool
forall a. Eq a => a -> [a] -> Bool
forall (t :: * -> *) a. (Foldable t, Eq a) => a -> t a -> Bool
`elem` [String]
compiledExts = [String
f]
| (Char -> Char) -> String -> String
forall a b. (a -> b) -> [a] -> [b]
map Char -> Char
toLower (String -> String
takeExtension String
g) String -> [String] -> Bool
forall a. Eq a => a -> [a] -> Bool
forall (t :: * -> *) a. (Foldable t, Eq a) => a -> t a -> Bool
`elem` [String]
compiledExts = [String
g]
dropRedundantSourceVersion [String]
fs = [String]
fs
compiledExts :: [String]
compiledExts = [String
"",String
".com",String
".exe"]
hledgerExecutablesInPath :: IO [String]
hledgerExecutablesInPath :: IO [String]
hledgerExecutablesInPath = (String -> Bool) -> [String] -> [String]
forall a. (a -> Bool) -> [a] -> [a]
filter String -> Bool
isHledgerExeName ([String] -> [String]) -> IO [String] -> IO [String]
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> IO [String]
likelyExecutablesInPath
isHledgerExeName :: String -> Bool
isHledgerExeName :: String -> Bool
isHledgerExeName = Either (ParseErrorBundle Text HledgerParseErrorData) () -> Bool
forall a b. Either a b -> Bool
isRight (Either (ParseErrorBundle Text HledgerParseErrorData) () -> Bool)
-> (String
-> Either (ParseErrorBundle Text HledgerParseErrorData) ())
-> String
-> Bool
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Parsec HledgerParseErrorData Text ()
-> Text -> Either (ParseErrorBundle Text HledgerParseErrorData) ()
forall e a.
Parsec e Text a -> Text -> Either (ParseErrorBundle Text e) a
parsewith Parsec HledgerParseErrorData Text ()
forall {m :: * -> *}. ParsecT HledgerParseErrorData Text m ()
hledgerexenamep (Text -> Either (ParseErrorBundle Text HledgerParseErrorData) ())
-> (String -> Text)
-> String
-> Either (ParseErrorBundle Text HledgerParseErrorData) ()
forall b c a. (b -> c) -> (a -> b) -> a -> c
. String -> Text
T.pack
where
hledgerexenamep :: ParsecT HledgerParseErrorData Text m ()
hledgerexenamep = do
_ <- Tokens Text -> ParsecT HledgerParseErrorData Text m (Tokens Text)
forall e s (m :: * -> *).
MonadParsec e s m =>
Tokens s -> m (Tokens s)
string (Tokens Text -> ParsecT HledgerParseErrorData Text m (Tokens Text))
-> Tokens Text
-> ParsecT HledgerParseErrorData Text m (Tokens Text)
forall a b. (a -> b) -> a -> b
$ String -> Text
T.pack String
progname
_ <- char '-'
_ <- some $ noneOf ['.']
optional (string "." >> choice' (map (string . T.pack) addonExtensions))
eof
addonExtensions :: [String]
addonExtensions :: [String]
addonExtensions =
[String
"bat"
,String
"com"
,String
"exe"
,String
"hs"
,String
"js"
,String
"lhs"
,String
"lua"
,String
"php"
,String
"pl"
,String
"py"
,String
"rb"
,String
"rkt"
,String
"sh"
,String
"osh"
,String
"ysh"
]
runUnitTests :: String -> IO ()
runUnitTests :: String -> IO ()
runUnitTests String
pat =
[String] -> IO () -> IO ()
forall a. [String] -> IO a -> IO a
withArgs (String
"--num-threads=1" String -> [String] -> [String]
forall a. a -> [a] -> [a]
: [String
"--pattern=" String -> String -> String
forall a. Semigroup a => a -> a -> a
<> String
pat | Bool -> Bool
not (Bool -> Bool) -> Bool -> Bool
forall a b. (a -> b) -> a -> b
$ String -> Bool
forall a. [a] -> Bool
forall (t :: * -> *) a. Foldable t => t a -> Bool
null String
pat]) (IO () -> IO ()) -> IO () -> IO ()
forall a b. (a -> b) -> a -> b
$
TestTree -> IO ()
Test.Tasty.defaultMain (TestTree -> IO ()) -> TestTree -> IO ()
forall a b. (a -> b) -> a -> b
$ String -> [TestTree] -> TestTree
testGroup String
"hledger" [
TestTree
tests_Hledger
,TestTree
tests_Hledger_Cli
]
tests_Hledger_Cli :: TestTree
tests_Hledger_Cli = String -> [TestTree] -> TestTree
testGroup String
"Hledger.Cli" [
TestTree
tests_Cli_Utils
,TestTree
tests_Commands
]
tests_Commands :: TestTree
tests_Commands = String -> [TestTree] -> TestTree
testGroup String
"Commands" [
TestTree
tests_Balance
,TestTree
tests_Register
,TestTree
tests_Aregister
,TestTree
tests_Init
,String -> [TestTree] -> TestTree
testGroup String
"apply account directive" [
String -> IO () -> TestTree
testCase String
"works" (IO () -> TestTree) -> IO () -> TestTree
forall a b. (a -> b) -> a -> b
$ do
let
ignoresourcepos :: Journal -> Journal
ignoresourcepos Journal
j = Journal
j{jtxns=map (\Transaction
t -> Transaction
t{tsourcepos=nullsourcepospair}) (jtxns j), jitems=[]}
sameParse :: Text -> Text -> IO ()
sameParse Text
str1 Text
str2 = do
j1 <- Journal -> Journal
ignoresourcepos (Journal -> Journal) -> IO Journal -> IO Journal
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Text -> IO Journal
readJournal'' Text
str1
j2 <- ignoresourcepos <$> readJournal'' str2
j1 @?= j2{jlastreadtime=jlastreadtime j1, jfiles=jfiles j1}
Text -> Text -> IO ()
sameParse
(Text
"2008/12/07 One\n alpha $-1\n beta $1\n" Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<>
Text
"apply account outer\n2008/12/07 Two\n aigh $-2\n bee $2\n" Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<>
Text
"apply account inner\n2008/12/07 Three\n gamma $-3\n delta $3\n" Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<>
Text
"end apply account\n2008/12/07 Four\n why $-4\n zed $4\n" Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<>
Text
"end apply account\n2008/12/07 Five\n foo $-5\n bar $5\n"
)
(Text
"2008/12/07 One\n alpha $-1\n beta $1\n" Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<>
Text
"2008/12/07 Two\n outer:aigh $-2\n outer:bee $2\n" Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<>
Text
"2008/12/07 Three\n outer:inner:gamma $-3\n outer:inner:delta $3\n" Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<>
Text
"2008/12/07 Four\n outer:why $-4\n outer:zed $4\n" Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<>
Text
"2008/12/07 Five\n foo $-5\n bar $5\n"
)
,String -> IO () -> TestTree
testCase String
"preserves \"virtual\" posting type" (IO () -> TestTree) -> IO () -> TestTree
forall a b. (a -> b) -> a -> b
$ do
j <- Text -> IO Journal
readJournal'' Text
"apply account test\n2008/12/07 One\n (from) $-1\n (to) $1\n"
let p = [Posting] -> Posting
forall a. HasCallStack => [a] -> a
headErr ([Posting] -> Posting) -> [Posting] -> Posting
forall a b. (a -> b) -> a -> b
$ Transaction -> [Posting]
tpostings (Transaction -> [Posting]) -> Transaction -> [Posting]
forall a b. (a -> b) -> a -> b
$ [Transaction] -> Transaction
forall a. HasCallStack => [a] -> a
headErr ([Transaction] -> Transaction) -> [Transaction] -> Transaction
forall a b. (a -> b) -> a -> b
$ Journal -> [Transaction]
jtxns Journal
j
paccount p @?= "test:from"
preal p @?= VirtualPosting
]
,String -> IO () -> TestTree
testCase String
"alias directive" (IO () -> TestTree) -> IO () -> TestTree
forall a b. (a -> b) -> a -> b
$ do
j <- Text -> IO Journal
readJournal'' Text
"!alias expenses = equity:draw:personal\n1/1\n (expenses:food) 1\n"
let p = [Posting] -> Posting
forall a. HasCallStack => [a] -> a
headErr ([Posting] -> Posting) -> [Posting] -> Posting
forall a b. (a -> b) -> a -> b
$ Transaction -> [Posting]
tpostings (Transaction -> [Posting]) -> Transaction -> [Posting]
forall a b. (a -> b) -> a -> b
$ [Transaction] -> Transaction
forall a. HasCallStack => [a] -> a
headErr ([Transaction] -> Transaction) -> [Transaction] -> Transaction
forall a b. (a -> b) -> a -> b
$ Journal -> [Transaction]
jtxns Journal
j
paccount p @?= "equity:draw:personal:food"
,String -> IO () -> TestTree
testCase String
"Y default year directive" (IO () -> TestTree) -> IO () -> TestTree
forall a b. (a -> b) -> a -> b
$ do
j <- Text -> IO Journal
readJournal'' Text
defaultyear_journal_txt
tdate (headErr $ jtxns j) @?= fromGregorian 2009 1 1
,String -> IO () -> TestTree
testCase String
"ledgerAccountNames" (IO () -> TestTree) -> IO () -> TestTree
forall a b. (a -> b) -> a -> b
$
(Ledger -> [Text]
ledgerAccountNames Ledger
ledger7)
[Text] -> [Text] -> IO ()
forall a. (Eq a, Show a, HasCallStack) => a -> a -> IO ()
@?=
[Text
"assets",Text
"assets:cash",Text
"assets:checking",Text
"assets:saving",Text
"equity",Text
"equity:opening balances",
Text
"expenses",Text
"expenses:food",Text
"expenses:food:dining",Text
"expenses:phone",Text
"expenses:vacation",
Text
"liabilities",Text
"liabilities:credit cards",Text
"liabilities:credit cards:discover"]
,String -> IO () -> TestTree
testCase String
"show dollars" (IO () -> TestTree) -> IO () -> TestTree
forall a b. (a -> b) -> a -> b
$ Amount -> String
showAmount (DecimalRaw Integer -> Amount
usd DecimalRaw Integer
1) String -> String -> IO ()
forall a. (Eq a, Show a, HasCallStack) => a -> a -> IO ()
@?= String
"$1.00"
,String -> IO () -> TestTree
testCase String
"show hours" (IO () -> TestTree) -> IO () -> TestTree
forall a b. (a -> b) -> a -> b
$ Amount -> String
showAmount (DecimalRaw Integer -> Amount
hrs DecimalRaw Integer
1) String -> String -> IO ()
forall a. (Eq a, Show a, HasCallStack) => a -> a -> IO ()
@?= String
"1.00h"
]
defaultyear_journal_txt :: Text
defaultyear_journal_txt :: Text
defaultyear_journal_txt = [Text] -> Text
T.unlines
[Text
"Y2009"
,Text
""
,Text
"01/01 A"
,Text
" a $1"
,Text
" b"
]
journal7 :: Journal
journal7 :: Journal
journal7 = Journal
nulljournal {jtxns =
[
txnTieKnot Transaction {
tindex=0,
tsourcepos=nullsourcepospair,
tdate=fromGregorian 2007 01 01,
tdate2=Nothing,
tstatus=Unmarked,
tcode="*",
tdescription="opening balance",
tcomment="",
ttags=[],
tpostings=
["assets:cash" `post` usd 4.82
,"equity:opening balances" `post` usd (-4.82)
],
tprecedingcomment=""
}
,
txnTieKnot Transaction {
tindex=0,
tsourcepos=nullsourcepospair,
tdate=fromGregorian 2007 02 01,
tdate2=Nothing,
tstatus=Unmarked,
tcode="*",
tdescription="ayres suites",
tcomment="",
ttags=[],
tpostings=
["expenses:vacation" `post` usd 179.92
,"assets:checking" `post` usd (-179.92)
],
tprecedingcomment=""
}
,
txnTieKnot Transaction {
tindex=0,
tsourcepos=nullsourcepospair,
tdate=fromGregorian 2007 01 02,
tdate2=Nothing,
tstatus=Unmarked,
tcode="*",
tdescription="auto transfer to savings",
tcomment="",
ttags=[],
tpostings=
["assets:saving" `post` usd 200
,"assets:checking" `post` usd (-200)
],
tprecedingcomment=""
}
,
txnTieKnot Transaction {
tindex=0,
tsourcepos=nullsourcepospair,
tdate=fromGregorian 2007 01 03,
tdate2=Nothing,
tstatus=Unmarked,
tcode="*",
tdescription="poquito mas",
tcomment="",
ttags=[],
tpostings=
["expenses:food:dining" `post` usd 4.82
,"assets:cash" `post` usd (-4.82)
],
tprecedingcomment=""
}
,
txnTieKnot Transaction {
tindex=0,
tsourcepos=nullsourcepospair,
tdate=fromGregorian 2007 01 03,
tdate2=Nothing,
tstatus=Unmarked,
tcode="*",
tdescription="verizon",
tcomment="",
ttags=[],
tpostings=
["expenses:phone" `post` usd 95.11
,"assets:checking" `post` usd (-95.11)
],
tprecedingcomment=""
}
,
txnTieKnot Transaction {
tindex=0,
tsourcepos=nullsourcepospair,
tdate=fromGregorian 2007 01 03,
tdate2=Nothing,
tstatus=Unmarked,
tcode="*",
tdescription="discover",
tcomment="",
ttags=[],
tpostings=
["liabilities:credit cards:discover" `post` usd 80
,"assets:checking" `post` usd (-80)
],
tprecedingcomment=""
}
]
}
ledger7 :: Ledger
ledger7 :: Ledger
ledger7 = Query -> Journal -> Ledger
ledgerFromJournal Query
Any Journal
journal7