summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAndrew Cady <d@cryptonomic.net>2022-09-14 15:48:25 -0400
committerAndrew Cady <d@cryptonomic.net>2022-09-14 15:48:25 -0400
commit788c473836cfb69f168edb9ddcefe49db215c45f (patch)
tree1cc66c5b3cca4232a0c946de94f56f95db1b622a
parent737a808304a0d099e763431489bf1ec47c20b2de (diff)
clock functionality
-rwxr-xr-xcountdown.hs20
1 files changed, 10 insertions, 10 deletions
diff --git a/countdown.hs b/countdown.hs
index 13269ff..0960a85 100755
--- a/countdown.hs
+++ b/countdown.hs
@@ -52,11 +52,11 @@ billion = 1000 * 1000 * 1000
52ageOfUniverseInYears :: Integer 52ageOfUniverseInYears :: Integer
53ageOfUniverseInYears = 13 * billion 53ageOfUniverseInYears = 13 * billion
54 54
55data CustomEvent = Counter deriving Show 55data CustomEvent = TimeChanged UTCTime deriving Show
56 56
57data St = 57data St =
58 St { _stLastBrickEvent :: Maybe (BrickEvent () CustomEvent) 58 St { _stLastBrickEvent :: Maybe (BrickEvent () CustomEvent)
59 , _stCounter :: Int 59 , _stCurrentTime :: UTCTime
60 } 60 }
61 61
62makeLenses ''St 62makeLenses ''St
@@ -66,21 +66,21 @@ drawUI st = [a]
66 where 66 where
67 a = (str $ "Last event: " <> (show $ st^.stLastBrickEvent)) 67 a = (str $ "Last event: " <> (show $ st^.stLastBrickEvent))
68 <=> 68 <=>
69 (str $ "Counter value is: " <> (show $ st^.stCounter)) 69 (str $ "Time now is: " <> (show $ st^.stCurrentTime))
70 70
71appEvent :: St -> BrickEvent () CustomEvent -> EventM () (Next St) 71appEvent :: St -> BrickEvent () CustomEvent -> EventM () (Next St)
72appEvent st e = 72appEvent st e =
73 case e of 73 case e of
74 VtyEvent (V.EvKey V.KEsc []) -> halt st 74 VtyEvent (V.EvKey V.KEsc []) -> halt st
75 VtyEvent _ -> continue $ st & stLastBrickEvent .~ (Just e) 75 VtyEvent _ -> continue $ st & stLastBrickEvent .~ (Just e)
76 AppEvent Counter -> continue $ st & stCounter %~ (+1) 76 AppEvent (TimeChanged t) -> continue $ st & stCurrentTime .~ t
77 & stLastBrickEvent .~ (Just e) 77 & stLastBrickEvent .~ (Just e)
78 _ -> continue st 78 _ -> continue st
79 79
80initialState :: St 80initialState :: UTCTime -> St
81initialState = 81initialState t =
82 St { _stLastBrickEvent = Nothing 82 St { _stLastBrickEvent = Nothing
83 , _stCounter = 0 83 , _stCurrentTime = t
84 } 84 }
85 85
86theApp :: App St CustomEvent () 86theApp :: App St CustomEvent ()
@@ -98,9 +98,9 @@ main = do
98 chan <- newBChan 10 98 chan <- newBChan 10
99 99
100 void $ forkIO $ forever $ do 100 void $ forkIO $ forever $ do
101 writeBChan chan Counter 101 getCurrentTime >>= writeBChan chan . TimeChanged
102 threadDelay 1000000 102 threadDelay 1000000
103 103
104 let buildVty = V.mkVty V.defaultConfig 104 let buildVty = V.mkVty V.defaultConfig
105 initialVty <- buildVty 105 initialVty <- buildVty
106 void $ customMain initialVty buildVty (Just chan) theApp initialState 106 void $ customMain initialVty buildVty (Just chan) theApp (initialState now)