From 7fc921db864c83cbdb4ffb5ef3dce0ac8fb87ade Mon Sep 17 00:00:00 2001 From: Simon Michael Date: Sat, 10 Jun 2017 13:30:48 -0700 Subject: [PATCH] lib, cli: -U/-P/-C flags can be combined (#564) --- hledger-lib/Hledger/Reports/ReportOptions.hs | 17 +++++++++------- hledger-ui/Hledger/UI/UIState.hs | 21 ++++++++------------ hledger-ui/Hledger/UI/UIUtils.hs | 11 +++++----- tests/journal/status.test | 8 +++++--- 4 files changed, 28 insertions(+), 29 deletions(-) diff --git a/hledger-lib/Hledger/Reports/ReportOptions.hs b/hledger-lib/Hledger/Reports/ReportOptions.hs index dc14ca64f..5077f3344 100644 --- a/hledger-lib/Hledger/Reports/ReportOptions.hs +++ b/hledger-lib/Hledger/Reports/ReportOptions.hs @@ -34,6 +34,7 @@ import Data.Data (Data) #if !MIN_VERSION_base(4,8,0) import Data.Functor.Compat ((<$>)) #endif +import Data.List import Data.Maybe import qualified Data.Text as T import Data.Typeable (Typeable) @@ -73,7 +74,7 @@ instance Default AccountListMode where def = ALDefault data ReportOpts = ReportOpts { period_ :: Period ,interval_ :: Interval - ,clearedstatus_ :: Maybe ClearedStatus + ,clearedstatus_ :: [ClearedStatus] -- ^ Zero, one, or two statuses to be matched ,cost_ :: Bool ,depth_ :: Maybe Int ,display_ :: Maybe DisplayExp @@ -262,16 +263,18 @@ intervalFromRawOpts = lastDef NoInterval . catMaybes . map intervalfromrawopt | n == "yearly" = Just $ Years 1 | otherwise = Nothing --- | Get the cleared status, if any, specified by the last of -C/--cleared, --- --pending, -U/--uncleared options. -clearedStatusFromRawOpts :: RawOpts -> Maybe ClearedStatus -clearedStatusFromRawOpts = lastMay . catMaybes . map clearedstatusfromrawopt +-- | Get any cleared statuses to be matched, as specified by -C/--cleared, +-- -P/--pending, -U/--uncleared options. -UPC is equivalent to no flags, +-- so this returns a list of 0-2 unique statuses. +clearedStatusFromRawOpts :: RawOpts -> [ClearedStatus] +clearedStatusFromRawOpts = simplify . nub . sort . catMaybes . map clearedstatusfromrawopt where clearedstatusfromrawopt (n,_) | n == "cleared" = Just Cleared | n == "pending" = Just Pending | n == "uncleared" = Just Uncleared | otherwise = Nothing + simplify l = if length l == 3 then [] else l -- TODO: (maxBound :: ClearedStatus) or something type DisplayExp = String @@ -319,7 +322,7 @@ queryFromOpts d ReportOpts{..} = simplifyQuery $ And $ [flagsq, argsq] [(if date2_ then Date2 else Date) $ periodAsDateSpan period_] ++ (if real_ then [Real True] else []) ++ (if empty_ then [Empty True] else []) -- ? - ++ (maybe [] ((:[]) . Status) clearedstatus_) + ++ [Or $ map Status clearedstatus_] ++ (maybe [] ((:[]) . Depth) depth_) argsq = fst $ parseQuery d (T.pack query_) @@ -331,7 +334,7 @@ queryFromOptsOnly _d ReportOpts{..} = simplifyQuery flagsq [(if date2_ then Date2 else Date) $ periodAsDateSpan period_] ++ (if real_ then [Real True] else []) ++ (if empty_ then [Empty True] else []) -- ? - ++ (maybe [] ((:[]) . Status) clearedstatus_) + ++ [Or $ map Status clearedstatus_] ++ (maybe [] ((:[]) . Depth) depth_) tests_queryFromOpts :: [Test] diff --git a/hledger-ui/Hledger/UI/UIState.hs b/hledger-ui/Hledger/UI/UIState.hs index 456380df3..5a7b7d5f1 100644 --- a/hledger-ui/Hledger/UI/UIState.hs +++ b/hledger-ui/Hledger/UI/UIState.hs @@ -20,26 +20,21 @@ import Hledger.UI.UIOptions -- | Toggle between showing only cleared items or all items. toggleCleared :: UIState -> UIState toggleCleared ui@UIState{aopts=uopts@UIOpts{cliopts_=copts@CliOpts{reportopts_=ropts}}} = - ui{aopts=uopts{cliopts_=copts{reportopts_=toggleCleared ropts}}} - where - toggleCleared ropts@ReportOpts{clearedstatus_=Just Cleared} = ropts{clearedstatus_=Nothing} - toggleCleared ropts = ropts{clearedstatus_=Just Cleared} + ui{aopts=uopts{cliopts_=copts{reportopts_=reportOptsToggleStatus Cleared ropts}}} -- | Toggle between showing only pending items or all items. togglePending :: UIState -> UIState togglePending ui@UIState{aopts=uopts@UIOpts{cliopts_=copts@CliOpts{reportopts_=ropts}}} = - ui{aopts=uopts{cliopts_=copts{reportopts_=togglePending ropts}}} - where - togglePending ropts@ReportOpts{clearedstatus_=Just Pending} = ropts{clearedstatus_=Nothing} - togglePending ropts = ropts{clearedstatus_=Just Pending} + ui{aopts=uopts{cliopts_=copts{reportopts_=reportOptsToggleStatus Pending ropts}}} -- | Toggle between showing only uncleared items or all items. toggleUncleared :: UIState -> UIState toggleUncleared ui@UIState{aopts=uopts@UIOpts{cliopts_=copts@CliOpts{reportopts_=ropts}}} = - ui{aopts=uopts{cliopts_=copts{reportopts_=toggleUncleared ropts}}} - where - toggleUncleared ropts@ReportOpts{clearedstatus_=Just Uncleared} = ropts{clearedstatus_=Nothing} - toggleUncleared ropts = ropts{clearedstatus_=Just Uncleared} + ui{aopts=uopts{cliopts_=copts{reportopts_=reportOptsToggleStatus Uncleared ropts}}} + +reportOptsToggleStatus s ropts + | clearedstatus_ ropts == [s] = ropts{clearedstatus_=[]} + | otherwise = ropts{clearedstatus_=[s]} -- | Toggle between showing all and showing only nonempty (more precisely, nonzero) items. toggleEmpty :: UIState -> UIState @@ -125,7 +120,7 @@ resetFilter ui@UIState{aopts=uopts@UIOpts{cliopts_=copts@CliOpts{reportopts_=rop ui{aopts=uopts{cliopts_=copts{reportopts_=ropts{ accountlistmode_=ALTree ,empty_=True - ,clearedstatus_=Nothing + ,clearedstatus_=[] ,real_=False ,query_="" --,period_=PeriodAll diff --git a/hledger-ui/Hledger/UI/UIUtils.hs b/hledger-ui/Hledger/UI/UIUtils.hs index cf23502d4..36d8ad423 100644 --- a/hledger-ui/Hledger/UI/UIUtils.hs +++ b/hledger-ui/Hledger/UI/UIUtils.hs @@ -28,12 +28,11 @@ runHelp = runCommand "hledger-ui --help | less" >>= waitForProcess -- ui -uiShowClearedStatus mc = - case mc of - Just Cleared -> ["cleared"] - Just Pending -> ["pending"] - Just Uncleared -> ["uncleared"] - Nothing -> [] +uiShowClearedStatus = map showstatus + where + showstatus Cleared = "cleared" + showstatus Pending = "pending" + showstatus Uncleared = "uncleared" -- | Draw the help dialog, called when help mode is active. helpDialog :: Widget Name diff --git a/tests/journal/status.test b/tests/journal/status.test index 0109e85d1..d2295a599 100644 --- a/tests/journal/status.test +++ b/tests/journal/status.test @@ -55,11 +55,13 @@ hledger -fstatus.journal print --uncleared >>>=0 -# 6. only one of these flags (the last) takes effect -hledger -fstatus.journal register --uncleared --pending --cleared +# 6. these flags can be combined +hledger -fstatus.journal register --uncleared --pending >>> -2017/01/03 cleared (a) 1 1 +2017/01/01 uncleared (a) 1 1 (b) 1 2 +2017/01/02 pending (a) 1 3 + (b) 1 4 >>>= 0 # 7. these flags work with other commands