diff options
author | Andrew Cady <d@cryptonomic.net> | 2022-09-14 16:24:34 -0400 |
---|---|---|
committer | Andrew Cady <d@cryptonomic.net> | 2022-09-14 16:24:34 -0400 |
commit | 149837d6c4a6181d09f2bbc8139594f29f2b4f9f (patch) | |
tree | 5e4e5d6235a361aaa84d10ff08ffc115b30e570e | |
parent | 788c473836cfb69f168edb9ddcefe49db215c45f (diff) |
tick the second hand at the right time
-rwxr-xr-x | countdown.hs | 26 |
1 files changed, 20 insertions, 6 deletions
diff --git a/countdown.hs b/countdown.hs index 0960a85..5068989 100755 --- a/countdown.hs +++ b/countdown.hs | |||
@@ -57,6 +57,7 @@ data CustomEvent = TimeChanged UTCTime deriving Show | |||
57 | data St = | 57 | data St = |
58 | St { _stLastBrickEvent :: Maybe (BrickEvent () CustomEvent) | 58 | St { _stLastBrickEvent :: Maybe (BrickEvent () CustomEvent) |
59 | , _stCurrentTime :: UTCTime | 59 | , _stCurrentTime :: UTCTime |
60 | , _stNextEvent :: Maybe UTCTime | ||
60 | } | 61 | } |
61 | 62 | ||
62 | makeLenses ''St | 63 | makeLenses ''St |
@@ -72,15 +73,19 @@ appEvent :: St -> BrickEvent () CustomEvent -> EventM () (Next St) | |||
72 | appEvent st e = | 73 | appEvent st e = |
73 | case e of | 74 | case e of |
74 | VtyEvent (V.EvKey V.KEsc []) -> halt st | 75 | VtyEvent (V.EvKey V.KEsc []) -> halt st |
75 | VtyEvent _ -> continue $ st & stLastBrickEvent .~ (Just e) | 76 | VtyEvent _ -> cont st |
76 | AppEvent (TimeChanged t) -> continue $ st & stCurrentTime .~ t | 77 | AppEvent (TimeChanged t) -> cont $ st & stCurrentTime .~ t |
77 | & stLastBrickEvent .~ (Just e) | 78 | _ -> cont st |
78 | _ -> continue st | 79 | where |
80 | cont s = do | ||
81 | now <- liftIO getCurrentTime | ||
82 | continue $ s & stLastBrickEvent .~ (Just e) | ||
79 | 83 | ||
80 | initialState :: UTCTime -> St | 84 | initialState :: UTCTime -> St |
81 | initialState t = | 85 | initialState t = |
82 | St { _stLastBrickEvent = Nothing | 86 | St { _stLastBrickEvent = Nothing |
83 | , _stCurrentTime = t | 87 | , _stCurrentTime = t |
88 | , _stNextEvent = Nothing | ||
84 | } | 89 | } |
85 | 90 | ||
86 | theApp :: App St CustomEvent () | 91 | theApp :: App St CustomEvent () |
@@ -92,15 +97,24 @@ theApp = | |||
92 | , appAttrMap = const $ attrMap V.defAttr [] | 97 | , appAttrMap = const $ attrMap V.defAttr [] |
93 | } | 98 | } |
94 | 99 | ||
100 | zone :: TimeZone | ||
101 | zone = unsafePerformIO getCurrentTimeZone | ||
102 | |||
95 | main :: IO () | 103 | main :: IO () |
96 | main = do | 104 | main = do |
97 | now <- getCurrentTime | ||
98 | chan <- newBChan 10 | 105 | chan <- newBChan 10 |
99 | 106 | ||
100 | void $ forkIO $ forever $ do | 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 | ||
101 | getCurrentTime >>= writeBChan chan . TimeChanged | 115 | getCurrentTime >>= writeBChan chan . TimeChanged |
102 | threadDelay 1000000 | ||
103 | 116 | ||
104 | let buildVty = V.mkVty V.defaultConfig | 117 | let buildVty = V.mkVty V.defaultConfig |
105 | initialVty <- buildVty | 118 | initialVty <- buildVty |
119 | now <- getCurrentTime | ||
106 | void $ customMain initialVty buildVty (Just chan) theApp (initialState now) | 120 | void $ customMain initialVty buildVty (Just chan) theApp (initialState now) |