From 7168f7efc41a1ea2cc078dd0ab3fa6e95b067b84 Mon Sep 17 00:00:00 2001 From: Stephen Morgan Date: Fri, 22 Nov 2024 23:16:04 +1100 Subject: [PATCH] imp: ui: Use new DepthSpec in hledger-ui. The regular expression depths are ignored, and only the flat depths are used. --- hledger-ui/Hledger/UI/AccountsScreen.hs | 2 +- hledger-ui/Hledger/UI/UIScreens.hs | 2 +- hledger-ui/Hledger/UI/UIState.hs | 26 ++++++++++++++----------- 3 files changed, 17 insertions(+), 13 deletions(-) diff --git a/hledger-ui/Hledger/UI/AccountsScreen.hs b/hledger-ui/Hledger/UI/AccountsScreen.hs index 28361cce5..8c1454358 100644 --- a/hledger-ui/Hledger/UI/AccountsScreen.hs +++ b/hledger-ui/Hledger/UI/AccountsScreen.hs @@ -130,7 +130,7 @@ asDrawHelper UIState{aScreen=scr, aopts=uopts, ajournal=j, aMode=mode} ropts scr ,uiShowStatus copts $ statuses_ ropts ,if real_ ropts then ["real"] else [] ] - mdepth = depth_ ropts + mdepth = dsFlatDepth $ depth_ ropts curidx = case ass ^. assList . listSelectedL of Nothing -> "-" Just i -> show (i + 1) diff --git a/hledger-ui/Hledger/UI/UIScreens.hs b/hledger-ui/Hledger/UI/UIScreens.hs index 8dde1c56f..095a2b370 100644 --- a/hledger-ui/Hledger/UI/UIScreens.hs +++ b/hledger-ui/Hledger/UI/UIScreens.hs @@ -256,7 +256,7 @@ rsUpdate uopts d j rss@RSS{_rssAccount, _rssForceInclusive, _rssList=oldlist} = -- adjust the report options and report spec, carefully as usual to avoid screwups (#1523) ropts' = ropts { -- ignore any depth limit, as in postingsReport; allows register's total to match accounts screen - depth_=Nothing + depth_=mempty -- do not strip prices so we can toggle costs within the ui , show_costs_=True -- XXX aregister also has this, needed ? diff --git a/hledger-ui/Hledger/UI/UIState.hs b/hledger-ui/Hledger/UI/UIState.hs index b55950e94..ac137a569 100644 --- a/hledger-ui/Hledger/UI/UIState.hs +++ b/hledger-ui/Hledger/UI/UIState.hs @@ -281,7 +281,7 @@ resetFilter = set querystringNoUpdate [] . set realNoUpdate False . set statuses -- resetOpts ui@UIState{astartupopts} = ui{aopts=astartupopts} resetDepth :: UIState -> UIState -resetDepth = updateReportDepth (const Nothing) +resetDepth = updateReportDepth (const mempty) -- | Get the maximum account depth in the current journal. maxDepth :: UIState -> Int @@ -292,34 +292,38 @@ maxDepth UIState{ajournal=j} = getMax . foldMap (Max . accountNameLevel) $ journ decDepth :: UIState -> UIState decDepth ui = updateReportDepth dec ui where - dec (Just d) = Just $ max 0 (d-1) - dec Nothing = Just $ maxDepth ui - 1 + dec (DepthSpec (Just d) _) = DepthSpec (Just $ max 0 (d-1)) [] + dec (DepthSpec Nothing _) = DepthSpec (Just $ maxDepth ui - 1) [] -- | Increment the current depth limit. If this makes it equal to the -- the maximum account depth, remove the depth limit. incDepth :: UIState -> UIState -incDepth = updateReportDepth (fmap succ) +incDepth = updateReportDepth inc + where + inc (DepthSpec Nothing _) = DepthSpec Nothing [] + inc (DepthSpec (Just d) _) = DepthSpec (Just $ d + 1) [] -- | Set the current depth limit to the specified depth, or remove the depth limit. -- Also remove the depth limit if the specified depth is greater than the current -- maximum account depth. If the specified depth is negative, reset the depth limit -- to whatever was specified at uiartup. setDepth :: Maybe Int -> UIState -> UIState -setDepth mdepth = updateReportDepth (const mdepth) +setDepth mdepth = updateReportDepth (const $ DepthSpec mdepth []) getDepth :: UIState -> Maybe Int -getDepth = (^.depth) +getDepth = dsFlatDepth . (^.depth) -- | Update report depth by a applying a function. If asked to set a depth less -- than zero, it will leave it unchanged. -updateReportDepth :: (Maybe Int -> Maybe Int) -> UIState -> UIState +updateReportDepth :: (DepthSpec -> DepthSpec) -> UIState -> UIState updateReportDepth updateDepth ui = over reportSpec update ui where update = fromRight (error "updateReportDepth: updating depth should not result in an error") -- PARTIAL: - . updateReportSpecWith (\ropts -> ropts{depth_=updateDepth (depth_ ropts) >>= clipDepth ropts}) - clipDepth ropts d | d < 0 = depth_ ropts - | d >= maxDepth ui = Nothing - | otherwise = Just d + . updateReportSpecWith (\ropts -> ropts{depth_=clipDepth ropts $ updateDepth (depth_ ropts)}) + clipDepth _ (DepthSpec Nothing _) = mempty + clipDepth ropts ds@(DepthSpec (Just d) _) | d < 0 = depth_ ropts + | d >= maxDepth ui = mempty + | otherwise = ds -- | Open the minibuffer, setting its content to the current query with the cursor at the end. showMinibuffer :: T.Text -> Maybe String -> UIState -> UIState