diff options
author | Andrew Cady <d@cryptonomic.net> | 2022-09-14 16:51:56 -0400 |
---|---|---|
committer | Andrew Cady <d@cryptonomic.net> | 2022-09-14 16:51:56 -0400 |
commit | 06fac581f5f0748f08d21113ed6cd283b35f0239 (patch) | |
tree | 5a145f8245926f2cb8b5e5caba78668bffb2adfb | |
parent | 149837d6c4a6181d09f2bbc8139594f29f2b4f9f (diff) |
access to chan from within event handler
-rwxr-xr-x | countdown.hs | 39 |
1 files changed, 21 insertions, 18 deletions
diff --git a/countdown.hs b/countdown.hs index 5068989..2e4114e 100755 --- a/countdown.hs +++ b/countdown.hs | |||
@@ -69,16 +69,22 @@ drawUI st = [a] | |||
69 | <=> | 69 | <=> |
70 | (str $ "Time now is: " <> (show $ st^.stCurrentTime)) | 70 | (str $ "Time now is: " <> (show $ st^.stCurrentTime)) |
71 | 71 | ||
72 | appEvent :: St -> BrickEvent () CustomEvent -> EventM () (Next St) | 72 | queueNextEvent :: MonadIO m => BChan CustomEvent -> UTCTime -> m () |
73 | appEvent st e = | 73 | queueNextEvent chan now = liftIO . void . forkIO $ do |
74 | threadDelay $ microsecondsUntilNextSecond now | ||
75 | getCurrentTime >>= writeBChan chan . TimeChanged | ||
76 | |||
77 | appEvent :: BChan CustomEvent -> St -> BrickEvent () CustomEvent -> EventM () (Next St) | ||
78 | appEvent chan st e = | ||
74 | case e of | 79 | case e of |
75 | VtyEvent (V.EvKey V.KEsc []) -> halt st | 80 | VtyEvent (V.EvKey V.KEsc []) -> halt st |
76 | VtyEvent _ -> cont st | 81 | VtyEvent _ -> cont st |
77 | AppEvent (TimeChanged t) -> cont $ st & stCurrentTime .~ t | 82 | AppEvent (TimeChanged now) -> do |
83 | queueNextEvent chan now | ||
84 | cont $ st & stCurrentTime .~ now | ||
78 | _ -> cont st | 85 | _ -> cont st |
79 | where | 86 | where |
80 | cont s = do | 87 | cont s = do |
81 | now <- liftIO getCurrentTime | ||
82 | continue $ s & stLastBrickEvent .~ (Just e) | 88 | continue $ s & stLastBrickEvent .~ (Just e) |
83 | 89 | ||
84 | initialState :: UTCTime -> St | 90 | initialState :: UTCTime -> St |
@@ -88,11 +94,11 @@ initialState t = | |||
88 | , _stNextEvent = Nothing | 94 | , _stNextEvent = Nothing |
89 | } | 95 | } |
90 | 96 | ||
91 | theApp :: App St CustomEvent () | 97 | theApp :: BChan CustomEvent -> App St CustomEvent () |
92 | theApp = | 98 | theApp chan = |
93 | App { appDraw = drawUI | 99 | App { appDraw = drawUI |
94 | , appChooseCursor = showFirstCursor | 100 | , appChooseCursor = showFirstCursor |
95 | , appHandleEvent = appEvent | 101 | , appHandleEvent = appEvent chan |
96 | , appStartEvent = return | 102 | , appStartEvent = return |
97 | , appAttrMap = const $ attrMap V.defAttr [] | 103 | , appAttrMap = const $ attrMap V.defAttr [] |
98 | } | 104 | } |
@@ -100,21 +106,18 @@ theApp = | |||
100 | zone :: TimeZone | 106 | zone :: TimeZone |
101 | zone = unsafePerformIO getCurrentTimeZone | 107 | zone = unsafePerformIO getCurrentTimeZone |
102 | 108 | ||
109 | microsecondsUntilNextSecond :: Integral a => UTCTime -> a | ||
110 | microsecondsUntilNextSecond now = (1000 * 1000 * remainingInCurrentSecond) `div'` 1 | ||
111 | where | ||
112 | todSecNow = todSec $ localTimeOfDay $ utcToLocalTime zone now | ||
113 | remainingInCurrentSecond = 1 - (todSecNow `mod'` 1) | ||
114 | |||
103 | main :: IO () | 115 | main :: IO () |
104 | main = do | 116 | main = do |
105 | chan <- newBChan 10 | 117 | chan <- newBChan 10 |
106 | 118 | ||
107 | void $ forkIO $ forever $ do | ||
108 | now <- getCurrentTime | ||
109 | let localNow = utcToLocalTime zone now | ||
110 | todNow = localTimeOfDay localNow | ||
111 | todSecNow = todSec todNow | ||
112 | remainingInCurrentSecond = 1 - (todSecNow `mod'` 1) | ||
113 | delay = (1000000 * remainingInCurrentSecond) `div'` 1 | ||
114 | threadDelay $ delay | ||
115 | getCurrentTime >>= writeBChan chan . TimeChanged | ||
116 | |||
117 | let buildVty = V.mkVty V.defaultConfig | 119 | let buildVty = V.mkVty V.defaultConfig |
118 | initialVty <- buildVty | 120 | initialVty <- buildVty |
119 | now <- getCurrentTime | 121 | now <- getCurrentTime |
120 | void $ customMain initialVty buildVty (Just chan) theApp (initialState now) | 122 | queueNextEvent chan now |
123 | void $ customMain initialVty buildVty (Just chan) (theApp chan) (initialState now) | ||