ref: ui: Pull register and theme options into UIOpts.

This commit is contained in:
Stephen Morgan 2021-08-26 22:39:14 +10:00 committed by Simon Michael
parent 54c73ff759
commit 09a2449f4f
2 changed files with 28 additions and 31 deletions

View File

@ -121,12 +121,7 @@ runBrickUi uopts@UIOpts{cliopts_=copts@CliOpts{inputopts_=_iopts,reportspec_=rsp
filteredQuery q = simplifyQuery $ And [queryFromFlags ropts, filtered q] filteredQuery q = simplifyQuery $ And [queryFromFlags ropts, filtered q]
where filtered = filterQuery (\x -> not $ queryIsDepth x || queryIsDate x) where filtered = filterQuery (\x -> not $ queryIsDepth x || queryIsDate x)
-- XXX move this stuff into Options, UIOpts (scr, prevscrs) = case uoRegister uopts' of
theme = maybe defaultTheme (fromMaybe defaultTheme . getTheme) $
maybestringopt "theme" $ rawopts_ copts
mregister = maybestringopt "register" $ rawopts_ copts
(scr, prevscrs) = case mregister of
Nothing -> (accountsScreen, []) Nothing -> (accountsScreen, [])
-- with --register, start on the register screen, and also put -- with --register, start on the register screen, and also put
-- the accounts screen on the prev screens stack so you can exit -- the accounts screen on the prev screens stack so you can exit
@ -165,7 +160,7 @@ runBrickUi uopts@UIOpts{cliopts_=copts@CliOpts{inputopts_=_iopts,reportspec_=rsp
brickapp :: App UIState AppEvent Name brickapp :: App UIState AppEvent Name
brickapp = App { brickapp = App {
appStartEvent = return appStartEvent = return
, appAttrMap = const theme , appAttrMap = const $ fromMaybe defaultTheme $ getTheme =<< uoTheme uopts'
, appChooseCursor = showFirstCursor , appChooseCursor = showFirstCursor
, appHandleEvent = \ui ev -> sHandle (aScreen ui) ui ev , appHandleEvent = \ui ev -> sHandle (aScreen ui) ui ev
, appDraw = \ui -> sDraw (aScreen ui) ui , appDraw = \ui -> sDraw (aScreen ui) ui

View File

@ -8,12 +8,13 @@ where
import Data.Default (def) import Data.Default (def)
import Data.List (intercalate) import Data.List (intercalate)
import qualified Data.Map as M
import Data.Maybe (fromMaybe) import Data.Maybe (fromMaybe)
import Lens.Micro (set) import Lens.Micro (set)
import System.Environment (getArgs) import System.Environment (getArgs)
import Hledger.Cli hiding (packageversion, progname, prognameandversion) import Hledger.Cli hiding (packageversion, progname, prognameandversion)
import Hledger.UI.Theme (themeNames) import Hledger.UI.Theme (themes, themeNames)
-- cf Hledger.Cli.Version -- cf Hledger.Cli.Version
@ -64,34 +65,35 @@ uimode = (mode "hledger-ui" (setopt "command" "ui" def)
} }
-- hledger-ui options, used in hledger-ui and above -- hledger-ui options, used in hledger-ui and above
data UIOpts = UIOpts { data UIOpts = UIOpts
watch_ :: Bool { watch_ :: Bool
,cliopts_ :: CliOpts , uoTheme :: Maybe String
, uoRegister :: Maybe String
, cliopts_ :: CliOpts
} deriving (Show) } deriving (Show)
defuiopts = UIOpts defuiopts = UIOpts
{ watch_ = False { watch_ = False
, cliopts_ = def , uoTheme = Nothing
, uoRegister = Nothing
, cliopts_ = defcliopts
} }
-- instance Default CliOpts where def = defcliopts -- | Process a RawOpts into a UIOpts.
-- This will return a usage error if provided an invalid theme.
rawOptsToUIOpts :: RawOpts -> IO UIOpts rawOptsToUIOpts :: RawOpts -> IO UIOpts
rawOptsToUIOpts rawopts = checkUIOpts <$> do rawOptsToUIOpts rawopts = do
-- show historical balance by default (unlike hledger)
let accum = fromMaybe Historical $ balanceAccumulationOverride rawopts
cliopts <- set balanceaccum accum <$> rawOptsToCliOpts rawopts cliopts <- set balanceaccum accum <$> rawOptsToCliOpts rawopts
return defuiopts { return defuiopts {
watch_ = boolopt "watch" rawopts watch_ = boolopt "watch" rawopts
,uoTheme = checkTheme <$> maybestringopt "theme" rawopts
,uoRegister = maybestringopt "register" rawopts
,cliopts_ = cliopts ,cliopts_ = cliopts
} }
where
checkUIOpts :: UIOpts -> UIOpts -- show historical balance by default (unlike hledger)
checkUIOpts opts = accum = fromMaybe Historical $ balanceAccumulationOverride rawopts
either usageError (const opts) $ do checkTheme t = if t `M.member` themes then t else usageError $ "invalid theme name: " ++ t
case maybestringopt "theme" $ rawopts_ $ cliopts_ opts of
Just t | t `notElem` themeNames -> Left $ "invalid theme name: "++t
_ -> Right ()
-- XXX some refactoring seems due -- XXX some refactoring seems due
getHledgerUIOpts :: IO UIOpts getHledgerUIOpts :: IO UIOpts