ui: I toggles balance assertions

This was a bit tricky.
This commit is contained in:
Simon Michael 2016-07-06 15:08:57 -07:00
parent 19fc5b61f7
commit 7eeb5e01ee
8 changed files with 31 additions and 2 deletions

View File

@ -53,6 +53,7 @@ module Hledger.Data.Journal (
canonicalStyleFrom, canonicalStyleFrom,
matchpats, matchpats,
nulljournal, nulljournal,
journalCheckBalanceAssertions,
-- * Tests -- * Tests
samplejournal, samplejournal,
tests_Hledger_Data_Journal, tests_Hledger_Data_Journal,

View File

@ -271,6 +271,7 @@ asHandle ui0@UIState{
EvKey KEsc [] -> continue $ resetScreens d ui EvKey KEsc [] -> continue $ resetScreens d ui
EvKey (KChar c) [] | c `elem` ['?'] -> continue $ setMode Help ui EvKey (KChar c) [] | c `elem` ['?'] -> continue $ setMode Help ui
EvKey (KChar 'g') [] -> liftIO (uiReloadJournalIfChanged copts d j ui) >>= continue EvKey (KChar 'g') [] -> liftIO (uiReloadJournalIfChanged copts d j ui) >>= continue
EvKey (KChar 'I') [] -> continue $ uiCheckBalanceAssertions d (toggleIgnoreBalanceAssertions ui)
EvKey (KChar 'a') [] -> suspendAndResume $ clearScreen >> setCursorPosition 0 0 >> add copts j >> uiReloadJournalIfChanged copts d j ui EvKey (KChar 'a') [] -> suspendAndResume $ clearScreen >> setCursorPosition 0 0 >> add copts j >> uiReloadJournalIfChanged copts d j ui
EvKey (KChar 'E') [] -> suspendAndResume $ void (runEditor endPos (journalFilePath j)) >> uiReloadJournalIfChanged copts d j ui EvKey (KChar 'E') [] -> suspendAndResume $ void (runEditor endPos (journalFilePath j)) >> uiReloadJournalIfChanged copts d j ui
EvKey (KChar '0') [] -> continue $ regenerateScreens j d $ setDepth (Just 0) ui EvKey (KChar '0') [] -> continue $ regenerateScreens j d $ setDepth (Just 0) ui

View File

@ -4,6 +4,8 @@
module Hledger.UI.ErrorScreen module Hledger.UI.ErrorScreen
(errorScreen (errorScreen
,uiCheckBalanceAssertions
,uiReloadJournal
,uiReloadJournalIfChanged ,uiReloadJournalIfChanged
) )
where where
@ -82,18 +84,19 @@ esHandle ui@UIState{
d <- liftIO getCurrentDay d <- liftIO getCurrentDay
case ev of case ev of
EvKey (KChar 'q') [] -> halt ui EvKey (KChar 'q') [] -> halt ui
EvKey KEsc [] -> continue $ resetScreens d ui EvKey KEsc [] -> continue $ uiCheckBalanceAssertions d $ resetScreens d ui
EvKey (KChar c) [] | c `elem` ['h','?'] -> continue $ setMode Help ui EvKey (KChar c) [] | c `elem` ['h','?'] -> continue $ setMode Help ui
EvKey (KChar 'E') [] -> suspendAndResume $ void (runEditor pos f) >> uiReloadJournalIfChanged copts d j (popScreen ui) EvKey (KChar 'E') [] -> suspendAndResume $ void (runEditor pos f) >> uiReloadJournalIfChanged copts d j (popScreen ui)
where where
(pos,f) = case parsewith hledgerparseerrorpositionp esError of (pos,f) = case parsewith hledgerparseerrorpositionp esError of
Right (f,l,c) -> (Just (l, Just c),f) Right (f,l,c) -> (Just (l, Just c),f)
Left _ -> (endPos, journalFilePath j) Left _ -> (endPos, journalFilePath j)
EvKey (KChar 'g') [] -> liftIO (uiReloadJournalIfChanged copts d j (popScreen ui)) >>= continue EvKey (KChar 'g') [] -> liftIO (uiReloadJournalIfChanged copts d j (popScreen ui)) >>= continue . uiCheckBalanceAssertions d
-- (ej, _) <- liftIO $ journalReloadIfChanged copts d j -- (ej, _) <- liftIO $ journalReloadIfChanged copts d j
-- case ej of -- case ej of
-- Left err -> continue ui{aScreen=s{esError=err}} -- show latest parse error -- Left err -> continue ui{aScreen=s{esError=err}} -- show latest parse error
-- Right j' -> continue $ regenerateScreens j' d $ popScreen ui -- return to previous screen, and reload it -- Right j' -> continue $ regenerateScreens j' d $ popScreen ui -- return to previous screen, and reload it
EvKey (KChar 'I') [] -> continue $ uiCheckBalanceAssertions d (popScreen $ toggleIgnoreBalanceAssertions ui)
_ -> continue ui _ -> continue ui
esHandle _ _ = error "event handler called with wrong screen type, should not happen" esHandle _ _ = error "event handler called with wrong screen type, should not happen"
@ -140,3 +143,16 @@ uiReloadJournalIfChanged copts d j ui = do
UIState{aScreen=s@ErrorScreen{}} -> ui{aScreen=s{esError=err}} UIState{aScreen=s@ErrorScreen{}} -> ui{aScreen=s{esError=err}}
_ -> screenEnter d errorScreen{esError=err} ui _ -> screenEnter d errorScreen{esError=err} ui
-- Re-check any balance assertions in the current journal, and if any
-- fail, enter (or update) the error screen. Or if balance assertions
-- are disabled, do nothing.
uiCheckBalanceAssertions :: Day -> UIState -> UIState
uiCheckBalanceAssertions d ui@UIState{aopts=UIOpts{cliopts_=copts}, ajournal=j}
| ignore_assertions_ copts = ui
| otherwise =
case journalCheckBalanceAssertions j of
Right _ -> ui
Left err ->
case ui of
UIState{aScreen=s@ErrorScreen{}} -> ui{aScreen=s{esError=err}}
_ -> screenEnter d errorScreen{esError=err} ui

View File

@ -250,6 +250,7 @@ rsHandle ui@UIState{
EvKey KEsc [] -> continue $ resetScreens d ui EvKey KEsc [] -> continue $ resetScreens d ui
EvKey (KChar c) [] | c `elem` ['?'] -> continue $ setMode Help ui EvKey (KChar c) [] | c `elem` ['?'] -> continue $ setMode Help ui
EvKey (KChar 'g') [] -> liftIO (uiReloadJournalIfChanged copts d j ui) >>= continue EvKey (KChar 'g') [] -> liftIO (uiReloadJournalIfChanged copts d j ui) >>= continue
EvKey (KChar 'I') [] -> continue $ uiCheckBalanceAssertions d (toggleIgnoreBalanceAssertions ui)
EvKey (KChar 'a') [] -> suspendAndResume $ clearScreen >> setCursorPosition 0 0 >> add copts j >> uiReloadJournalIfChanged copts d j ui EvKey (KChar 'a') [] -> suspendAndResume $ clearScreen >> setCursorPosition 0 0 >> add copts j >> uiReloadJournalIfChanged copts d j ui
EvKey (KChar 'E') [] -> suspendAndResume $ void (runEditor pos f) >> uiReloadJournalIfChanged copts d j ui EvKey (KChar 'E') [] -> suspendAndResume $ void (runEditor pos f) >> uiReloadJournalIfChanged copts d j ui
where where

View File

@ -157,6 +157,7 @@ tsHandle ui@UIState{aScreen=s@TransactionScreen{tsTransaction=(i,t)
,tsTransactions=numberedts ,tsTransactions=numberedts
,tsAccount=acct}} ,tsAccount=acct}}
continue $ regenerateScreens j' d ui' continue $ regenerateScreens j' d ui'
EvKey (KChar 'I') [] -> continue $ uiCheckBalanceAssertions d (toggleIgnoreBalanceAssertions ui)
-- if allowing toggling here, we should refresh the txn list from the parent register screen -- if allowing toggling here, we should refresh the txn list from the parent register screen
-- EvKey (KChar 'E') [] -> continue $ regenerateScreens j d $ stToggleEmpty ui -- EvKey (KChar 'E') [] -> continue $ regenerateScreens j d $ stToggleEmpty ui
-- EvKey (KChar 'C') [] -> continue $ regenerateScreens j d $ stToggleCleared ui -- EvKey (KChar 'C') [] -> continue $ regenerateScreens j d $ stToggleCleared ui

View File

@ -60,6 +60,11 @@ toggleReal ui@UIState{aopts=uopts@UIOpts{cliopts_=copts@CliOpts{reportopts_=ropt
where where
toggleReal ropts = ropts{real_=not $ real_ ropts} toggleReal ropts = ropts{real_=not $ real_ ropts}
-- | Toggle the ignoring of balance assertions.
toggleIgnoreBalanceAssertions :: UIState -> UIState
toggleIgnoreBalanceAssertions ui@UIState{aopts=uopts@UIOpts{cliopts_=copts@CliOpts{}}} =
ui{aopts=uopts{cliopts_=copts{ignore_assertions_=not $ ignore_assertions_ copts}}}
-- | Apply a new filter query. -- | Apply a new filter query.
setFilter :: String -> UIState -> UIState setFilter :: String -> UIState -> UIState
setFilter s ui@UIState{aopts=uopts@UIOpts{cliopts_=copts@CliOpts{reportopts_=ropts}}} = setFilter s ui@UIState{aopts=uopts@UIOpts{cliopts_=copts@CliOpts{reportopts_=ropts}}} =

View File

@ -52,6 +52,7 @@ helpDialog =
,renderKey ("a", "add transaction") ,renderKey ("a", "add transaction")
,renderKey ("E", "open editor") ,renderKey ("E", "open editor")
,renderKey ("g", "reload data") ,renderKey ("g", "reload data")
,renderKey ("I", "toggle balance assertions")
,renderKey ("q", "quit") ,renderKey ("q", "quit")
] ]
,padLeftRight 1 $ ,padLeftRight 1 $

View File

@ -103,6 +103,9 @@ Or, it cancels a minibuffer edit or help dialog if one is active.
`g` reloads from the data file(s) and updates the current screen and any `g` reloads from the data file(s) and updates the current screen and any
previous screens. (With large files, there can be a noticeable pause.) previous screens. (With large files, there can be a noticeable pause.)
`I` toggles balance assertion checking.
Disabling balance assertions temporarily can be useful for troubleshooting.
`a` runs command-line hledger's add command, and reloads the updated file. `a` runs command-line hledger's add command, and reloads the updated file.
This allows some basic data entry. This allows some basic data entry.