summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAndrew Cady <d@cryptonomic.net>2022-09-14 16:51:56 -0400
committerAndrew Cady <d@cryptonomic.net>2022-09-14 16:51:56 -0400
commit06fac581f5f0748f08d21113ed6cd283b35f0239 (patch)
tree5a145f8245926f2cb8b5e5caba78668bffb2adfb
parent149837d6c4a6181d09f2bbc8139594f29f2b4f9f (diff)
access to chan from within event handler
-rwxr-xr-xcountdown.hs39
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
72appEvent :: St -> BrickEvent () CustomEvent -> EventM () (Next St) 72queueNextEvent :: MonadIO m => BChan CustomEvent -> UTCTime -> m ()
73appEvent st e = 73queueNextEvent chan now = liftIO . void . forkIO $ do
74 threadDelay $ microsecondsUntilNextSecond now
75 getCurrentTime >>= writeBChan chan . TimeChanged
76
77appEvent :: BChan CustomEvent -> St -> BrickEvent () CustomEvent -> EventM () (Next St)
78appEvent 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
84initialState :: UTCTime -> St 90initialState :: UTCTime -> St
@@ -88,11 +94,11 @@ initialState t =
88 , _stNextEvent = Nothing 94 , _stNextEvent = Nothing
89 } 95 }
90 96
91theApp :: App St CustomEvent () 97theApp :: BChan CustomEvent -> App St CustomEvent ()
92theApp = 98theApp 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 =
100zone :: TimeZone 106zone :: TimeZone
101zone = unsafePerformIO getCurrentTimeZone 107zone = unsafePerformIO getCurrentTimeZone
102 108
109microsecondsUntilNextSecond :: Integral a => UTCTime -> a
110microsecondsUntilNextSecond now = (1000 * 1000 * remainingInCurrentSecond) `div'` 1
111 where
112 todSecNow = todSec $ localTimeOfDay $ utcToLocalTime zone now
113 remainingInCurrentSecond = 1 - (todSecNow `mod'` 1)
114
103main :: IO () 115main :: IO ()
104main = do 116main = 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)