lib, cli: -U/-P/-C flags can be combined (#564)

This commit is contained in:
Simon Michael 2017-06-10 13:30:48 -07:00
parent 44c6b324ab
commit 7fc921db86
4 changed files with 28 additions and 29 deletions

View File

@ -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]

View File

@ -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

View File

@ -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

View File

@ -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