From 3c4cb4eeeb14e3fa0d585554283b057db7cc517a Mon Sep 17 00:00:00 2001 From: Simon Michael Date: Wed, 7 Dec 2016 15:07:59 -0800 Subject: [PATCH] ui: --watch: track date only when a standard period is in effect --- hledger-lib/Hledger/Data/Period.hs | 15 +++++++++++++-- hledger-ui/Hledger/UI/AccountsScreen.hs | 4 +++- hledger-ui/Hledger/UI/RegisterScreen.hs | 4 +++- hledger-ui/Hledger/UI/TransactionScreen.hs | 4 +++- 4 files changed, 22 insertions(+), 5 deletions(-) diff --git a/hledger-lib/Hledger/Data/Period.hs b/hledger-lib/Hledger/Data/Period.hs index ea07bb0da..fe6e0dcf5 100644 --- a/hledger-lib/Hledger/Data/Period.hs +++ b/hledger-lib/Hledger/Data/Period.hs @@ -118,6 +118,17 @@ isLastDayOfMonth y m d = 12 -> d==31 _ -> False +-- | Is this period a "standard" period, referencing a particular day, week, month, quarter, or year ? +-- Periods of other durations, or infinite duration, or not starting on a standard period boundary, are not. +isStandardPeriod = isStandardPeriod' . simplifyPeriod + where + isStandardPeriod' (DayPeriod _) = True + isStandardPeriod' (WeekPeriod _) = True + isStandardPeriod' (MonthPeriod _ _) = True + isStandardPeriod' (QuarterPeriod _ _) = True + isStandardPeriod' (YearPeriod _) = True + isStandardPeriod' _ = False + -- | Render a period as a compact display string suitable for user output. -- -- >>> showPeriod (WeekPeriod (fromGregorian 2016 7 25)) @@ -143,7 +154,7 @@ periodEnd p = me where DateSpan _ me = periodAsDateSpan p --- | Move a standard period (day, week, month etc.) to the following period of same duration. +-- | Move a standard period to the following period of same duration. -- Non-standard periods are unaffected. periodNext :: Period -> Period periodNext (DayPeriod b) = DayPeriod (addDays 1 b) @@ -155,7 +166,7 @@ periodNext (QuarterPeriod y q) = QuarterPeriod y (q+1) periodNext (YearPeriod y) = YearPeriod (y+1) periodNext p = p --- | Move a standard period (day, week, month etc.) to the preceding period of same duration. +-- | Move a standard period to the preceding period of same duration. -- Non-standard periods are unaffected. periodPrevious :: Period -> Period periodPrevious (DayPeriod b) = DayPeriod (addDays (-1) b) diff --git a/hledger-ui/Hledger/UI/AccountsScreen.hs b/hledger-ui/Hledger/UI/AccountsScreen.hs index 7a51e0783..e0f670988 100644 --- a/hledger-ui/Hledger/UI/AccountsScreen.hs +++ b/hledger-ui/Hledger/UI/AccountsScreen.hs @@ -291,8 +291,10 @@ asHandle ui0@UIState{ VtyEvent (EvKey (KChar c) []) | c `elem` ['?'] -> continue $ setMode Help ui -- XXX AppEvents currently handled only in Normal mode -- XXX be sure we don't leave unconsumed events piling up - AppEvent (DateChange old _) | periodContainsDate (reportPeriod ui) old -> + AppEvent (DateChange old _) | isStandardPeriod p && p `periodContainsDate` old -> continue $ regenerateScreens j d $ setReportPeriod (DayPeriod d) ui + where + p = reportPeriod 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) diff --git a/hledger-ui/Hledger/UI/RegisterScreen.hs b/hledger-ui/Hledger/UI/RegisterScreen.hs index 542d8ae85..a09ff7855 100644 --- a/hledger-ui/Hledger/UI/RegisterScreen.hs +++ b/hledger-ui/Hledger/UI/RegisterScreen.hs @@ -267,8 +267,10 @@ 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 old _) | periodContainsDate (reportPeriod ui) old -> + AppEvent (DateChange old _) | isStandardPeriod p && p `periodContainsDate` old -> continue $ regenerateScreens j d $ setReportPeriod (DayPeriod d) ui + where + p = reportPeriod 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) diff --git a/hledger-ui/Hledger/UI/TransactionScreen.hs b/hledger-ui/Hledger/UI/TransactionScreen.hs index 301fa6d99..043ac5e49 100644 --- a/hledger-ui/Hledger/UI/TransactionScreen.hs +++ b/hledger-ui/Hledger/UI/TransactionScreen.hs @@ -129,8 +129,10 @@ 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 old _) | periodContainsDate (reportPeriod ui) old -> + AppEvent (DateChange old _) | isStandardPeriod p && p `periodContainsDate` old -> continue $ regenerateScreens j d $ setReportPeriod (DayPeriod d) ui + where + p = reportPeriod ui e | e `elem` [VtyEvent (EvKey (KChar 'g') []), AppEvent FileChange] -> do d <- liftIO getCurrentDay ej <- liftIO $ journalReload copts