diff options
author | Andrew Cady <d@cryptonomic.net> | 2022-09-14 15:48:25 -0400 |
---|---|---|
committer | Andrew Cady <d@cryptonomic.net> | 2022-09-14 15:48:25 -0400 |
commit | 788c473836cfb69f168edb9ddcefe49db215c45f (patch) | |
tree | 1cc66c5b3cca4232a0c946de94f56f95db1b622a | |
parent | 737a808304a0d099e763431489bf1ec47c20b2de (diff) |
clock functionality
-rwxr-xr-x | countdown.hs | 20 |
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 | |||
52 | ageOfUniverseInYears :: Integer | 52 | ageOfUniverseInYears :: Integer |
53 | ageOfUniverseInYears = 13 * billion | 53 | ageOfUniverseInYears = 13 * billion |
54 | 54 | ||
55 | data CustomEvent = Counter deriving Show | 55 | data CustomEvent = TimeChanged UTCTime deriving Show |
56 | 56 | ||
57 | data St = | 57 | data St = |
58 | St { _stLastBrickEvent :: Maybe (BrickEvent () CustomEvent) | 58 | St { _stLastBrickEvent :: Maybe (BrickEvent () CustomEvent) |
59 | , _stCounter :: Int | 59 | , _stCurrentTime :: UTCTime |
60 | } | 60 | } |
61 | 61 | ||
62 | makeLenses ''St | 62 | makeLenses ''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 | ||
71 | appEvent :: St -> BrickEvent () CustomEvent -> EventM () (Next St) | 71 | appEvent :: St -> BrickEvent () CustomEvent -> EventM () (Next St) |
72 | appEvent st e = | 72 | appEvent 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 | ||
80 | initialState :: St | 80 | initialState :: UTCTime -> St |
81 | initialState = | 81 | initialState t = |
82 | St { _stLastBrickEvent = Nothing | 82 | St { _stLastBrickEvent = Nothing |
83 | , _stCounter = 0 | 83 | , _stCurrentTime = t |
84 | } | 84 | } |
85 | 85 | ||
86 | theApp :: App St CustomEvent () | 86 | theApp :: 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) |