| 1 |
-- The error screen, showing a current error condition (such as a parse error after reloading the journal) |
| 2 |
|
| 3 |
{-# LANGUAGE FlexibleContexts #-} |
| 4 |
{-# LANGUAGE OverloadedStrings #-} |
| 5 |
{-# LANGUAGE RecordWildCards #-} |
| 6 |
{-# OPTIONS_GHC -Wno-incomplete-record-updates #-} |
| 7 |
|
| 8 |
module Hledger.UI.ErrorScreen |
| 9 |
(esNew |
| 10 |
,esUpdate |
| 11 |
,esDraw |
| 12 |
,esHandle |
| 13 |
,uiCheckBalanceAssertions |
| 14 |
,uiReload |
| 15 |
,uiReloadIfFileChanged |
| 16 |
,uiToggleBalanceAssertions |
| 17 |
) |
| 18 |
where |
| 19 |
|
| 20 |
import Brick |
| 21 |
-- import Brick.Widgets.Border ("border") |
| 22 |
import Control.Monad.IO.Class (liftIO) |
| 23 |
import Data.Time.Calendar (Day) |
| 24 |
import Data.Void (Void) |
| 25 |
import Graphics.Vty (Event(..),Key(..),Modifier(..)) |
| 26 |
import Lens.Micro ((^.)) |
| 27 |
import Safe (headMay) |
| 28 |
import Text.Megaparsec |
| 29 |
import Text.Megaparsec.Char |
| 30 |
|
| 31 |
import Hledger.Cli hiding (mode, progname,prognameandversion) |
| 32 |
import Hledger.UI.UIOptions |
| 33 |
import Hledger.UI.UITypes |
| 34 |
import Hledger.UI.UIState |
| 35 |
import Hledger.UI.UIUtils |
| 36 |
import Hledger.UI.UIScreens |
| 37 |
import Hledger.UI.Editor |
| 38 |
|
| 39 |
esDraw :: UIState -> [Widget Name] |
| 40 |
esDraw UIState{aScreen=ES ESS{..} |
| 41 |
,aMode=mode |
| 42 |
} = |
| 43 |
case mode of |
| 44 |
Help -> [helpDialog, maincontent] |
| 45 |
_ -> [maincontent] |
| 46 |
where |
| 47 |
maincontent = Widget Greedy Greedy $ do |
| 48 |
render $ defaultLayout toplabel bottomlabel $ withAttr (attrName "error") $ str $ _essError |
| 49 |
where |
| 50 |
toplabel = |
| 51 |
withAttr (attrName "border" <> attrName "bold") (str "Oops. Please fix this problem then press g to reload") |
| 52 |
-- <+> (if ignore_assertions_ copts then withAttr ("border" <> "query") (str " ignoring") else str " not ignoring") |
| 53 |
|
| 54 |
bottomlabel = quickhelp |
| 55 |
-- case mode of |
| 56 |
-- Minibuffer ed -> minibuffer ed |
| 57 |
-- _ -> quickhelp |
| 58 |
where |
| 59 |
quickhelp = borderKeysStr [ |
| 60 |
("h", "help") |
| 61 |
,("ESC", "cancel/top") |
| 62 |
,("E", "editor") |
| 63 |
,("g", "reload") |
| 64 |
,("q", "quit") |
| 65 |
] |
| 66 |
|
| 67 |
esDraw _ = error' "draw function called with wrong screen type, should not happen" -- PARTIAL: |
| 68 |
|
| 69 |
esHandle :: BrickEvent Name AppEvent -> EventM Name UIState () |
| 70 |
esHandle ev = do |
| 71 |
ui0 <- get' |
| 72 |
case ui0 of |
| 73 |
ui@UIState{aScreen=ES ESS{..} |
| 74 |
,aopts=UIOpts{uoCliOpts=copts} |
| 75 |
,ajournal=j |
| 76 |
,aMode=mode |
| 77 |
} -> |
| 78 |
case mode of |
| 79 |
Help -> |
| 80 |
case ev of |
| 81 |
VtyEvent (EvKey (KChar 'q') []) -> halt |
| 82 |
VtyEvent (EvKey (KChar 'l') [MCtrl]) -> redraw |
| 83 |
VtyEvent (EvKey (KChar 'z') [MCtrl]) -> suspend ui |
| 84 |
_ -> helpHandle ev |
| 85 |
|
| 86 |
_ -> do |
| 87 |
d <- liftIO getCurrentDay |
| 88 |
case ev of |
| 89 |
VtyEvent (EvKey (KChar 'q') []) -> halt |
| 90 |
VtyEvent (EvKey KEsc []) -> put' $ uiCheckBalanceAssertions d $ resetScreens d ui |
| 91 |
VtyEvent (EvKey (KChar c) []) | c `elem` ['h','?'] -> put' $ setMode Help ui |
| 92 |
|
| 93 |
-- g or file change: reload the journal and rebuild app state. |
| 94 |
e | e `elem` [VtyEvent (EvKey (KChar 'g') []), AppEvent FileChange] -> esReload copts d ui |
| 95 |
|
| 96 |
-- E: run editor, reload the journal. |
| 97 |
VtyEvent (EvKey (KChar 'E') []) -> do |
| 98 |
suspendAndResume' $ do |
| 99 |
let |
| 100 |
(pos,f) = case parsewithString hledgerparseerrorpositionp _essError of |
| 101 |
Right (f',l,c) -> (Just (l, Just c),f') |
| 102 |
Left _ -> (endPosition, journalFilePath j) |
| 103 |
runEditor pos f |
| 104 |
esReloadIfFileChanged copts d j ui |
| 105 |
|
| 106 |
VtyEvent (EvKey (KChar 'I') []) -> uiToggleBalanceAssertions d (popScreen ui) |
| 107 |
VtyEvent (EvKey (KChar 'l') [MCtrl]) -> redraw |
| 108 |
VtyEvent (EvKey (KChar 'z') [MCtrl]) -> suspend ui |
| 109 |
_ -> return () |
| 110 |
|
| 111 |
_ -> errorWrongScreenType "esHandle" |
| 112 |
|
| 113 |
where |
| 114 |
-- Reload and fully regenerate the error screen. |
| 115 |
-- XXX On an error screen below the transaction screen, this is tricky because of a current limitation of regenerateScreens. |
| 116 |
-- For now we try to work around by re-entering the transaction screen. |
| 117 |
-- This can show flicker in the UI and it's hard to handle all situations robustly. |
| 118 |
esReload copts d ui = uiReload copts d ui >>= maybeReloadErrorScreen copts d |
| 119 |
esReloadIfFileChanged copts d j ui = liftIO (uiReloadIfFileChanged copts d j ui) >>= maybeReloadErrorScreen copts d |
| 120 |
maybeReloadErrorScreen copts d ui = |
| 121 |
case headMay $ aPrevScreens ui of |
| 122 |
Just (TS _) -> do |
| 123 |
-- check balance assertions, exit to register screen, enter transaction screen, reload once more |
| 124 |
put' $ popScreen $ popScreen $ uiCheckBalanceAssertions d ui |
| 125 |
sendVtyEvents [EvKey KEnter [], EvKey (KChar 'g') []] -- XXX Might be disrupted if other events are queued ? |
| 126 |
_ -> uiReload copts d (popScreen ui) >>= put' . uiCheckBalanceAssertions d |
| 127 |
|
| 128 |
-- | Parse the file name, line and column number from a hledger parse error message, if possible. |
| 129 |
-- Temporary, we should keep the original parse error location. XXX |
| 130 |
-- Keep in sync with 'Hledger.Data.Transaction.showGenericSourcePos' |
| 131 |
hledgerparseerrorpositionp :: ParsecT Void String t (String, Int, Int) |
| 132 |
hledgerparseerrorpositionp = do |
| 133 |
anySingle `manyTill` char '"' |
| 134 |
f <- anySingle `manyTill` (oneOf ['"','\n']) |
| 135 |
choice [ |
| 136 |
do |
| 137 |
string " (line " |
| 138 |
l <- read <$> some digitChar |
| 139 |
string ", column " |
| 140 |
c <- read <$> some digitChar |
| 141 |
return (f, l, c), |
| 142 |
do |
| 143 |
string " (lines " |
| 144 |
l <- read <$> some digitChar |
| 145 |
char '-' |
| 146 |
some digitChar |
| 147 |
char ')' |
| 148 |
return (f, l, 1) |
| 149 |
] |
| 150 |
|
| 151 |
|
| 152 |
-- Defined here so it can reference the error screen: |
| 153 |
|
| 154 |
-- | Modify some input options for hledger-ui (enable --forecast). |
| 155 |
uiAdjustOpts :: UIOpts -> CliOpts -> CliOpts |
| 156 |
uiAdjustOpts uopts = enableForecast uopts |
| 157 |
|
| 158 |
-- | Reload the journal from its input files, then update the ui app state accordingly. |
| 159 |
-- This means regenerate the entire screen stack from top level down to the current screen, using the provided today-date. |
| 160 |
-- As a convenience (usually), if journal reloading fails, this enters the error screen, or if already there, updates its message. |
| 161 |
-- |
| 162 |
-- The provided cli options can influence reloading; then if reloading succeeds they are saved in the ui state, |
| 163 |
-- otherwise the UIState keeps its old options. (XXX needed for.. ?) |
| 164 |
-- |
| 165 |
-- Like at hledger-ui startup, --forecast is always enabled. |
| 166 |
-- A forecast period specified in the provided opts, or at startup, is preserved. |
| 167 |
-- |
| 168 |
uiReload :: CliOpts -> Day -> UIState -> EventM Name UIState UIState |
| 169 |
uiReload copts d ui = liftIO $ do |
| 170 |
ej <- |
| 171 |
let copts1 = uiAdjustOpts (astartupopts ui) copts |
| 172 |
in runExceptT $ journalTransform copts1 <$> journalReload copts1 |
| 173 |
-- dbg1IO "uiReload before reload" (map tdescription $ jtxns $ ajournal ui) |
| 174 |
return $ case ej of |
| 175 |
Right j -> |
| 176 |
-- dbg1 "uiReload after reload" (map tdescription $ jtxns j) $ |
| 177 |
regenerateScreens j d ui |
| 178 |
Left err -> |
| 179 |
case ui of |
| 180 |
UIState{aScreen=ES _} -> ui{aScreen=esNew err} |
| 181 |
_ -> pushScreen (esNew err) ui |
| 182 |
-- XXX GHC 9.2 warning: |
| 183 |
-- hledger-ui/Hledger/UI/ErrorScreen.hs:164:59: warning: [-Wincomplete-record-updates] |
| 184 |
-- Pattern match(es) are non-exhaustive |
| 185 |
-- In a record-update construct: |
| 186 |
-- Patterns of type ‘Screen’ not matched: |
| 187 |
-- AccountsScreen _ _ _ _ _ |
| 188 |
-- RegisterScreen _ _ _ _ _ _ |
| 189 |
-- TransactionScreen _ _ _ _ _ _ |
| 190 |
|
| 191 |
-- | Like uiReload, except it skips re-reading the journal if its file(s) have not changed |
| 192 |
-- since it was last loaded. The up app state is always updated, since the options or today-date may have changed. |
| 193 |
-- Also, this one runs in IO, suitable for suspendAndResume. |
| 194 |
uiReloadIfFileChanged :: CliOpts -> Day -> Journal -> UIState -> IO UIState |
| 195 |
uiReloadIfFileChanged copts d j ui = do |
| 196 |
ej <- |
| 197 |
let copts1 = uiAdjustOpts (astartupopts ui) copts |
| 198 |
in runExceptT $ journalReloadIfChanged copts1 d j |
| 199 |
return $ case ej of |
| 200 |
Right (j', _) -> regenerateScreens j' d ui |
| 201 |
Left err -> case aScreen ui of |
| 202 |
ES _ -> ui{aScreen=esNew err} |
| 203 |
_ -> pushScreen (esNew err) ui |
| 204 |
|
| 205 |
-- Re-check any balance assertions in the current journal, |
| 206 |
-- and if any fail, enter (or update) the error screen. |
| 207 |
-- Or if balance assertions are disabled or pivot is active, do nothing. |
| 208 |
-- (When pivot is active, assertions have already been checked on the pre-pivot journal, |
| 209 |
-- and the current post-pivot journal's account names don't match the original assertions.) |
| 210 |
uiCheckBalanceAssertions :: Day -> UIState -> UIState |
| 211 |
uiCheckBalanceAssertions _d ui@UIState{ajournal=j, aopts=UIOpts{uoCliOpts=CliOpts{inputopts_=InputOpts{pivot_=pval}}}} |
| 212 |
| ui^.ignore_assertions = ui -- user disabled checks |
| 213 |
| not (null pval) = ui -- post-pivot journal, assertions already checked pre-pivot |
| 214 |
| otherwise = |
| 215 |
case journalCheckBalanceAssertions j of |
| 216 |
Right () -> ui |
| 217 |
Left err -> |
| 218 |
case ui of |
| 219 |
UIState{aScreen=ES sst} -> ui{aScreen=ES sst{_essError=err}} |
| 220 |
_ -> pushScreen (esNew err) ui |
| 221 |
|
| 222 |
-- | Toggle ignoring balance assertions (when user presses I), and if no longer ignoring, recheck them. |
| 223 |
-- Normally the recheck is done quickly on the in-memory journal. |
| 224 |
-- But if --pivot is active, a full journal reload is done instead |
| 225 |
-- (because we can't check balance assertions after pivoting has occurred). |
| 226 |
-- In that case, this operation could be slower and could reveal other data changes (not just balance assertion failures). |
| 227 |
uiToggleBalanceAssertions :: Day -> UIState -> EventM Name UIState () |
| 228 |
uiToggleBalanceAssertions d ui@UIState{aopts=UIOpts{uoCliOpts=copts@CliOpts{inputopts_=InputOpts{pivot_=pivotval}}}} = |
| 229 |
let ui' = toggleIgnoreBalanceAssertions ui |
| 230 |
in case (ui'^.ignore_assertions, null pivotval) of |
| 231 |
(True, _) -> put' ui' -- ignoring enabled, no check needed |
| 232 |
(False, True) -> put' $ uiCheckBalanceAssertions d ui' -- unpivoted journal, can check in memory |
| 233 |
(False, False) -> uiReload copts d ui' >>= put' -- pivoted journal, must reload to check it |