| 1 |
-- The accounts screen, showing accounts and balances like the CLI balance command. |
| 2 |
|
| 3 |
{-# LANGUAGE CPP #-} |
| 4 |
{-# LANGUAGE OverloadedStrings #-} |
| 5 |
{-# LANGUAGE RecordWildCards #-} |
| 6 |
{-# LANGUAGE TupleSections #-} |
| 7 |
|
| 8 |
module Hledger.UI.AccountsScreen |
| 9 |
(asNew |
| 10 |
,asUpdate |
| 11 |
,asDraw |
| 12 |
,asDrawHelper |
| 13 |
,asHandle |
| 14 |
,handleHelpMode |
| 15 |
,handleMinibufferMode |
| 16 |
,asHandleNormalMode |
| 17 |
,enterRegisterScreen |
| 18 |
,asSetSelectedAccount |
| 19 |
) |
| 20 |
where |
| 21 |
|
| 22 |
import Brick |
| 23 |
import Brick.Widgets.List |
| 24 |
import Brick.Widgets.Edit |
| 25 |
import Control.Monad |
| 26 |
import Control.Monad.IO.Class (liftIO) |
| 27 |
#if MIN_VERSION_base(4,19,0) |
| 28 |
import Data.List hiding (reverse, (!?)) |
| 29 |
#else |
| 30 |
import Data.List hiding (reverse) |
| 31 |
#endif |
| 32 |
import Data.Maybe |
| 33 |
import Data.Text qualified as T |
| 34 |
import Data.Time.Calendar (Day) |
| 35 |
import Data.Vector qualified as V |
| 36 |
import Data.Vector ((!?)) |
| 37 |
import Graphics.Vty (Event(..),Key(..),Modifier(..), Button (BLeft, BScrollDown, BScrollUp)) |
| 38 |
import Lens.Micro.Platform |
| 39 |
import System.Console.ANSI |
| 40 |
import System.FilePath (takeFileName) |
| 41 |
import Text.DocLayout (realLength) |
| 42 |
|
| 43 |
import Hledger |
| 44 |
import Hledger.Cli hiding (Mode, mode, progname, prognameandversion) |
| 45 |
import Hledger.UI.UIOptions |
| 46 |
import Hledger.UI.UITypes |
| 47 |
import Hledger.UI.UIState |
| 48 |
import Hledger.UI.UIUtils |
| 49 |
import Hledger.UI.UIScreens |
| 50 |
import Hledger.UI.Editor |
| 51 |
import Hledger.UI.ErrorScreen (uiReload, uiReloadIfFileChanged, uiToggleBalanceAssertions) |
| 52 |
import Hledger.UI.RegisterScreen (rsCenterSelection) |
| 53 |
import Data.Either (fromRight) |
| 54 |
import Control.Arrow ((>>>)) |
| 55 |
import Safe (headDef) |
| 56 |
|
| 57 |
|
| 58 |
asDraw :: UIState -> [Widget Name] |
| 59 |
asDraw ui = dbgui "asDraw" $ asDrawHelper ui ropts' scrname |
| 60 |
where |
| 61 |
ropts' = _rsReportOpts $ reportspec_ $ uoCliOpts $ aopts ui |
| 62 |
scrname = "account " ++ if ishistorical then "balances" else "changes" |
| 63 |
where ishistorical = balanceaccum_ ropts' == Historical |
| 64 |
|
| 65 |
-- | Help draw any accounts-like screen (all accounts, balance sheet, income statement..). |
| 66 |
-- The provided ReportOpts are used instead of the ones in the UIState. |
| 67 |
-- The other argument is the screen display name. |
| 68 |
asDrawHelper :: UIState -> ReportOpts -> String -> [Widget Name] |
| 69 |
asDrawHelper UIState{aScreen=scr, aopts=uopts, ajournal=j, aMode=mode} ropts scrname = |
| 70 |
dbgui "asDrawHelper" $ |
| 71 |
case toAccountsLikeScreen scr of |
| 72 |
Nothing -> dbgui "asDrawHelper" $ errorWrongScreenType "asDrawHelper" -- PARTIAL: |
| 73 |
Just (ALS _ ass) -> case mode of |
| 74 |
Help -> [helpDialog, maincontent] |
| 75 |
_ -> [maincontent] |
| 76 |
where |
| 77 |
UIOpts{uoCliOpts=copts} = uopts |
| 78 |
maincontent = Widget Greedy Greedy $ do |
| 79 |
c <- getContext |
| 80 |
let |
| 81 |
availwidth = |
| 82 |
-- ltrace "availwidth" $ |
| 83 |
c^.availWidthL |
| 84 |
- 2 -- XXX due to margin ? shouldn't be necessary (cf UIUtils) |
| 85 |
displayitems = ass ^. assList . listElementsL |
| 86 |
|
| 87 |
acctwidths = V.map (\AccountsScreenItem{..} -> asItemIndentLevel + realLength asItemDisplayAccountName) displayitems |
| 88 |
balwidths = V.map (maybe 0 (wbWidth . showMixedAmountB oneLineNoCostFmt) . asItemMixedAmount) displayitems |
| 89 |
preferredacctwidth = V.maximum acctwidths |
| 90 |
totalacctwidthseen = V.sum acctwidths |
| 91 |
preferredbalwidth = V.maximum balwidths |
| 92 |
totalbalwidthseen = V.sum balwidths |
| 93 |
|
| 94 |
totalwidthseen = totalacctwidthseen + totalbalwidthseen |
| 95 |
shortfall = preferredacctwidth + preferredbalwidth + 2 - availwidth |
| 96 |
acctwidthproportion = fromIntegral totalacctwidthseen `divideSafe` fromIntegral totalwidthseen |
| 97 |
adjustedacctwidth = min preferredacctwidth . max 15 . round $ acctwidthproportion * fromIntegral (availwidth - 2) -- leave 2 whitespace for padding |
| 98 |
adjustedbalwidth = availwidth - 2 - adjustedacctwidth |
| 99 |
|
| 100 |
-- XXX how to minimise the balance column's jumping around as you change the depth limit ? |
| 101 |
|
| 102 |
colwidths | shortfall <= 0 = (preferredacctwidth, preferredbalwidth) |
| 103 |
| otherwise = (adjustedacctwidth, adjustedbalwidth) |
| 104 |
|
| 105 |
render $ defaultLayout toplabel bottomlabel $ renderList (asDrawItem colwidths) True (ass ^. assList) |
| 106 |
|
| 107 |
where |
| 108 |
ishistorical = balanceaccum_ ropts == Historical |
| 109 |
|
| 110 |
toplabel = |
| 111 |
withAttr (attrName "border" <> attrName "filename") files |
| 112 |
<+> toggles |
| 113 |
<+> str (" " ++ scrname) |
| 114 |
<+> borderPeriodStr (if ishistorical then "at end of" else "in") (period_ ropts) |
| 115 |
<+> borderQueryStr (T.unpack . T.unwords . map textQuoteIfNeeded $ querystring_ ropts) |
| 116 |
<+> borderDepthStr mdepth |
| 117 |
<+> str (" ("++curidx++"/"++totidx++")") |
| 118 |
<+> (if ignore_assertions_ . balancingopts_ $ inputopts_ copts |
| 119 |
then withAttr (attrName "border" <> attrName "query") (str " ignoring balance assertions") |
| 120 |
else str "") |
| 121 |
where |
| 122 |
files = case journalFilePaths j of |
| 123 |
[] -> str "" |
| 124 |
f:_ -> str $ takeFileName f |
| 125 |
-- [f,_:[]] -> (withAttr ("border" <> "bold") $ str $ takeFileName f) <+> str " (& 1 included file)" |
| 126 |
-- f:fs -> (withAttr ("border" <> "bold") $ str $ takeFileName f) <+> str (" (& " ++ show (length fs) ++ " included files)") |
| 127 |
toggles = withAttr (attrName "border" <> attrName "query") $ str $ unwords $ concat [ |
| 128 |
[""] |
| 129 |
,if empty_ ropts then [] else ["nonzero"] |
| 130 |
,uiShowStatus copts $ statuses_ ropts |
| 131 |
,if real_ ropts then ["real"] else [] |
| 132 |
] |
| 133 |
mdepth = dsFlatDepth $ depth_ ropts |
| 134 |
curidx = case ass ^. assList . listSelectedL of |
| 135 |
Nothing -> "-" |
| 136 |
Just i -> show (i + 1) |
| 137 |
totidx = show $ V.length nonblanks |
| 138 |
where |
| 139 |
nonblanks = V.takeWhile (not . T.null . asItemAccountName) $ ass ^. assList . listElementsL |
| 140 |
|
| 141 |
bottomlabel = case mode of |
| 142 |
Minibuffer label ed -> minibuffer label ed |
| 143 |
_ -> quickhelp |
| 144 |
where |
| 145 |
quickhelp = borderKeysStr' [ |
| 146 |
("LEFT", str "back") |
| 147 |
-- ,("RIGHT", str "register") |
| 148 |
,("t", renderToggle (tree_ ropts) "list" "tree") |
| 149 |
-- ,("t", str "tree") |
| 150 |
-- ,("l", str "list") |
| 151 |
,("-+", str "depth") |
| 152 |
,case scr of |
| 153 |
BS _ -> ("", str "") |
| 154 |
IS _ -> ("", str "") |
| 155 |
_ -> ("H", renderToggle (not ishistorical) "end-bals" "changes") |
| 156 |
,("F", renderToggle1 (isJust . forecast_ $ inputopts_ copts) "forecast") |
| 157 |
--,("/", "filter") |
| 158 |
--,("DEL", "unfilter") |
| 159 |
--,("ESC", "cancel/top") |
| 160 |
-- ,("a", str "add") |
| 161 |
-- ,("g", "reload") |
| 162 |
,("?", str "help") |
| 163 |
-- ,("q", str "quit") |
| 164 |
] |
| 165 |
|
| 166 |
asDrawItem :: (Int,Int) -> Bool -> AccountsScreenItem -> Widget Name |
| 167 |
asDrawItem (acctwidth, balwidth) selected AccountsScreenItem{..} = |
| 168 |
Widget Greedy Fixed $ do |
| 169 |
-- c <- getContext |
| 170 |
-- let showitem = intercalate "\n" . balanceReportItemAsText defreportopts fmt |
| 171 |
render $ |
| 172 |
txt (fitText (Just acctwidth) (Just acctwidth) True True $ T.replicate (asItemIndentLevel) " " <> asItemDisplayAccountName) <+> |
| 173 |
txt balspace <+> |
| 174 |
splitAmounts balBuilder |
| 175 |
where |
| 176 |
balBuilder = maybe mempty showamt asItemMixedAmount |
| 177 |
showamt = showMixedAmountB oneLineNoCostFmt{displayMinWidth=Just balwidth, displayMaxWidth=Just balwidth} |
| 178 |
balspace = T.replicate (2 + balwidth - wbWidth balBuilder) " " |
| 179 |
splitAmounts = foldr1 (<+>) . intersperse (str ", ") . map renderamt . T.splitOn ", " . wbToText |
| 180 |
renderamt :: T.Text -> Widget Name |
| 181 |
renderamt a | T.any (=='-') a = withAttr (sel $ attrName "list" <> attrName "balance" <> attrName "negative") $ txt a |
| 182 |
| otherwise = withAttr (sel $ attrName "list" <> attrName "balance" <> attrName "positive") $ txt a |
| 183 |
sel | selected = (<> attrName "selected") |
| 184 |
| otherwise = id |
| 185 |
|
| 186 |
-- | Handle events on any accounts-like screen (all accounts, balance sheet, income statement..). |
| 187 |
asHandle :: BrickEvent Name AppEvent -> EventM Name UIState () |
| 188 |
asHandle ev = do |
| 189 |
dbguiEv "asHandle" |
| 190 |
ui0@UIState{aScreen=scr, aMode=mode} <- get' |
| 191 |
case toAccountsLikeScreen scr of |
| 192 |
Nothing -> dbgui "asHandle" $ errorWrongScreenType "asHandle" -- PARTIAL: |
| 193 |
Just als@(ALS scons ass) -> do |
| 194 |
-- save the currently selected account, in case we leave this screen and lose the selection |
| 195 |
put' ui0{aScreen=scons ass{_assSelectedAccount=asSelectedAccount ass}} |
| 196 |
case mode of |
| 197 |
Normal -> asHandleNormalMode als ev |
| 198 |
Minibuffer _ ed -> handleMinibufferMode ed ev |
| 199 |
Help -> handleHelpMode ev |
| 200 |
|
| 201 |
-- | Handle events when in normal mode on any accounts-like screen. |
| 202 |
-- The provided AccountsLikeScreen should correspond to the ui state's current screen. |
| 203 |
asHandleNormalMode :: AccountsLikeScreen -> BrickEvent Name AppEvent -> EventM Name UIState () |
| 204 |
asHandleNormalMode (ALS scons ass) ev = do |
| 205 |
dbguiEv "asHandleNormalMode" |
| 206 |
|
| 207 |
ui@UIState{aopts=UIOpts{uoCliOpts=copts}, ajournal=j} <- get' |
| 208 |
d <- liftIO getCurrentDay |
| 209 |
let |
| 210 |
l = _assList ass |
| 211 |
selacct = asSelectedAccount ass |
| 212 |
centerSelection = scrollSelectionToMiddle l |
| 213 |
clickedAcctAt y = |
| 214 |
case asItemAccountName <$> listElements l !? y of |
| 215 |
Just t | not $ T.null t -> Just t |
| 216 |
_ -> Nothing |
| 217 |
nonblanks = V.takeWhile (not . T.null . asItemAccountName) $ listElements l |
| 218 |
lastnonblankidx = max 0 (length nonblanks - 1) |
| 219 |
journalspan = journalDateSpan False j |
| 220 |
|
| 221 |
case ev of |
| 222 |
|
| 223 |
VtyEvent (EvKey (KChar 'q') []) -> halt -- q: quit |
| 224 |
VtyEvent (EvKey (KChar 'z') [MCtrl]) -> suspend ui -- C-z: suspend |
| 225 |
VtyEvent (EvKey (KChar 'l') [MCtrl]) -> centerSelection >> redraw -- C-l: redraw |
| 226 |
VtyEvent (EvKey KEsc []) -> modify' (resetScreens d) -- ESC: reset |
| 227 |
VtyEvent (EvKey (KChar c) []) | c == '?' -> modify' (setMode Help) -- ?: enter help mode |
| 228 |
|
| 229 |
-- AppEvents come from the system, in --watch mode. |
| 230 |
-- XXX currently they are handled only in Normal mode |
| 231 |
-- XXX be sure we don't leave unconsumed app events piling up |
| 232 |
-- A data file has changed (or the user has pressed g): reload. |
| 233 |
e | e `elem` [AppEvent FileChange, VtyEvent (EvKey (KChar 'g') [])] -> |
| 234 |
uiReload copts d ui >>= put' |
| 235 |
|
| 236 |
-- The date has changed (and we are viewing a standard period which contained the old date): |
| 237 |
-- adjust the viewed period and regenerate, just in case needed. |
| 238 |
-- (Eg: when watching data for "today" and the time has just passed midnight.) |
| 239 |
AppEvent (DateChange old _) | isStandardPeriod p && p `periodContainsDate` old -> |
| 240 |
modify' (setReportPeriod (DayPeriod d) >>> regenerateScreens j d) |
| 241 |
where p = reportPeriod ui |
| 242 |
|
| 243 |
-- set or reset a filter: |
| 244 |
VtyEvent (EvKey (KChar '/') []) -> modify' (showMinibuffer "filter" Nothing >>> regenerateScreens j d) |
| 245 |
VtyEvent (EvKey k []) | k `elem` [KBS, KDel] -> modify' (resetFilter >>> regenerateScreens j d) |
| 246 |
|
| 247 |
-- run external programs: |
| 248 |
VtyEvent (EvKey (KChar 'a') []) -> suspendAndResume $ clearScreen >> setCursorPosition 0 0 >> add (cliOptsDropArgs copts) j >> uiReloadIfFileChanged copts d j ui |
| 249 |
VtyEvent (EvKey (KChar 'A') []) -> suspendAndResume $ void (runIadd (journalFilePath j)) >> uiReloadIfFileChanged copts d j ui |
| 250 |
VtyEvent (EvKey (KChar 'E') []) -> suspendAndResume $ void (runEditor endPosition (journalFilePath j)) >> uiReloadIfFileChanged copts d j ui |
| 251 |
-- J/K Jumps: move 10 rows at a time |
| 252 |
VtyEvent (EvKey (KChar 'J') []) -> do |
| 253 |
let l' = listMoveBy 10 l |
| 254 |
-- Ensure we don't jump into the blank padding at the end |
| 255 |
let l'' = if isBlankItem (listSelectedElement l') |
| 256 |
then listMoveTo lastnonblankidx l' |
| 257 |
else l' |
| 258 |
put' ui{aScreen=scons ass{_assList=l''}} |
| 259 |
|
| 260 |
VtyEvent (EvKey (KChar 'K') []) -> do |
| 261 |
let l' = listMoveBy (-10) l |
| 262 |
put' ui{aScreen=scons ass{_assList=l'}} |
| 263 |
|
| 264 |
-- adjust the period displayed: |
| 265 |
VtyEvent (EvKey (KChar 'T') []) -> modify' (setReportPeriod (DayPeriod d) >>> regenerateScreens j d) |
| 266 |
VtyEvent (EvKey (KDown) [MShift]) -> modify' (shrinkReportPeriod d >>> regenerateScreens j d) |
| 267 |
VtyEvent (EvKey (KUp) [MShift]) -> modify' (growReportPeriod d >>> regenerateScreens j d) |
| 268 |
VtyEvent (EvKey (KRight) [MShift]) -> modify' (nextReportPeriod journalspan >>> regenerateScreens j d) |
| 269 |
VtyEvent (EvKey (KLeft) [MShift]) -> modify' (previousReportPeriod journalspan >>> regenerateScreens j d) |
| 270 |
|
| 271 |
-- various toggles and settings: |
| 272 |
VtyEvent (EvKey (KChar 'I') []) -> get' >>= uiToggleBalanceAssertions d |
| 273 |
VtyEvent (EvKey (KChar 'F') []) -> modify' (toggleForecast d >>> regenerateScreens j d) |
| 274 |
VtyEvent (EvKey (KChar 'B') []) -> modify' (toggleConversionOp >>> regenerateScreens j d) |
| 275 |
VtyEvent (EvKey (KChar 'V') []) -> modify' (toggleValue >>> regenerateScreens j d) |
| 276 |
VtyEvent (EvKey (KChar '0') []) -> modify' (setDepth (Just 0) >>> regenerateScreens j d) |
| 277 |
VtyEvent (EvKey (KChar '1') []) -> modify' (setDepth (Just 1) >>> regenerateScreens j d) |
| 278 |
VtyEvent (EvKey (KChar '2') []) -> modify' (setDepth (Just 2) >>> regenerateScreens j d) |
| 279 |
VtyEvent (EvKey (KChar '3') []) -> modify' (setDepth (Just 3) >>> regenerateScreens j d) |
| 280 |
VtyEvent (EvKey (KChar '4') []) -> modify' (setDepth (Just 4) >>> regenerateScreens j d) |
| 281 |
VtyEvent (EvKey (KChar '5') []) -> modify' (setDepth (Just 5) >>> regenerateScreens j d) |
| 282 |
VtyEvent (EvKey (KChar '6') []) -> modify' (setDepth (Just 6) >>> regenerateScreens j d) |
| 283 |
VtyEvent (EvKey (KChar '7') []) -> modify' (setDepth (Just 7) >>> regenerateScreens j d) |
| 284 |
VtyEvent (EvKey (KChar '8') []) -> modify' (setDepth (Just 8) >>> regenerateScreens j d) |
| 285 |
VtyEvent (EvKey (KChar '9') []) -> modify' (setDepth (Just 9) >>> regenerateScreens j d) |
| 286 |
VtyEvent (EvKey (KChar c) []) | c `elem` ['-','_'] -> modify' (decDepth >>> regenerateScreens j d) |
| 287 |
VtyEvent (EvKey (KChar c) []) | c `elem` ['+','='] -> modify' (incDepth >>> regenerateScreens j d) |
| 288 |
-- toggles after which the selection should be recentered: |
| 289 |
VtyEvent (EvKey (KChar 'H') []) -> modify' (toggleHistorical >>> regenerateScreens j d) >> centerSelection -- harmless on BS/IS screens |
| 290 |
VtyEvent (EvKey (KChar 't') []) -> modify' (toggleTree >>> regenerateScreens j d) >> centerSelection |
| 291 |
VtyEvent (EvKey (KChar 'R') []) -> modify' (toggleReal >>> regenerateScreens j d) >> centerSelection |
| 292 |
VtyEvent (EvKey (KChar 'U') []) -> modify' (toggleUnmarked >>> regenerateScreens j d) >> centerSelection |
| 293 |
VtyEvent (EvKey (KChar 'P') []) -> modify' (togglePending >>> regenerateScreens j d) >> centerSelection |
| 294 |
VtyEvent (EvKey (KChar 'C') []) -> modify' (toggleCleared >>> regenerateScreens j d) >> centerSelection |
| 295 |
VtyEvent (EvKey (KChar c) []) | c `elem` ['z','Z'] -> modify' (toggleEmpty >>> regenerateScreens j d) >> centerSelection -- back compat: accept Z as well as z |
| 296 |
|
| 297 |
-- LEFT key or a click in the app's left margin: exit to the parent screen. |
| 298 |
VtyEvent e | e `elem` moveLeftEvents -> modify' popScreen |
| 299 |
VtyEvent (EvMouseUp 0 _ (Just BLeft)) -> modify' popScreen -- this mouse click is a VtyEvent since not in a clickable widget |
| 300 |
|
| 301 |
-- RIGHT key or MouseUp on an account: enter the register screen for the selected account |
| 302 |
VtyEvent e | e `elem` moveRightEvents, not $ isBlankItem $ listSelectedElement l -> enterRegisterScreen d selacct ui |
| 303 |
MouseUp _n (Just BLeft) Location{loc=(_,y)} | Just clkacct <- clickedAcctAt y -> enterRegisterScreen d clkacct ui |
| 304 |
|
| 305 |
-- MouseDown: this is not debounced and can repeat (https://github.com/jtdaugherty/brick/issues/347) |
| 306 |
-- so we only let it do something harmless: move the selection. |
| 307 |
MouseDown _n BLeft _mods Location{loc=(_,y)} | not $ isBlankItem clickeditem -> |
| 308 |
put' ui{aScreen=scons ass'} |
| 309 |
where |
| 310 |
clickeditem = (0,) <$> listElements l !? y |
| 311 |
ass' = ass{_assList=listMoveTo y l} |
| 312 |
|
| 313 |
-- Mouse scroll wheel: scroll up or down to the maximum extent, pushing the selection when necessary. |
| 314 |
MouseDown name btn _mods _loc | btn `elem` [BScrollUp, BScrollDown] -> do |
| 315 |
let scrollamt = if btn==BScrollUp then -1 else 1 |
| 316 |
l' <- nestEventM' l $ listScrollPushingSelection name (asListSize l) scrollamt |
| 317 |
put' ui{aScreen=scons ass{_assList=l'}} |
| 318 |
|
| 319 |
-- PGDOWN/END keys: handle with List's default handler, but restrict the selection to stop |
| 320 |
-- (and center) at the last non-blank item. |
| 321 |
VtyEvent e@(EvKey k []) | k `elem` [KPageDown, KEnd] -> do |
| 322 |
l1 <- nestEventM' l $ handleListEvent e |
| 323 |
if isBlankItem $ listSelectedElement l1 |
| 324 |
then do |
| 325 |
let l2 = listMoveTo lastnonblankidx l1 |
| 326 |
scrollSelectionToMiddle l2 |
| 327 |
put' ui{aScreen=scons ass{_assList=l2}} |
| 328 |
else |
| 329 |
put' ui{aScreen=scons ass{_assList=l1}} |
| 330 |
|
| 331 |
-- DOWN key when selection is at the last item: scroll instead of moving, until maximally scrolled |
| 332 |
VtyEvent e | e `elem` moveDownEvents, isBlankItem mnextelement -> vScrollBy (viewportScroll $ l^.listNameL) 1 |
| 333 |
where mnextelement = listSelectedElement $ listMoveDown l |
| 334 |
|
| 335 |
-- Any other vty event (UP, DOWN, PGUP etc): handle with List's default handler. |
| 336 |
VtyEvent e -> do |
| 337 |
l' <- nestEventM' l $ handleListEvent (normaliseMovementKeys e) |
| 338 |
put' ui{aScreen=scons $ ass & assList .~ l' & assSelectedAccount .~ selacct} |
| 339 |
|
| 340 |
-- Any other mouse/app event: ignore |
| 341 |
MouseDown{} -> return () |
| 342 |
MouseUp{} -> return () |
| 343 |
AppEvent _ -> return () |
| 344 |
|
| 345 |
-- | Handle events when in minibuffer mode on any screen. |
| 346 |
handleMinibufferMode ed ev = do |
| 347 |
ui@UIState{ajournal=j} <- get' |
| 348 |
d <- liftIO getCurrentDay |
| 349 |
case ev of |
| 350 |
VtyEvent (EvKey KEsc []) -> put' $ closeMinibuffer ui |
| 351 |
VtyEvent (EvKey KEnter []) -> put' $ regenerateScreens j d ui' |
| 352 |
where |
| 353 |
ui' = setFilter s (closeMinibuffer ui) |
| 354 |
& fromRight (showMinibuffer "Cannot compile regular expression" (Just s) ui) |
| 355 |
where s = chomp $ unlines $ map strip $ getEditContents ed |
| 356 |
VtyEvent (EvKey (KChar 'l') [MCtrl]) -> redraw |
| 357 |
VtyEvent (EvKey (KChar 'z') [MCtrl]) -> suspend ui |
| 358 |
VtyEvent e -> do |
| 359 |
ed' <- nestEventM' ed $ handleEditorEvent (VtyEvent e) |
| 360 |
put' ui{aMode=Minibuffer "filter" ed'} |
| 361 |
AppEvent _ -> return () |
| 362 |
MouseDown{} -> return () |
| 363 |
MouseUp{} -> return () |
| 364 |
|
| 365 |
-- | Handle events when in help mode on any screen. |
| 366 |
handleHelpMode ev = do |
| 367 |
ui <- get' |
| 368 |
case ev of |
| 369 |
-- VtyEvent (EvKey (KChar 'q') []) -> halt |
| 370 |
VtyEvent (EvKey (KChar 'l') [MCtrl]) -> redraw |
| 371 |
VtyEvent (EvKey (KChar 'z') [MCtrl]) -> suspend ui |
| 372 |
_ -> helpHandle ev |
| 373 |
|
| 374 |
enterRegisterScreen :: Day -> AccountName -> UIState -> EventM Name UIState () |
| 375 |
enterRegisterScreen d acct ui@UIState{ajournal=j, aopts=uopts} = do |
| 376 |
dbguiEv "enterRegisterScreen" |
| 377 |
let |
| 378 |
regscr = rsNew uopts d j acct isdepthclipped |
| 379 |
where |
| 380 |
isdepthclipped = case getDepth ui of |
| 381 |
Just de -> accountNameLevel acct >= de |
| 382 |
Nothing -> False |
| 383 |
ui1 = pushScreen regscr ui |
| 384 |
rsCenterSelection ui1 >>= put' |
| 385 |
|
| 386 |
-- | From any accounts screen's state, get the account name from the |
| 387 |
-- currently selected list item, or otherwise the last known selected account name. |
| 388 |
asSelectedAccount :: AccountsScreenState -> AccountName |
| 389 |
asSelectedAccount ass = |
| 390 |
case listSelectedElement $ _assList ass of |
| 391 |
Just (_, AccountsScreenItem{..}) -> asItemAccountName |
| 392 |
Nothing -> ass ^. assSelectedAccount |
| 393 |
|
| 394 |
-- | Set the selected account on any of the accounts screens. Has no effect on other screens. |
| 395 |
-- Sets the high-level property _assSelectedAccount and also selects the corresponding or |
| 396 |
-- best alternative item in the list widget (_assList). |
| 397 |
asSetSelectedAccount :: AccountName -> Screen -> Screen |
| 398 |
asSetSelectedAccount acct scr = |
| 399 |
case scr of |
| 400 |
(AS ass) -> AS $ assSetSelectedAccount acct ass |
| 401 |
(BS ass) -> BS $ assSetSelectedAccount acct ass |
| 402 |
(IS ass) -> IS $ assSetSelectedAccount acct ass |
| 403 |
_ -> scr |
| 404 |
where |
| 405 |
assSetSelectedAccount a ass@ASS{_assList=l} = |
| 406 |
ass{_assSelectedAccount=a, _assList=listMoveTo selidx l} |
| 407 |
where |
| 408 |
-- which list item should be selected ? |
| 409 |
selidx = headDef 0 $ catMaybes [ |
| 410 |
elemIndex a as -- the specified account, if it can be found |
| 411 |
,findIndex (a `isAccountNamePrefixOf`) as -- or the first account found with the same prefix |
| 412 |
,Just $ max 0 (length (filter (< a) as) - 1) -- otherwise, the alphabetically preceding account. |
| 413 |
] |
| 414 |
where |
| 415 |
as = map asItemAccountName $ V.toList $ listElements l |
| 416 |
|
| 417 |
isBlankItem mitem = ((asItemAccountName . snd) <$> mitem) == Just "" |
| 418 |
|
| 419 |
asListSize = V.length . V.takeWhile ((/="").asItemAccountName) . listElements |
| 420 |
|
| 421 |
|
| 422 |
|