| 1 |
{-# LANGUAGE CPP #-} |
| 2 |
{-# LANGUAGE TemplateHaskell #-} |
| 3 |
|
| 4 |
module Hledger.UI.UIOptions where |
| 5 |
|
| 6 |
import Data.Default (def) |
| 7 |
import Data.Either (fromRight) |
| 8 |
import Data.List (intercalate) |
| 9 |
import Data.Map qualified as M |
| 10 |
import Data.Maybe (fromMaybe) |
| 11 |
import GitHash (tGitInfoCwdTry) |
| 12 |
import Lens.Micro (set) |
| 13 |
import System.Environment (getArgs) |
| 14 |
|
| 15 |
import Hledger.Cli hiding (packageversion, progname, prognameandversion) |
| 16 |
import Hledger.UI.Theme (themes, themeNames) |
| 17 |
|
| 18 |
-- cf Hledger.Cli.Version |
| 19 |
|
| 20 |
packageversion :: PackageVersionString |
| 21 |
packageversion = |
| 22 |
#ifdef VERSION |
| 23 |
VERSION |
| 24 |
#else |
| 25 |
"" |
| 26 |
#endif |
| 27 |
|
| 28 |
progname :: ProgramName |
| 29 |
progname = "hledger-ui" |
| 30 |
|
| 31 |
-- | Generate the version string for this program. |
| 32 |
-- The template haskell call is here rather than in Hledger.Cli.Version to avoid wasteful recompilation. |
| 33 |
prognameandversion :: String |
| 34 |
prognameandversion = |
| 35 |
versionStringWith |
| 36 |
$$tGitInfoCwdTry |
| 37 |
#ifdef GHCDEBUG |
| 38 |
True |
| 39 |
#else |
| 40 |
False |
| 41 |
#endif |
| 42 |
progname |
| 43 |
packageversion |
| 44 |
|
| 45 |
binaryinfo :: HledgerBinaryInfo |
| 46 |
binaryinfo = fromRight nullbinaryinfo $ parseHledgerVersion prognameandversion |
| 47 |
|
| 48 |
|
| 49 |
uiflags = [ |
| 50 |
flagNone ["watch","w"] (setboolopt "watch") "watch for data and date changes and reload automatically" |
| 51 |
,flagReq ["theme"] (\s opts -> Right $ setopt "theme" s opts) "THEME" ("use this custom display theme ("++intercalate ", " themeNames++")") |
| 52 |
,flagNone ["cash"] (setboolopt "cash") "start in: the cash accounts screen" |
| 53 |
,flagNone ["bs"] (setboolopt "bs") "start in: the balance sheet accounts screen" |
| 54 |
,flagNone ["is"] (setboolopt "is") "start in: the income statement accounts screen" |
| 55 |
,flagNone ["all"] (setboolopt "all") "start in: the all accounts screen" |
| 56 |
,flagReq ["register"] (\s opts -> Right $ setopt "register" s opts) "ACCTREGEX" "start in: the (first matched) account's register" |
| 57 |
,flagNone ["change"] (setboolopt "change") |
| 58 |
"show period balances (changes) at startup instead of historical balances" |
| 59 |
-- ,flagNone ["cumulative"] (setboolopt "cumulative") |
| 60 |
-- "show balance change accumulated across periods (in multicolumn reports)" |
| 61 |
-- ,flagNone ["historical","H"] (setboolopt "historical") |
| 62 |
-- "show historical ending balance in each period (includes postings before report start date)\n " |
| 63 |
] |
| 64 |
++ flattreeflags False |
| 65 |
-- ,flagNone ["present"] (setboolopt "present") "exclude transactions dated later than today (default)" |
| 66 |
-- ,flagReq ["drop"] (\s opts -> Right $ setopt "drop" s opts) "N" "with --flat, omit this many leading account name components" |
| 67 |
-- ,flagReq ["format"] (\s opts -> Right $ setopt "format" s opts) "FORMATSTR" "use this custom line format" |
| 68 |
-- ,flagNone ["no-elide"] (setboolopt "no-elide") "don't compress empty parent accounts on one line" |
| 69 |
|
| 70 |
--uimode :: Mode RawOpts |
| 71 |
uimode = |
| 72 |
(mode "hledger-ui" (setopt "command" "ui" def) |
| 73 |
"browse accounts, postings and entries in a full-window TUI" |
| 74 |
(argsFlag "[--cash|--bs|--is|--all|--register=ACCT] [QUERY]") []) |
| 75 |
{modeGroupFlags = Group { |
| 76 |
groupUnnamed = uiflags |
| 77 |
,groupHidden = hiddenflags |
| 78 |
++ |
| 79 |
[flagNone ["future"] (setboolopt "forecast") "old flag, use --forecast instead" |
| 80 |
,flagNone ["menu"] (setboolopt "menu") "old flag, menu screen is now the default" |
| 81 |
] |
| 82 |
,groupNamed = mkgeneralflagsgroups1 helpflags |
| 83 |
} |
| 84 |
,modeHelpSuffix=[ |
| 85 |
-- "Reads your ~/.hledger.journal file, or another specified by $LEDGER_FILE or -f, and starts the full-window TUI." |
| 86 |
] |
| 87 |
} |
| 88 |
|
| 89 |
-- hledger-ui options, used in hledger-ui and above |
| 90 |
data UIOpts = UIOpts |
| 91 |
{ uoWatch :: Bool |
| 92 |
, uoTheme :: Maybe String |
| 93 |
, uoRegister :: Maybe String |
| 94 |
, uoCliOpts :: CliOpts |
| 95 |
} deriving (Show) |
| 96 |
|
| 97 |
defuiopts = UIOpts |
| 98 |
{ uoWatch = False |
| 99 |
, uoTheme = Nothing |
| 100 |
, uoRegister = Nothing |
| 101 |
, uoCliOpts = defcliopts |
| 102 |
} |
| 103 |
|
| 104 |
-- | Process a RawOpts into a UIOpts. |
| 105 |
-- An invalid --theme name will raise an error. |
| 106 |
rawOptsToUIOpts :: RawOpts -> IO UIOpts |
| 107 |
rawOptsToUIOpts rawopts = do |
| 108 |
cliopts <- set balanceaccum accum <$> rawOptsToCliOpts rawopts |
| 109 |
return |
| 110 |
defuiopts { |
| 111 |
uoWatch = boolopt "watch" rawopts |
| 112 |
,uoTheme = checkTheme <$> maybestringopt "theme" rawopts |
| 113 |
,uoRegister = maybestringopt "register" rawopts |
| 114 |
,uoCliOpts = cliopts |
| 115 |
} |
| 116 |
where |
| 117 |
-- show historical balance by default (unlike hledger) |
| 118 |
accum = fromMaybe Historical $ balanceAccumulationOverride rawopts |
| 119 |
checkTheme t = let t' = if t == "default" then "light" else t -- "default" is a deprecated alias for "light" |
| 120 |
in if t' `M.member` themes then t' else usageError $ "invalid theme name: " ++ t |
| 121 |
|
| 122 |
-- XXX some refactoring seems due |
| 123 |
getHledgerUIOpts :: IO UIOpts |
| 124 |
--getHledgerUIOpts = processArgs uimode >>= return >>= rawOptsToUIOpts |
| 125 |
getHledgerUIOpts = do |
| 126 |
args <- getArgs >>= expandArgsAt |
| 127 |
let args' = ensureDebugFlagHasVal $ replaceNumericFlags args |
| 128 |
let cmdargopts = either usageError id $ process uimode args' |
| 129 |
rawOptsToUIOpts cmdargopts |
| 130 |
|
| 131 |
instance HasCliOpts UIOpts where |
| 132 |
cliOpts f uiopts = (\x -> uiopts{uoCliOpts=x}) <$> f (uoCliOpts uiopts) |
| 133 |
|
| 134 |
instance HasInputOpts UIOpts where |
| 135 |
inputOpts = cliOpts.inputOpts |
| 136 |
|
| 137 |
instance HasBalancingOpts UIOpts where |
| 138 |
balancingOpts = cliOpts.balancingOpts |
| 139 |
|
| 140 |
instance HasReportSpec UIOpts where |
| 141 |
reportSpec = cliOpts.reportSpec |
| 142 |
|
| 143 |
instance HasReportOptsNoUpdate UIOpts where |
| 144 |
reportOptsNoUpdate = cliOpts.reportOptsNoUpdate |
| 145 |
|
| 146 |
instance HasReportOpts UIOpts where |
| 147 |
reportOpts = cliOpts.reportOpts |