summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAndrew Cady <d@cryptonomic.net>2022-09-16 00:20:16 -0400
committerAndrew Cady <d@cryptonomic.net>2022-09-16 00:20:16 -0400
commit789bf4d934666918401bf599e7b7faf430bd43ac (patch)
tree6f4c2d14b4bd110b47540cef8609adcc95673a14
parentf5645f4fa3b00c68bacd7f8ddd8da71e1430d667 (diff)
update display on tenths of seconds
-rwxr-xr-xcountdown.hs41
1 files changed, 23 insertions, 18 deletions
diff --git a/countdown.hs b/countdown.hs
index a7d1377..7c510b5 100755
--- a/countdown.hs
+++ b/countdown.hs
@@ -112,19 +112,14 @@ yearEnd (LocalTime d _) = LocalTime d' t'
112dayNumOfYear :: LocalTime -> Int 112dayNumOfYear :: LocalTime -> Int
113dayNumOfYear = snd . toOrdinalDate . localDay 113dayNumOfYear = snd . toOrdinalDate . localDay
114 114
115pluralize :: Integral i => i -> String 115pluralize :: (Num n, Eq n) => n -> String
116pluralize 1 = "" 116pluralize 1 = ""
117pluralize _ = "s" 117pluralize _ = "s"
118 118
119pluralizeVerb :: Integral i => i -> String 119pluralizeVerb :: (Num n, Eq n) => n -> String
120pluralizeVerb 1 = "s" 120pluralizeVerb 1 = "s"
121pluralizeVerb _ = "" 121pluralizeVerb _ = ""
122 122
123truncateTime :: LocalTime -> LocalTime
124truncateTime (LocalTime d (TimeOfDay h m s)) = (LocalTime d (TimeOfDay h m s'))
125 where
126 s' = fromIntegral (floor s :: Int)
127
128drawUI :: St -> [Widget ()] 123drawUI :: St -> [Widget ()]
129drawUI st = [a] 124drawUI st = [a]
130 where 125 where
@@ -184,7 +179,7 @@ showLarge n | n >= 10 = unpack $ commasF 0 n
184showLarge n = printf "%.3f" n 179showLarge n = printf "%.3f" n
185 180
186countdownWidget :: Bool -> LocalTime -> Widget n 181countdownWidget :: Bool -> LocalTime -> Widget n
187countdownWidget isSimulated (truncateTime -> t) = 182countdownWidget isSimulated t =
188 (hCenter (borderWithLabel (str $ printf "Current time%s" (if isSimulated then " (SIMULATED)" else "")) $ 183 (hCenter (borderWithLabel (str $ printf "Current time%s" (if isSimulated then " (SIMULATED)" else "")) $
189 padLeftRight 3 $ (str (formatTime defaultTimeLocale "%A, %B %e%n%Y-%m-%d %r" t)))) 184 padLeftRight 3 $ (str (formatTime defaultTimeLocale "%A, %B %e%n%Y-%m-%d %r" t))))
190 185
@@ -202,7 +197,7 @@ countdownWidget isSimulated (truncateTime -> t) =
202 (commasF 2 minutesLeft) 197 (commasF 2 minutesLeft)
203 (pluralize $ (floor minutesLeft :: Integer)) 198 (pluralize $ (floor minutesLeft :: Integer))
204 (pluralizeVerb $ (floor minutesLeft :: Integer)) 199 (pluralizeVerb $ (floor minutesLeft :: Integer))
205 (commas secondsLeft) 200 (commasF 1 secondsLeft)
206 (pluralize secondsLeft) 201 (pluralize secondsLeft)
207 (pluralizeVerb secondsLeft))) 202 (pluralizeVerb secondsLeft)))
208 , (borderWithLabel (str "Cosmic Time") $ 203 , (borderWithLabel (str "Cosmic Time") $
@@ -253,9 +248,9 @@ countdownWidget isSimulated (truncateTime -> t) =
253 248
254 daysLeft = numDays - dayNum 249 daysLeft = numDays - dayNum
255 hoursLeft = minutesLeft / 60 250 hoursLeft = minutesLeft / 60
256 minutesLeft = (fromIntegral secondsLeft) / 60 :: Double 251 minutesLeft = fromRational $ toRational secondsLeft / 60 :: Double
257 secondsLeft = toSeconds $ yearLength - yearElapsed :: Int
258 toSeconds = floor . nominalDiffTimeToSeconds 252 toSeconds = floor . nominalDiffTimeToSeconds
253 secondsLeft = nominalDiffTimeToSeconds (yearLength - yearElapsed)
259 x // y = fromRational $ toRational x / toRational y :: Double 254 x // y = fromRational $ toRational x / toRational y :: Double
260 progressLabel = printf "%.6F%%" (100 * (yearElapsed // yearLength)) 255 progressLabel = printf "%.6F%%" (100 * (yearElapsed // yearLength))
261 256
@@ -267,6 +262,11 @@ commasF precision = prettyF cfg
267commas :: Integral i => i -> Text 262commas :: Integral i => i -> Text
268commas = prettyI (Just ',') . fromIntegral 263commas = prettyI (Just ',') . fromIntegral
269 264
265nextTenthOfSecond :: ZonedTime -> ZonedTime
266nextTenthOfSecond (ZonedTime (LocalTime day (TimeOfDay h m s)) z) = (ZonedTime (LocalTime day (TimeOfDay h m s')) z)
267 where
268 s' = fromRational $ toRational (floor (s * 10) + 1 :: Integer) / 10
269
270nextWholeSecond :: ZonedTime -> ZonedTime 270nextWholeSecond :: ZonedTime -> ZonedTime
271nextWholeSecond (ZonedTime (LocalTime day (TimeOfDay h m s)) z) = (ZonedTime (LocalTime day (TimeOfDay h m s')) z) 271nextWholeSecond (ZonedTime (LocalTime day (TimeOfDay h m s)) z) = (ZonedTime (LocalTime day (TimeOfDay h m s')) z)
272 where 272 where
@@ -275,12 +275,16 @@ nextWholeSecond (ZonedTime (LocalTime day (TimeOfDay h m s)) z) = (ZonedTime (Lo
275diffZonedTime :: ZonedTime -> ZonedTime -> NominalDiffTime 275diffZonedTime :: ZonedTime -> ZonedTime -> NominalDiffTime
276diffZonedTime = diffLocalTime `Prelude.on` zonedTimeToLocalTime 276diffZonedTime = diffLocalTime `Prelude.on` zonedTimeToLocalTime
277 277
278queueNextEvent :: MonadIO m => BChan CustomEvent -> m ZonedTime 278isNewYearsEve t = dayNumOfYear t == daysInYear t
279queueNextEvent chan = liftIO $ do 279
280queueNextEvent :: MonadIO m => Bool -> BChan CustomEvent -> m ZonedTime
281queueNextEvent hyper chan = liftIO $ do
280 now <- getZonedTime 282 now <- getZonedTime
281 void . forkIO $ do 283 void . forkIO $ do
282 let next = nextWholeSecond now 284
283 let delay = next `diffZonedTime` now 285 let getNext = if hyper then nextTenthOfSecond else nextWholeSecond
286 next = getNext now
287 delay = next `diffZonedTime` now
284 threadDelay $ floor $ delay * 1000 * 1000 288 threadDelay $ floor $ delay * 1000 * 1000
285 writeBChan chan $ TimeChanged next 289 writeBChan chan $ TimeChanged next
286 return now 290 return now
@@ -310,10 +314,11 @@ handleEvent chan st e =
310 VtyEvent (V.EvKey (V.KChar 'p') []) -> cont $ st & stPaused %~ not 314 VtyEvent (V.EvKey (V.KChar 'p') []) -> cont $ st & stPaused %~ not
311 VtyEvent _ -> cont st 315 VtyEvent _ -> cont st
312 AppEvent (TimeChanged now) -> do 316 AppEvent (TimeChanged now) -> do
313 void $ queueNextEvent chan 317 let hyper = isNewYearsEve $ st ^. stDisplayTime
318 void $ queueNextEvent hyper chan
314 let oldTime = st ^. stClockTime 319 let oldTime = st ^. stClockTime
315 cont $ st & stClockTime .~ (zonedTimeToLocalTime now) 320 cont $ st & stClockTime .~ (zonedTimeToLocalTime now)
316 & stDisplayTime %~ if st ^. stPaused then id else (addLocalTime ((zonedTimeToLocalTime now) `diffLocalTime` oldTime)) 321 & stDisplayTime %~ if st ^. stPaused then id else addLocalTime $ zonedTimeToLocalTime now `diffLocalTime` oldTime
317 _ -> cont st 322 _ -> cont st
318 where 323 where
319 cont s = do 324 cont s = do
@@ -386,5 +391,5 @@ main = do
386 391
387 let buildVty = V.mkVty V.defaultConfig 392 let buildVty = V.mkVty V.defaultConfig
388 initialVty <- buildVty 393 initialVty <- buildVty
389 now <- queueNextEvent chan 394 now <- queueNextEvent False chan
390 void $ customMain initialVty buildVty (Just chan) (theApp chan) (initialState $ zonedTimeToLocalTime now) 395 void $ customMain initialVty buildVty (Just chan) (theApp chan) (initialState $ zonedTimeToLocalTime now)