| 1 |
-- The menu screen, showing other screens available in hledger-ui. |
| 2 |
|
| 3 |
{-# LANGUAGE OverloadedStrings #-} |
| 4 |
{-# LANGUAGE RecordWildCards #-} |
| 5 |
|
| 6 |
module Hledger.UI.MenuScreen |
| 7 |
(msNew |
| 8 |
,msUpdate |
| 9 |
,msDraw |
| 10 |
,msHandle |
| 11 |
,msSetSelectedScreen |
| 12 |
) |
| 13 |
where |
| 14 |
|
| 15 |
import Brick |
| 16 |
import Brick.Widgets.List |
| 17 |
import Control.Monad |
| 18 |
import Control.Monad.IO.Class (liftIO) |
| 19 |
import Data.Maybe |
| 20 |
import Data.Text qualified as T |
| 21 |
import Data.Time.Calendar (Day) |
| 22 |
import Data.Vector qualified as V |
| 23 |
import Data.Vector ((!?)) |
| 24 |
import Graphics.Vty (Event(..),Key(..),Modifier(..), Button (BLeft, BScrollDown, BScrollUp)) |
| 25 |
import Lens.Micro.Platform |
| 26 |
import System.Console.ANSI |
| 27 |
import System.FilePath (takeFileName) |
| 28 |
|
| 29 |
import Hledger |
| 30 |
import Hledger.Cli hiding (mode, progname, prognameandversion) |
| 31 |
import Hledger.UI.UIOptions |
| 32 |
import Hledger.UI.UITypes |
| 33 |
import Hledger.UI.UIState |
| 34 |
import Hledger.UI.UIUtils |
| 35 |
import Hledger.UI.UIScreens |
| 36 |
import Hledger.UI.ErrorScreen (uiReload, uiReloadIfFileChanged, uiToggleBalanceAssertions) |
| 37 |
import Hledger.UI.Editor (runIadd, runEditor, endPosition) |
| 38 |
import Brick.Widgets.Edit (getEditContents, handleEditorEvent) |
| 39 |
import Control.Arrow ((>>>)) |
| 40 |
|
| 41 |
|
| 42 |
msDraw :: UIState -> [Widget Name] |
| 43 |
msDraw UIState{aopts=_uopts@UIOpts{uoCliOpts=copts@CliOpts{reportspec_=_rspec@ReportSpec{_rsReportOpts=ropts}}} |
| 44 |
,ajournal=j |
| 45 |
,aScreen=MS sst |
| 46 |
,aMode=mode |
| 47 |
} = dbgui "msDraw" $ |
| 48 |
case mode of |
| 49 |
Help -> [helpDialog, maincontent] |
| 50 |
_ -> [maincontent] |
| 51 |
where |
| 52 |
maincontent = Widget Greedy Greedy $ do |
| 53 |
render $ defaultLayout toplabel bottomlabel $ renderList msDrawItem True (sst ^. mssList) |
| 54 |
where |
| 55 |
toplabel = |
| 56 |
withAttr (attrName "border" <> attrName "filename") files |
| 57 |
<+> borderPeriodStr "" (period_ ropts) |
| 58 |
<+> (if ignore_assertions_ . balancingopts_ $ inputopts_ copts |
| 59 |
then withAttr (attrName "border" <> attrName "query") (str " ignoring balance assertions") |
| 60 |
else str "") |
| 61 |
where |
| 62 |
files = case journalFilePaths j of |
| 63 |
[] -> str "" |
| 64 |
f:_ -> str $ takeFileName f |
| 65 |
-- [f,_:[]] -> (withAttr ("border" <> "bold") $ str $ takeFileName f) <+> str " (& 1 included file)" |
| 66 |
-- f:fs -> (withAttr ("border" <> "bold") $ str $ takeFileName f) <+> str (" (& " ++ show (length fs) ++ " included files)") |
| 67 |
|
| 68 |
bottomlabel = case mode of |
| 69 |
Minibuffer label ed -> minibuffer label ed |
| 70 |
_ -> quickhelp |
| 71 |
where |
| 72 |
quickhelp = borderKeysStr' [ |
| 73 |
("DOWN/UP", str "select") |
| 74 |
,("RIGHT", str "enter screen") |
| 75 |
-- ,("t", renderToggle (tree_ ropts) "list" "tree") |
| 76 |
-- ,("t", str "tree") |
| 77 |
-- ,("l", str "list") |
| 78 |
-- ,("-+", str "depth") |
| 79 |
-- ,("H", renderToggle (not ishistorical) "end-bals" "changes") |
| 80 |
-- ,("F", renderToggle1 (isJust . forecast_ $ inputopts_ copts) "forecast") |
| 81 |
--,("/", "filter") |
| 82 |
--,("DEL", "unfilter") |
| 83 |
--,("ESC", "cancel/top") |
| 84 |
,("a", str "add txn") |
| 85 |
-- ,("g", "reload") |
| 86 |
,("?", str "help") |
| 87 |
,("q", str "quit") |
| 88 |
] |
| 89 |
|
| 90 |
msDraw _ = dbgui "msDraw" $ errorWrongScreenType "msDraw" -- PARTIAL: |
| 91 |
|
| 92 |
-- msDrawItem :: (Int,Int) -> Bool -> MenuScreenItem -> Widget Name |
| 93 |
-- msDrawItem (_acctwidth, _balwidth) _selected MenuScreenItem{..} = |
| 94 |
msDrawItem :: Bool -> MenuScreenItem -> Widget Name |
| 95 |
msDrawItem _selected MenuScreenItem{..} = |
| 96 |
Widget Greedy Fixed $ do |
| 97 |
render $ txt msItemScreenName |
| 98 |
|
| 99 |
-- XXX clean up like asHandle |
| 100 |
msHandle :: BrickEvent Name AppEvent -> EventM Name UIState () |
| 101 |
msHandle ev = do |
| 102 |
ui0 <- get' |
| 103 |
dbguiEv "msHandle" |
| 104 |
case ui0 of |
| 105 |
ui@UIState{ |
| 106 |
aopts=UIOpts{uoCliOpts=copts} |
| 107 |
,ajournal=j |
| 108 |
,aMode=mode |
| 109 |
,aScreen=MS sst |
| 110 |
} -> do |
| 111 |
let |
| 112 |
-- save the currently selected account, in case we leave this screen and lose the selection |
| 113 |
mselscr = case listSelectedElement $ _mssList sst of |
| 114 |
Just (_, MenuScreenItem{..}) -> Just msItemScreen |
| 115 |
Nothing -> Nothing |
| 116 |
nonblanks = V.takeWhile (not . T.null . msItemScreenName) $ listElements $ _mssList sst |
| 117 |
lastnonblankidx = max 0 (length nonblanks - 1) |
| 118 |
journalspan = journalDateSpan False j |
| 119 |
d <- liftIO getCurrentDay |
| 120 |
|
| 121 |
case mode of |
| 122 |
Minibuffer _ ed -> |
| 123 |
case ev of |
| 124 |
VtyEvent (EvKey KEsc []) -> put' $ closeMinibuffer ui |
| 125 |
VtyEvent (EvKey KEnter []) -> put' $ regenerateScreens j d $ |
| 126 |
case setFilter s $ closeMinibuffer ui of |
| 127 |
Left bad -> showMinibuffer "Cannot compile regular expression" (Just bad) ui |
| 128 |
Right ui' -> ui' |
| 129 |
where s = chomp $ unlines $ map strip $ getEditContents ed |
| 130 |
VtyEvent (EvKey (KChar 'l') [MCtrl]) -> redraw |
| 131 |
VtyEvent (EvKey (KChar 'z') [MCtrl]) -> suspend ui |
| 132 |
VtyEvent e -> do |
| 133 |
ed' <- nestEventM' ed $ handleEditorEvent (VtyEvent e) |
| 134 |
put' ui{aMode=Minibuffer "filter" ed'} |
| 135 |
AppEvent _ -> return () |
| 136 |
MouseDown{} -> return () |
| 137 |
MouseUp{} -> return () |
| 138 |
|
| 139 |
Help -> |
| 140 |
case ev of |
| 141 |
-- VtyEvent (EvKey (KChar 'q') []) -> halt |
| 142 |
VtyEvent (EvKey (KChar 'l') [MCtrl]) -> redraw |
| 143 |
VtyEvent (EvKey (KChar 'z') [MCtrl]) -> suspend ui |
| 144 |
_ -> helpHandle ev |
| 145 |
|
| 146 |
Normal -> |
| 147 |
case ev of |
| 148 |
VtyEvent (EvKey (KChar 'q') []) -> halt |
| 149 |
-- EvKey (KChar 'l') [MCtrl] -> do |
| 150 |
VtyEvent (EvKey KEsc []) -> put' $ resetScreens d ui |
| 151 |
VtyEvent (EvKey (KChar c) []) | c == '?' -> put' $ setMode Help ui |
| 152 |
-- XXX AppEvents currently handled only in Normal mode |
| 153 |
-- XXX be sure we don't leave unconsumed events piling up |
| 154 |
AppEvent (DateChange old _) | isStandardPeriod p && p `periodContainsDate` old -> |
| 155 |
put' $ regenerateScreens j d $ setReportPeriod (DayPeriod d) ui |
| 156 |
where |
| 157 |
p = reportPeriod ui |
| 158 |
e | e `elem` [VtyEvent (EvKey (KChar 'g') []), AppEvent FileChange] -> uiReload copts d ui >>= put' |
| 159 |
VtyEvent (EvKey (KChar 'I') []) -> uiToggleBalanceAssertions d ui |
| 160 |
VtyEvent (EvKey (KChar 'a') []) -> suspendAndResume $ clearScreen >> setCursorPosition 0 0 >> add (cliOptsDropArgs copts) j >> uiReloadIfFileChanged copts d j ui |
| 161 |
VtyEvent (EvKey (KChar 'A') []) -> suspendAndResume $ void (runIadd (journalFilePath j)) >> uiReloadIfFileChanged copts d j ui |
| 162 |
VtyEvent (EvKey (KChar 'E') []) -> suspendAndResume $ void (runEditor endPosition (journalFilePath j)) >> uiReloadIfFileChanged copts d j ui |
| 163 |
|
| 164 |
-- VtyEvent (EvKey (KChar 'B') []) -> put' $ regenerateScreens j d $ toggleConversionOp ui |
| 165 |
-- VtyEvent (EvKey (KChar 'V') []) -> put' $ regenerateScreens j d $ toggleValue ui |
| 166 |
-- VtyEvent (EvKey (KChar '0') []) -> put' $ regenerateScreens j d $ setDepth (Just 0) ui |
| 167 |
-- VtyEvent (EvKey (KChar '1') []) -> put' $ regenerateScreens j d $ setDepth (Just 1) ui |
| 168 |
-- VtyEvent (EvKey (KChar '2') []) -> put' $ regenerateScreens j d $ setDepth (Just 2) ui |
| 169 |
-- VtyEvent (EvKey (KChar '3') []) -> put' $ regenerateScreens j d $ setDepth (Just 3) ui |
| 170 |
-- VtyEvent (EvKey (KChar '4') []) -> put' $ regenerateScreens j d $ setDepth (Just 4) ui |
| 171 |
-- VtyEvent (EvKey (KChar '5') []) -> put' $ regenerateScreens j d $ setDepth (Just 5) ui |
| 172 |
-- VtyEvent (EvKey (KChar '6') []) -> put' $ regenerateScreens j d $ setDepth (Just 6) ui |
| 173 |
-- VtyEvent (EvKey (KChar '7') []) -> put' $ regenerateScreens j d $ setDepth (Just 7) ui |
| 174 |
-- VtyEvent (EvKey (KChar '8') []) -> put' $ regenerateScreens j d $ setDepth (Just 8) ui |
| 175 |
-- VtyEvent (EvKey (KChar '9') []) -> put' $ regenerateScreens j d $ setDepth (Just 9) ui |
| 176 |
-- VtyEvent (EvKey (KChar '-') []) -> put' $ regenerateScreens j d $ decDepth ui |
| 177 |
-- VtyEvent (EvKey (KChar '_') []) -> put' $ regenerateScreens j d $ decDepth ui |
| 178 |
-- VtyEvent (EvKey (KChar c) []) | c `elem` ['+','='] -> put' $ regenerateScreens j d $ incDepth ui |
| 179 |
-- VtyEvent (EvKey (KChar 'T') []) -> put' $ regenerateScreens j d $ setReportPeriod (DayPeriod d) ui |
| 180 |
|
| 181 |
-- -- display mode/query toggles |
| 182 |
-- VtyEvent (EvKey (KChar 't') []) -> modify' (regenerateScreens j d . toggleTree) >> msCenterAndContinue |
| 183 |
-- VtyEvent (EvKey (KChar c) []) | c `elem` ['z','Z'] -> modify' (regenerateScreens j d . toggleEmpty) >> msCenterAndContinue |
| 184 |
-- VtyEvent (EvKey (KChar 'R') []) -> modify' (regenerateScreens j d . toggleReal) >> msCenterAndContinue |
| 185 |
-- VtyEvent (EvKey (KChar 'U') []) -> modify' (regenerateScreens j d . toggleUnmarked) >> msCenterAndContinue |
| 186 |
-- VtyEvent (EvKey (KChar 'P') []) -> modify' (regenerateScreens j d . togglePending) >> msCenterAndContinue |
| 187 |
-- VtyEvent (EvKey (KChar 'C') []) -> modify' (regenerateScreens j d . toggleCleared) >> msCenterAndContinue |
| 188 |
-- VtyEvent (EvKey (KChar 'F') []) -> modify' (regenerateScreens j d . toggleForecast d) |
| 189 |
-- VtyEvent (EvKey (KChar 'H') []) -> modify' (toggleHistorical >>> regenerateScreens j d) |
| 190 |
|
| 191 |
-- narrow/widen/move the period as on other screens, for consistency |
| 192 |
VtyEvent (EvKey (KDown) [MShift]) -> modify' (shrinkReportPeriod d >>> regenerateScreens j d) |
| 193 |
VtyEvent (EvKey (KUp) [MShift]) -> modify' (growReportPeriod d >>> regenerateScreens j d) |
| 194 |
VtyEvent (EvKey (KRight) [MShift]) -> modify' (nextReportPeriod journalspan >>> regenerateScreens j d) |
| 195 |
VtyEvent (EvKey (KLeft) [MShift]) -> modify' (previousReportPeriod journalspan >>> regenerateScreens j d) |
| 196 |
VtyEvent (EvKey (KChar 'T') []) -> modify' (setReportPeriod (DayPeriod d) >>> regenerateScreens j d) |
| 197 |
|
| 198 |
VtyEvent (EvKey (KChar '/') []) -> put' $ regenerateScreens j d $ showMinibuffer "filter" Nothing ui |
| 199 |
VtyEvent (EvKey k []) | k `elem` [KBS, KDel] -> (put' $ regenerateScreens j d $ resetFilter ui) |
| 200 |
|
| 201 |
VtyEvent (EvKey (KChar 'l') [MCtrl]) -> scrollSelectionToMiddle (_mssList sst) >> redraw |
| 202 |
VtyEvent (EvKey (KChar 'z') [MCtrl]) -> suspend ui |
| 203 |
|
| 204 |
-- RIGHT enters selected screen if there is one |
| 205 |
VtyEvent e | e `elem` moveRightEvents |
| 206 |
, not $ isBlankElement $ listSelectedElement (_mssList sst) -> |
| 207 |
msEnterScreen d (fromMaybe Accounts mselscr) ui |
| 208 |
|
| 209 |
-- MouseDown is sometimes duplicated, https://github.com/jtdaugherty/brick/issues/347 |
| 210 |
-- just use it to move the selection |
| 211 |
MouseDown _n BLeft _mods Location{loc=(_x,y)} | not $ (=="") clickedname -> do |
| 212 |
put' ui{aScreen=MS sst} -- XXX does this do anything ? |
| 213 |
where |
| 214 |
item = listElements (_mssList sst) !? y |
| 215 |
clickedname = maybe "" msItemScreenName item |
| 216 |
-- mclickedscr = msItemScreen <$> item |
| 217 |
-- and on MouseUp, enter the subscreen |
| 218 |
MouseUp _n (Just BLeft) Location{loc=(_x,y)} -> -- | not $ (=="") clickedname -> |
| 219 |
case mclickedscr of |
| 220 |
Just scr -> msEnterScreen d scr ui |
| 221 |
Nothing -> return () |
| 222 |
where |
| 223 |
item = listElements (_mssList sst) !? y |
| 224 |
-- clickedname = maybe "" msItemScreenName item |
| 225 |
mclickedscr = msItemScreen <$> item |
| 226 |
|
| 227 |
-- when selection is at the last item, DOWN scrolls instead of moving, until maximally scrolled |
| 228 |
VtyEvent e | e `elem` moveDownEvents, isBlankElement mnextelement -> do |
| 229 |
vScrollBy (viewportScroll $ (_mssList sst)^.listNameL) 1 |
| 230 |
where mnextelement = listSelectedElement $ listMoveDown (_mssList sst) |
| 231 |
|
| 232 |
-- mouse scroll wheel scrolls the viewport up or down to its maximum extent, |
| 233 |
-- pushing the selection when necessary. |
| 234 |
MouseDown name btn _mods _loc | btn `elem` [BScrollUp, BScrollDown] -> do |
| 235 |
let scrollamt = if btn==BScrollUp then -1 else 1 |
| 236 |
list' <- nestEventM' (_mssList sst) $ listScrollPushingSelection name (msListSize (_mssList sst)) scrollamt |
| 237 |
put' ui{aScreen=MS sst{_mssList=list'}} |
| 238 |
|
| 239 |
-- if page down or end leads to a blank padding item, stop at last non-blank |
| 240 |
VtyEvent e@(EvKey k []) | k `elem` [KPageDown, KEnd] -> do |
| 241 |
l <- nestEventM' (_mssList sst) $ handleListEvent e |
| 242 |
if isBlankElement $ listSelectedElement l |
| 243 |
then do |
| 244 |
let l' = listMoveTo lastnonblankidx l |
| 245 |
scrollSelectionToMiddle l' |
| 246 |
put' ui{aScreen=MS sst{_mssList=l'}} |
| 247 |
else |
| 248 |
put' ui{aScreen=MS sst{_mssList=l}} |
| 249 |
|
| 250 |
-- fall through to the list's event handler (handles up/down) |
| 251 |
VtyEvent e -> do |
| 252 |
list' <- nestEventM' (_mssList sst) $ handleListEvent (normaliseMovementKeys e) |
| 253 |
put' ui{aScreen=MS $ sst & mssList .~ list'} |
| 254 |
|
| 255 |
MouseDown{} -> return () |
| 256 |
MouseUp{} -> return () |
| 257 |
AppEvent _ -> return () |
| 258 |
|
| 259 |
_ -> dbguiEv "msHandle" >> errorWrongScreenType "msHandle" |
| 260 |
|
| 261 |
msEnterScreen :: Day -> ScreenName -> UIState -> EventM Name UIState () |
| 262 |
msEnterScreen d scrname ui@UIState{ajournal=j, aopts=uopts} = do |
| 263 |
dbguiEv "msEnterScreen" |
| 264 |
let |
| 265 |
scr = case scrname of |
| 266 |
Accounts -> asNew uopts d j Nothing |
| 267 |
CashScreen -> csNew uopts d j Nothing |
| 268 |
Balancesheet -> bsNew uopts d j Nothing |
| 269 |
Incomestatement -> isNew uopts d j Nothing |
| 270 |
put' $ pushScreen scr ui |
| 271 |
|
| 272 |
-- | Set the selected list item on the menu screen. Has no effect on other screens. |
| 273 |
msSetSelectedScreen :: Int -> Screen -> Screen |
| 274 |
msSetSelectedScreen selidx (MS mss@MSS{_mssList=l}) = MS mss{_mssList=listMoveTo selidx l} |
| 275 |
msSetSelectedScreen _ s = s |
| 276 |
|
| 277 |
isBlankElement mel = ((msItemScreenName . snd) <$> mel) == Just "" |
| 278 |
|
| 279 |
msListSize = V.length . V.takeWhile ((/="").msItemScreenName) . listElements |
| 280 |
|