ui: --watch date tracking fixes

- move the period only if it's "current" (contains the old "today" date)
- make it work on all screens, not just accounts
This commit is contained in:
Simon Michael 2016-12-02 15:36:23 -08:00
parent e3a7f6697e
commit 9952f93e97
8 changed files with 27 additions and 13 deletions

View File

@ -34,6 +34,7 @@ module Hledger.Data.Dates (
getCurrentYear,
nulldate,
spanContainsDate,
periodContainsDate,
parsedateM,
parsedate,
showDate,
@ -212,6 +213,11 @@ spanContainsDate (DateSpan Nothing (Just e)) d = d < e
spanContainsDate (DateSpan (Just b) Nothing) d = d >= b
spanContainsDate (DateSpan (Just b) (Just e)) d = d >= b && d < e
-- | Does the period include the given date ?
-- (Here to avoid import cycle).
periodContainsDate :: Period -> Day -> Bool
periodContainsDate p = spanContainsDate (periodAsDateSpan p)
-- | Calculate the intersection of a number of datespans.
spansIntersect [] = nulldatespan
spansIntersect [d] = d

View File

@ -289,9 +289,10 @@ asHandle ui0@UIState{
-- EvKey (KChar 'l') [MCtrl] -> do
VtyEvent (EvKey KEsc []) -> continue $ resetScreens d ui
VtyEvent (EvKey (KChar c) []) | c `elem` ['?'] -> continue $ setMode Help ui
-- XXX handles FileChange/DateChange events only in Normal mode ?
-- XXX AppEvents currently handled only in Normal mode
-- XXX be sure we don't leave unconsumed events piling up
AppEvent DateChange -> continue $ regenerateScreens j d $ setReportPeriod (DayPeriod d) ui
AppEvent (DateChange old _) | periodContainsDate (reportPeriod ui) old ->
continue $ regenerateScreens j d $ setReportPeriod (DayPeriod d) ui
e | e `elem` [VtyEvent (EvKey (KChar 'g') []), AppEvent FileChange] ->
liftIO (uiReloadJournal copts d ui) >>= continue
VtyEvent (EvKey (KChar 'I') []) -> continue $ uiCheckBalanceAssertions d (toggleIgnoreBalanceAssertions ui)

View File

@ -92,7 +92,6 @@ esHandle ui@UIState{
(pos,f) = case parsewithString hledgerparseerrorpositionp esError of
Right (f,l,c) -> (Just (l, Just c),f)
Left _ -> (endPos, journalFilePath j)
-- AppEvent DateChange -> continue $ regenerateScreens j d ui
e | e `elem` [VtyEvent (EvKey (KChar 'g') [])] ->
liftIO (uiReloadJournal copts d (popScreen ui)) >>= continue . uiCheckBalanceAssertions d
-- (ej, _) <- liftIO $ journalReloadIfChanged copts d j

View File

@ -161,13 +161,14 @@ runBrickUi uopts@UIOpts{cliopts_=copts@CliOpts{reportopts_=ropts}} j = do
-- start a background thread reporting changes in the current date
-- use async for proper child termination in GHCI
let
watchDate lastd = do
watchDate old = do
threadDelay 1000000 -- 1s
d <- getCurrentDay
when (d /= lastd) $ do
-- dbg1IO "datechange" DateChange -- XXX don't uncomment until dbg*IO fixed to use traceIO, GHC may block/end thread
writeChan eventChan DateChange
watchDate d
new <- getCurrentDay
when (new /= old) $ do
let dc = DateChange old new
-- dbg1IO "datechange" dc -- XXX don't uncomment until dbg*IO fixed to use traceIO, GHC may block/end thread
writeChan eventChan dc
watchDate new
withAsync
(getCurrentDay >>= watchDate)

View File

@ -267,7 +267,8 @@ rsHandle ui@UIState{
VtyEvent (EvKey (KChar 'q') []) -> halt ui
VtyEvent (EvKey KEsc []) -> continue $ resetScreens d ui
VtyEvent (EvKey (KChar c) []) | c `elem` ['?'] -> continue $ setMode Help ui
AppEvent DateChange -> continue $ regenerateScreens j d ui
AppEvent (DateChange old _) | periodContainsDate (reportPeriod ui) old ->
continue $ regenerateScreens j d $ setReportPeriod (DayPeriod d) ui
e | e `elem` [VtyEvent (EvKey (KChar 'g') []), AppEvent FileChange] ->
liftIO (uiReloadJournal copts d ui) >>= continue
VtyEvent (EvKey (KChar 'I') []) -> continue $ uiCheckBalanceAssertions d (toggleIgnoreBalanceAssertions ui)

View File

@ -129,7 +129,8 @@ tsHandle ui@UIState{aScreen=s@TransactionScreen{tsTransaction=(i,t)
VtyEvent (EvKey (KChar 'E') []) -> suspendAndResume $ void (runEditor pos f) >> uiReloadJournalIfChanged copts d j ui
where
(pos,f) = let GenericSourcePos f l c = tsourcepos t in (Just (l, Just c),f)
AppEvent DateChange -> continue $ regenerateScreens j d ui
AppEvent (DateChange old _) | periodContainsDate (reportPeriod ui) old ->
continue $ regenerateScreens j d $ setReportPeriod (DayPeriod d) ui
e | e `elem` [VtyEvent (EvKey (KChar 'g') []), AppEvent FileChange] -> do
d <- liftIO getCurrentDay
ej <- liftIO $ journalReload copts

View File

@ -104,6 +104,11 @@ moveReportPeriodToDate :: Day -> UIState -> UIState
moveReportPeriodToDate d ui@UIState{aopts=uopts@UIOpts{cliopts_=copts@CliOpts{reportopts_=ropts@ReportOpts{period_=p}}}} =
ui{aopts=uopts{cliopts_=copts{reportopts_=ropts{period_=periodMoveTo d p}}}}
-- | Get the report period.
reportPeriod :: UIState -> Period
reportPeriod UIState{aopts=UIOpts{cliopts_=CliOpts{reportopts_=ReportOpts{period_=p}}}} =
p
-- | Set the report period.
setReportPeriod :: Period -> UIState -> UIState
setReportPeriod p ui@UIState{aopts=uopts@UIOpts{cliopts_=copts@CliOpts{reportopts_=ropts}}} =

View File

@ -83,8 +83,8 @@ data Name =
deriving (Ord, Show, Eq)
data AppEvent =
FileChange
| DateChange
FileChange -- one of the Journal's files has been added/modified/removed
| DateChange Day Day -- the current date has changed since last checked (with the old and new values)
deriving (Eq, Show)
-- | hledger-ui screen types & instances.