diff options
author | Andrew Cady <d@cryptonomic.net> | 2022-09-16 00:20:16 -0400 |
---|---|---|
committer | Andrew Cady <d@cryptonomic.net> | 2022-09-16 00:20:16 -0400 |
commit | 789bf4d934666918401bf599e7b7faf430bd43ac (patch) | |
tree | 6f4c2d14b4bd110b47540cef8609adcc95673a14 | |
parent | f5645f4fa3b00c68bacd7f8ddd8da71e1430d667 (diff) |
update display on tenths of seconds
-rwxr-xr-x | countdown.hs | 41 |
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' | |||
112 | dayNumOfYear :: LocalTime -> Int | 112 | dayNumOfYear :: LocalTime -> Int |
113 | dayNumOfYear = snd . toOrdinalDate . localDay | 113 | dayNumOfYear = snd . toOrdinalDate . localDay |
114 | 114 | ||
115 | pluralize :: Integral i => i -> String | 115 | pluralize :: (Num n, Eq n) => n -> String |
116 | pluralize 1 = "" | 116 | pluralize 1 = "" |
117 | pluralize _ = "s" | 117 | pluralize _ = "s" |
118 | 118 | ||
119 | pluralizeVerb :: Integral i => i -> String | 119 | pluralizeVerb :: (Num n, Eq n) => n -> String |
120 | pluralizeVerb 1 = "s" | 120 | pluralizeVerb 1 = "s" |
121 | pluralizeVerb _ = "" | 121 | pluralizeVerb _ = "" |
122 | 122 | ||
123 | truncateTime :: LocalTime -> LocalTime | ||
124 | truncateTime (LocalTime d (TimeOfDay h m s)) = (LocalTime d (TimeOfDay h m s')) | ||
125 | where | ||
126 | s' = fromIntegral (floor s :: Int) | ||
127 | |||
128 | drawUI :: St -> [Widget ()] | 123 | drawUI :: St -> [Widget ()] |
129 | drawUI st = [a] | 124 | drawUI st = [a] |
130 | where | 125 | where |
@@ -184,7 +179,7 @@ showLarge n | n >= 10 = unpack $ commasF 0 n | |||
184 | showLarge n = printf "%.3f" n | 179 | showLarge n = printf "%.3f" n |
185 | 180 | ||
186 | countdownWidget :: Bool -> LocalTime -> Widget n | 181 | countdownWidget :: Bool -> LocalTime -> Widget n |
187 | countdownWidget isSimulated (truncateTime -> t) = | 182 | countdownWidget 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 | |||
267 | commas :: Integral i => i -> Text | 262 | commas :: Integral i => i -> Text |
268 | commas = prettyI (Just ',') . fromIntegral | 263 | commas = prettyI (Just ',') . fromIntegral |
269 | 264 | ||
265 | nextTenthOfSecond :: ZonedTime -> ZonedTime | ||
266 | nextTenthOfSecond (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 | |||
270 | nextWholeSecond :: ZonedTime -> ZonedTime | 270 | nextWholeSecond :: ZonedTime -> ZonedTime |
271 | nextWholeSecond (ZonedTime (LocalTime day (TimeOfDay h m s)) z) = (ZonedTime (LocalTime day (TimeOfDay h m s')) z) | 271 | nextWholeSecond (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 | |||
275 | diffZonedTime :: ZonedTime -> ZonedTime -> NominalDiffTime | 275 | diffZonedTime :: ZonedTime -> ZonedTime -> NominalDiffTime |
276 | diffZonedTime = diffLocalTime `Prelude.on` zonedTimeToLocalTime | 276 | diffZonedTime = diffLocalTime `Prelude.on` zonedTimeToLocalTime |
277 | 277 | ||
278 | queueNextEvent :: MonadIO m => BChan CustomEvent -> m ZonedTime | 278 | isNewYearsEve t = dayNumOfYear t == daysInYear t |
279 | queueNextEvent chan = liftIO $ do | 279 | |
280 | queueNextEvent :: MonadIO m => Bool -> BChan CustomEvent -> m ZonedTime | ||
281 | queueNextEvent 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) |