| 1 |
{- | UIState operations. -} |
| 2 |
|
| 3 |
module Hledger.UI.UIState |
| 4 |
(uiState |
| 5 |
,uiShowStatus |
| 6 |
,setFilter |
| 7 |
,setMode |
| 8 |
,setReportPeriod |
| 9 |
,showMinibuffer |
| 10 |
,closeMinibuffer |
| 11 |
,toggleCleared |
| 12 |
,toggleConversionOp |
| 13 |
,toggleIgnoreBalanceAssertions |
| 14 |
,toggleEmpty |
| 15 |
,toggleForecast |
| 16 |
,toggleHistorical |
| 17 |
,togglePending |
| 18 |
,toggleUnmarked |
| 19 |
,toggleReal |
| 20 |
,toggleTree |
| 21 |
,setTree |
| 22 |
,setList |
| 23 |
,toggleValue |
| 24 |
,reportPeriod |
| 25 |
,shrinkReportPeriod |
| 26 |
,growReportPeriod |
| 27 |
,nextReportPeriod |
| 28 |
,previousReportPeriod |
| 29 |
,resetReportPeriod |
| 30 |
,moveReportPeriodToDate |
| 31 |
,getDepth |
| 32 |
,setDepth |
| 33 |
,decDepth |
| 34 |
,incDepth |
| 35 |
,resetDepth |
| 36 |
,popScreen |
| 37 |
,pushScreen |
| 38 |
,enableForecast |
| 39 |
,resetFilter |
| 40 |
,resetScreens |
| 41 |
,regenerateScreens |
| 42 |
) |
| 43 |
where |
| 44 |
|
| 45 |
import Brick.Widgets.Edit |
| 46 |
import Data.Bifunctor (first) |
| 47 |
import Data.Foldable (asum) |
| 48 |
import Data.Either (fromRight) |
| 49 |
import Data.List ((\\), sort) |
| 50 |
import Data.Maybe (fromMaybe) |
| 51 |
import Data.Semigroup (Max(..)) |
| 52 |
import Data.Text qualified as T |
| 53 |
import Data.Text.Zipper (gotoEOL) |
| 54 |
import Data.Time.Calendar (Day) |
| 55 |
import Lens.Micro ((^.), over, set) |
| 56 |
import Safe |
| 57 |
|
| 58 |
import Hledger |
| 59 |
import Hledger.Cli.CliOptions |
| 60 |
import Hledger.UI.UITypes |
| 61 |
import Hledger.UI.UIOptions (UIOpts(uoCliOpts)) |
| 62 |
import Hledger.UI.UIScreens (screenUpdate) |
| 63 |
import Hledger.UI.UIUtils (showScreenId, showScreenStack) |
| 64 |
|
| 65 |
-- | Make an initial UI state with the given options, journal, |
| 66 |
-- parent screen stack if any, and starting screen. |
| 67 |
uiState :: UIOpts -> Journal -> [Screen] -> Screen -> UIState |
| 68 |
uiState uopts j prevscrs scr = UIState { |
| 69 |
astartupopts = uopts |
| 70 |
,aopts = uopts |
| 71 |
,ajournal = j |
| 72 |
,aMode = Normal |
| 73 |
,aScreen = scr |
| 74 |
,aPrevScreens = prevscrs |
| 75 |
} |
| 76 |
|
| 77 |
-- | Toggle between showing only unmarked items or all items. |
| 78 |
toggleUnmarked :: UIState -> UIState |
| 79 |
toggleUnmarked = over statuses (toggleStatus1 Unmarked) |
| 80 |
|
| 81 |
-- | Toggle between showing only pending items or all items. |
| 82 |
togglePending :: UIState -> UIState |
| 83 |
togglePending = over statuses (toggleStatus1 Pending) |
| 84 |
|
| 85 |
-- | Toggle between showing only cleared items or all items. |
| 86 |
toggleCleared :: UIState -> UIState |
| 87 |
toggleCleared = over statuses (toggleStatus1 Cleared) |
| 88 |
|
| 89 |
-- TODO testing different status toggle styles |
| 90 |
|
| 91 |
-- | Generate zero or more indicators of the status filters currently active, |
| 92 |
-- which will be shown comma-separated as part of the indicators list. |
| 93 |
uiShowStatus :: CliOpts -> [Status] -> [String] |
| 94 |
uiShowStatus copts ss = |
| 95 |
case style of |
| 96 |
-- in style 2, instead of "Y, Z" show "not X" |
| 97 |
Just 2 | length ss == numstatuses-1 |
| 98 |
-> map (("not "++). showstatus) $ sort $ complement ss -- should be just one |
| 99 |
_ -> map showstatus $ sort ss |
| 100 |
where |
| 101 |
numstatuses = length [minBound..maxBound::Status] |
| 102 |
style = maybeposintopt "status-toggles" $ rawopts_ copts |
| 103 |
showstatus Cleared = "cleared" |
| 104 |
showstatus Pending = "pending" |
| 105 |
showstatus Unmarked = "unmarked" |
| 106 |
|
| 107 |
-- various toggle behaviours: |
| 108 |
|
| 109 |
-- 1 UPC toggles only X/all |
| 110 |
toggleStatus1 :: Status -> [Status] -> [Status] |
| 111 |
toggleStatus1 s ss = if ss == [s] then [] else [s] |
| 112 |
|
| 113 |
-- 2 UPC cycles X/not-X/all |
| 114 |
-- repeatedly pressing X cycles: |
| 115 |
-- [] U [u] |
| 116 |
-- [u] U [pc] |
| 117 |
-- [pc] U [] |
| 118 |
-- pressing Y after first or second step starts new cycle: |
| 119 |
-- [u] P [p] |
| 120 |
-- [pc] P [p] |
| 121 |
-- toggleStatus s ss |
| 122 |
-- | ss == [s] = complement [s] |
| 123 |
-- | ss == complement [s] = [] |
| 124 |
-- | otherwise = [s] -- XXX assume only three values |
| 125 |
|
| 126 |
-- 3 UPC toggles each X |
| 127 |
-- toggleStatus3 s ss |
| 128 |
-- | s `elem` ss = filter (/= s) ss |
| 129 |
-- | otherwise = simplifyStatuses (s:ss) |
| 130 |
|
| 131 |
-- 4 upc sets X, UPC sets not-X |
| 132 |
-- toggleStatus4 s ss |
| 133 |
-- | s `elem` ss = filter (/= s) ss |
| 134 |
-- | otherwise = simplifyStatuses (s:ss) |
| 135 |
|
| 136 |
-- 5 upc toggles X, UPC toggles not-X |
| 137 |
-- toggleStatus5 s ss |
| 138 |
-- | s `elem` ss = filter (/= s) ss |
| 139 |
-- | otherwise = simplifyStatuses (s:ss) |
| 140 |
|
| 141 |
-- | Given a list of unique enum values, list the other possible values of that enum. |
| 142 |
complement :: (Bounded a, Enum a, Eq a) => [a] -> [a] |
| 143 |
complement = ([minBound..maxBound] \\) |
| 144 |
|
| 145 |
-- |
| 146 |
|
| 147 |
-- | Toggle between showing all and showing only nonempty (more precisely, nonzero) items. |
| 148 |
toggleEmpty :: UIState -> UIState |
| 149 |
toggleEmpty = over empty__ not |
| 150 |
|
| 151 |
-- | Toggle between showing the primary amounts or costs. |
| 152 |
toggleConversionOp :: UIState -> UIState |
| 153 |
toggleConversionOp ui = (over value valOff) (over conversionop toggleCostMode ui) |
| 154 |
where |
| 155 |
toggleCostMode Nothing = Just ToCost |
| 156 |
toggleCostMode (Just NoConversionOp) = Just ToCost |
| 157 |
toggleCostMode (Just ToCost) = Just NoConversionOp |
| 158 |
valOff _ = Nothing |
| 159 |
|
| 160 |
-- | Toggle between showing primary amounts or values (using valuation specified at startup, or a default). |
| 161 |
toggleValue :: UIState -> UIState |
| 162 |
toggleValue ui = (over conversionop costOff) (over value (valuationToggleValue mstartupvaluation0) ui) |
| 163 |
where |
| 164 |
mstartupvaluation0 = value_ $ _rsReportOpts $ reportspec_ $ uoCliOpts $ astartupopts ui |
| 165 |
mdefvaluation = Just (AtEnd Nothing) |
| 166 |
-- valuationToggleValue (maybe startupvaluation) (maybe currentvaluation) = ... |
| 167 |
valuationToggleValue Nothing Nothing = mdefvaluation |
| 168 |
valuationToggleValue Nothing (Just _) = Nothing |
| 169 |
valuationToggleValue mstartupvaluation Nothing = mstartupvaluation |
| 170 |
valuationToggleValue _ (Just _) = Nothing |
| 171 |
costOff _ = Just NoConversionOp |
| 172 |
|
| 173 |
-- | Set hierarchic account tree mode. |
| 174 |
setTree :: UIState -> UIState |
| 175 |
setTree = set accountlistmode ALTree |
| 176 |
|
| 177 |
-- | Set flat account list mode. |
| 178 |
setList :: UIState -> UIState |
| 179 |
setList = set accountlistmode ALFlat |
| 180 |
|
| 181 |
-- | Toggle between flat and tree mode. If current mode is unspecified/default, assume it's flat. |
| 182 |
toggleTree :: UIState -> UIState |
| 183 |
toggleTree = over accountlistmode toggleTreeMode |
| 184 |
where |
| 185 |
toggleTreeMode ALTree = ALFlat |
| 186 |
toggleTreeMode ALFlat = ALTree |
| 187 |
|
| 188 |
-- | Toggle between historical balances and period balances. |
| 189 |
toggleHistorical :: UIState -> UIState |
| 190 |
toggleHistorical = over balanceaccum toggleBalanceAccum |
| 191 |
where |
| 192 |
toggleBalanceAccum Historical = PerPeriod |
| 193 |
toggleBalanceAccum _ = Historical |
| 194 |
|
| 195 |
-- | Toggle hledger-ui's "forecast/future mode". When this mode is enabled, |
| 196 |
-- hledger-shows regular transactions which have future dates, and |
| 197 |
-- "forecast" transactions generated by periodic transaction rules |
| 198 |
-- (which are usually but not necessarily future-dated). |
| 199 |
-- In normal mode, both of these are hidden. |
| 200 |
toggleForecast :: Day -> UIState -> UIState |
| 201 |
toggleForecast _d ui = set forecast newForecast ui |
| 202 |
where |
| 203 |
newForecast = case ui^.forecast of |
| 204 |
Just _ -> Nothing |
| 205 |
Nothing -> enableForecast (astartupopts ui) (ui^.cliOpts) ^. forecast |
| 206 |
|
| 207 |
-- | Enable forecasting in this CliOpts. |
| 208 |
-- If it previously specified a forecast period, or else if the given ui startup options did, |
| 209 |
-- preserve that as the forecast period. |
| 210 |
enableForecast :: UIOpts -> CliOpts -> CliOpts |
| 211 |
enableForecast startopts currentopts = set forecast mforecast currentopts |
| 212 |
where |
| 213 |
mforecast = asum [mcurrentforecastperiod, mstartupforecastperiod, mdefaultforecastperiod] |
| 214 |
where |
| 215 |
mcurrentforecastperiod = currentopts ^. forecast |
| 216 |
mstartupforecastperiod = startopts ^. forecast |
| 217 |
mdefaultforecastperiod = Just nulldatespan |
| 218 |
|
| 219 |
-- | Toggle between showing all and showing only real (non-virtual) items. |
| 220 |
toggleReal :: UIState -> UIState |
| 221 |
toggleReal = fromRight err . overEither real not -- PARTIAL: |
| 222 |
where err = error' "toggleReal: updating Real should not result in an error" |
| 223 |
|
| 224 |
-- | Toggle the ignoring of balance assertions. |
| 225 |
toggleIgnoreBalanceAssertions :: UIState -> UIState |
| 226 |
toggleIgnoreBalanceAssertions = over ignore_assertions not |
| 227 |
|
| 228 |
-- | Step through larger report periods, up to all. |
| 229 |
growReportPeriod :: Day -> UIState -> UIState |
| 230 |
growReportPeriod _d = updateReportPeriod periodGrow |
| 231 |
|
| 232 |
-- | Step through smaller report periods, down to a day. |
| 233 |
shrinkReportPeriod :: Day -> UIState -> UIState |
| 234 |
shrinkReportPeriod d = updateReportPeriod (periodShrink d) |
| 235 |
|
| 236 |
-- | Step the report start/end dates to the next period of same duration, |
| 237 |
-- remaining inside the given enclosing span. |
| 238 |
nextReportPeriod :: DateSpan -> UIState -> UIState |
| 239 |
nextReportPeriod enclosingspan = updateReportPeriod (periodNextIn enclosingspan) |
| 240 |
|
| 241 |
-- | Step the report start/end dates to the next period of same duration, |
| 242 |
-- remaining inside the given enclosing span. |
| 243 |
previousReportPeriod :: DateSpan -> UIState -> UIState |
| 244 |
previousReportPeriod enclosingspan = updateReportPeriod (periodPreviousIn enclosingspan) |
| 245 |
|
| 246 |
-- | If a standard report period is set, step it forward/backward if needed so that |
| 247 |
-- it encloses the given date. |
| 248 |
moveReportPeriodToDate :: Day -> UIState -> UIState |
| 249 |
moveReportPeriodToDate d = updateReportPeriod (periodMoveTo d) |
| 250 |
|
| 251 |
-- | Clear any report period limits. |
| 252 |
resetReportPeriod :: UIState -> UIState |
| 253 |
resetReportPeriod = setReportPeriod PeriodAll |
| 254 |
|
| 255 |
-- | Get the report period. |
| 256 |
reportPeriod :: UIState -> Period |
| 257 |
reportPeriod = (^.period) |
| 258 |
|
| 259 |
-- | Set the report period. |
| 260 |
setReportPeriod :: Period -> UIState -> UIState |
| 261 |
setReportPeriod p = updateReportPeriod (const p) |
| 262 |
|
| 263 |
-- | Update report period by a applying a function. |
| 264 |
updateReportPeriod :: (Period -> Period) -> UIState -> UIState |
| 265 |
updateReportPeriod updatePeriod = fromRight err . overEither period updatePeriod -- PARTIAL: |
| 266 |
where err = error' "updateReportPeriod: updating period should not result in an error" |
| 267 |
|
| 268 |
-- | Apply a new filter query, or return the failing query. |
| 269 |
setFilter :: String -> UIState -> Either String UIState |
| 270 |
setFilter s = first (const s) . setEither querystring (words'' queryprefixes $ T.pack s) |
| 271 |
|
| 272 |
-- | Reset some filters & toggles. |
| 273 |
resetFilter :: UIState -> UIState |
| 274 |
resetFilter = set querystringNoUpdate [] . set realNoUpdate False . set statusesNoUpdate [] |
| 275 |
. set empty__ True -- set period PeriodAll |
| 276 |
. set rsQuery Any . set rsQueryOpts [] |
| 277 |
|
| 278 |
-- -- | Reset all options state to exactly what it was at startup |
| 279 |
-- -- (preserving any command-line options/arguments). |
| 280 |
-- resetOpts :: UIState -> UIState |
| 281 |
-- resetOpts ui@UIState{astartupopts} = ui{aopts=astartupopts} |
| 282 |
|
| 283 |
resetDepth :: UIState -> UIState |
| 284 |
resetDepth = updateReportDepth (const mempty) |
| 285 |
|
| 286 |
-- | Get the maximum account depth in the current journal. |
| 287 |
maxDepth :: UIState -> Int |
| 288 |
maxDepth UIState{ajournal=j} = getMax . foldMap (Max . accountNameLevel) $ journalAccountNamesDeclaredOrImplied j |
| 289 |
|
| 290 |
-- | Decrement the current depth limit towards 0. If there was no depth limit, |
| 291 |
-- set it to one less than the maximum account depth. |
| 292 |
decDepth :: UIState -> UIState |
| 293 |
decDepth ui = updateReportDepth dec ui |
| 294 |
where |
| 295 |
dec (DepthSpec (Just d) _) = DepthSpec (Just $ max 0 (d-1)) [] |
| 296 |
dec (DepthSpec Nothing _) = DepthSpec (Just $ maxDepth ui - 1) [] |
| 297 |
|
| 298 |
-- | Increment the current depth limit. If this makes it equal to the |
| 299 |
-- the maximum account depth, remove the depth limit. |
| 300 |
incDepth :: UIState -> UIState |
| 301 |
incDepth = updateReportDepth inc |
| 302 |
where |
| 303 |
inc (DepthSpec Nothing _) = DepthSpec Nothing [] |
| 304 |
inc (DepthSpec (Just d) _) = DepthSpec (Just $ d + 1) [] |
| 305 |
|
| 306 |
-- | Set the current depth limit to the specified depth, or remove the depth limit. |
| 307 |
-- Also remove the depth limit if the specified depth is greater than the current |
| 308 |
-- maximum account depth. If the specified depth is negative, reset the depth limit |
| 309 |
-- to whatever was specified at uiartup. |
| 310 |
setDepth :: Maybe Int -> UIState -> UIState |
| 311 |
setDepth mdepth = updateReportDepth (const $ DepthSpec mdepth []) |
| 312 |
|
| 313 |
getDepth :: UIState -> Maybe Int |
| 314 |
getDepth = dsFlatDepth . (^.depth) |
| 315 |
|
| 316 |
-- | Update report depth by a applying a function. If asked to set a depth less |
| 317 |
-- than zero, it will leave it unchanged. |
| 318 |
updateReportDepth :: (DepthSpec -> DepthSpec) -> UIState -> UIState |
| 319 |
updateReportDepth updateDepth ui = over reportSpec update ui |
| 320 |
where |
| 321 |
update = fromRight (error' "updateReportDepth: updating depth should not result in an error") -- PARTIAL: |
| 322 |
. updateReportSpecWith (\ropts -> ropts{depth_=clipDepth ropts $ updateDepth (depth_ ropts)}) |
| 323 |
clipDepth _ (DepthSpec Nothing _) = mempty |
| 324 |
clipDepth ropts ds@(DepthSpec (Just d) _) | d < 0 = depth_ ropts |
| 325 |
| d >= maxDepth ui = mempty |
| 326 |
| otherwise = ds |
| 327 |
|
| 328 |
-- | Open the minibuffer, setting its content to the current query with the cursor at the end. |
| 329 |
showMinibuffer :: T.Text -> Maybe String -> UIState -> UIState |
| 330 |
showMinibuffer label moldq ui = setMode (Minibuffer label e) ui |
| 331 |
where |
| 332 |
e = applyEdit gotoEOL $ editor MinibufferEditor (Just 1) oldq |
| 333 |
oldq = fromMaybe (T.unpack . T.unwords . map textQuoteIfNeeded $ ui^.querystring) moldq |
| 334 |
|
| 335 |
-- | Close the minibuffer, discarding any edit in progress. |
| 336 |
closeMinibuffer :: UIState -> UIState |
| 337 |
closeMinibuffer = setMode Normal |
| 338 |
|
| 339 |
setMode :: Mode -> UIState -> UIState |
| 340 |
setMode m ui = ui{aMode=m} |
| 341 |
|
| 342 |
pushScreen :: Screen -> UIState -> UIState |
| 343 |
pushScreen scr ui = |
| 344 |
dbg1Msg ("pushing screen " <> showScreenId scr <> ". " <> showScreenStack "" showScreenId ui1) |
| 345 |
ui1 |
| 346 |
where ui1 = ui{aPrevScreens=aScreen ui:aPrevScreens ui, aScreen=scr } |
| 347 |
|
| 348 |
popScreen :: UIState -> UIState |
| 349 |
popScreen ui@UIState{aPrevScreens = s : ss} = |
| 350 |
dbg1Msg ("popping screen " <> showScreenId (aScreen ui) <> ". " <> showScreenStack "" showScreenId ui1) |
| 351 |
ui1 |
| 352 |
where ui1 = ui{aPrevScreens = ss ,aScreen = s } |
| 353 |
popScreen ui = ui |
| 354 |
|
| 355 |
-- | Reset options to their startup values, discard screen navigation history, |
| 356 |
-- and return to the top screen, regenerating it with the startup options |
| 357 |
-- and the provided reporting date. |
| 358 |
resetScreens :: Day -> UIState -> UIState |
| 359 |
resetScreens d ui@UIState{astartupopts=origopts, ajournal=j, aScreen=s,aPrevScreens=ss} = |
| 360 |
ui{aopts=origopts, aPrevScreens=[], aScreen=topscreen', aMode=Normal} |
| 361 |
where |
| 362 |
topscreen' = screenUpdate origopts d j $ lastDef s ss |
| 363 |
|
| 364 |
-- | Given a new journal and reporting date, save the new journal in the ui state, |
| 365 |
-- then regenerate the content of all screens in the stack |
| 366 |
-- (using the ui state's current options), preserving the screen navigation history. |
| 367 |
-- Note, does not save the reporting date. |
| 368 |
-- |
| 369 |
-- XXX Currently this does not properly regenerate the transaction screen or error screen, |
| 370 |
-- which depend on state from their parent(s); those screens' handlers must do additional work, which is fragile. |
| 371 |
regenerateScreens :: Journal -> Day -> UIState -> UIState |
| 372 |
regenerateScreens j d ui@UIState{aopts=opts, aScreen=s,aPrevScreens=ss} = |
| 373 |
ui{ajournal=j, aScreen=screenUpdate opts d j s, aPrevScreens=map (screenUpdate opts d j) ss} |