summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAndrew Cady <d@cryptonomic.net>2022-09-14 16:24:34 -0400
committerAndrew Cady <d@cryptonomic.net>2022-09-14 16:24:34 -0400
commit149837d6c4a6181d09f2bbc8139594f29f2b4f9f (patch)
tree5e4e5d6235a361aaa84d10ff08ffc115b30e570e
parent788c473836cfb69f168edb9ddcefe49db215c45f (diff)
tick the second hand at the right time
-rwxr-xr-xcountdown.hs26
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
57data St = 57data 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
62makeLenses ''St 63makeLenses ''St
@@ -72,15 +73,19 @@ appEvent :: St -> BrickEvent () CustomEvent -> EventM () (Next St)
72appEvent st e = 73appEvent 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
80initialState :: UTCTime -> St 84initialState :: UTCTime -> St
81initialState t = 85initialState t =
82 St { _stLastBrickEvent = Nothing 86 St { _stLastBrickEvent = Nothing
83 , _stCurrentTime = t 87 , _stCurrentTime = t
88 , _stNextEvent = Nothing
84 } 89 }
85 90
86theApp :: App St CustomEvent () 91theApp :: App St CustomEvent ()
@@ -92,15 +97,24 @@ theApp =
92 , appAttrMap = const $ attrMap V.defAttr [] 97 , appAttrMap = const $ attrMap V.defAttr []
93 } 98 }
94 99
100zone :: TimeZone
101zone = unsafePerformIO getCurrentTimeZone
102
95main :: IO () 103main :: IO ()
96main = do 104main = 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)