summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAndrew Cady <d@cryptonomic.net>2022-09-16 21:05:25 -0400
committerAndrew Cady <d@cryptonomic.net>2022-09-16 21:05:25 -0400
commit49ad29c21b5b86e6e2f9837d041005c0b3e560a5 (patch)
treedd5035c6ccd9a680cd196bc33a7a83755b578bd4
parente31a8e857dbbadd0cd91749220a5d21d3b48cbfb (diff)
output tweaking
-rwxr-xr-xcountdown.hs29
1 files changed, 22 insertions, 7 deletions
diff --git a/countdown.hs b/countdown.hs
index a6d5a31..419a7c8 100755
--- a/countdown.hs
+++ b/countdown.hs
@@ -11,7 +11,8 @@
11{-# language FlexibleContexts #-} 11{-# language FlexibleContexts #-}
12{-# language TemplateHaskell #-} 12{-# language TemplateHaskell #-}
13{-# language ViewPatterns #-} 13{-# language ViewPatterns #-}
14import Rebase.Prelude hiding (toList, on, (<+>)) 14{-# language OverloadedStrings #-}
15import Rebase.Prelude hiding (toList, on, (<+>), Max)
15import qualified Rebase.Prelude as Prelude 16import qualified Rebase.Prelude as Prelude
16import Control.Lens hiding ((<|)) 17import Control.Lens hiding ((<|))
17import Data.Foldable (toList) 18import Data.Foldable (toList)
@@ -21,10 +22,13 @@ import Graphics.Vty
21import Data.Time.LocalTime 22import Data.Time.LocalTime
22import Control.Monad.RWS 23import Control.Monad.RWS
23import Data.Time.Calendar.OrdinalDate 24import Data.Time.Calendar.OrdinalDate
25import qualified Data.Text as Text
24import Data.Text.Format.Numbers 26import Data.Text.Format.Numbers
25import Rebase.Data.Map.Strict (Map) 27import Rebase.Data.Map.Strict (Map)
26import qualified Rebase.Data.Map.Strict as Map 28import qualified Rebase.Data.Map.Strict as Map
27 29
30import Brick
31import Brick.Types
28import Data.Text (unpack) 32import Data.Text (unpack)
29import Control.Lens 33import Control.Lens
30import Control.Monad (void, forever) 34import Control.Monad (void, forever)
@@ -109,11 +113,12 @@ drawUI st = [a]
109 113
110showTime :: NominalDiffTime -> String 114showTime :: NominalDiffTime -> String
111showTime t | t < 1 = printf "%.3f seconds" (realToFrac t :: Float) 115showTime t | t < 1 = printf "%.3f seconds" (realToFrac t :: Float)
112showTime t | t == 1 = "1 second" 116showTime t | t == 1 = "1 second"
113-- showTime t | t < 10 = formatTime defaultTimeLocale "%2Es seconds" t -- BUG! Doesn't respect <width> parameter at all! 117-- showTime t | t < 10 = formatTime defaultTimeLocale "%2Es seconds" t -- BUG! Doesn't respect <width> parameter at all!
114showTime t | t < 10 = printf "%.2f seconds" (realToFrac t :: Float) 118-- showTime t | t < 10 = printf "%.2f seconds" (realToFrac t :: Float)
115showTime t | t < 60 = printf "%.1f seconds" (realToFrac t :: Float) 119showTime t | t < 60 = printf "%.1f seconds" (realToFrac t :: Float)
116showTime t | t == 60 = formatTime defaultTimeLocale "%M minute" t 120showTime t | t == 60 = formatTime defaultTimeLocale "%M minute" t
121showTime t | t < 60*2 = formatTime defaultTimeLocale "%M minute %Ss" t
117showTime t | t < 60*10 = formatTime defaultTimeLocale "%M minutes %Ss" t 122showTime t | t < 60*10 = formatTime defaultTimeLocale "%M minutes %Ss" t
118showTime t | t < 60*60 = formatTime defaultTimeLocale "%M minutes" t 123showTime t | t < 60*60 = formatTime defaultTimeLocale "%M minutes" t
119showTime t | t == 60*60 = "1 hour" 124showTime t | t == 60*60 = "1 hour"
@@ -138,10 +143,13 @@ toCosmicTime t = realToFrac ageOfUniverseInYears * (realToFrac $ t // yearLength
138 yearLength = daysPerYear * nominalDay 143 yearLength = daysPerYear * nominalDay
139 x // y = fromRational $ toRational x / toRational y :: Double 144 x // y = fromRational $ toRational x / toRational y :: Double
140 145
146showCosmicTime :: NominalDiffTime -> String
147showCosmicTime n = showLarge (fromRational $ toCosmicTime n) ++ " years"
148
141conversionTableRowFromCosmicSeconds :: NominalDiffTime -> [Widget n] 149conversionTableRowFromCosmicSeconds :: NominalDiffTime -> [Widget n]
142conversionTableRowFromCosmicSeconds n = [cosmicTime, realTime] 150conversionTableRowFromCosmicSeconds n = [cosmicTime, realTime]
143 where 151 where
144 realTime = str $ showLarge (fromRational $ toCosmicTime n) ++ " years" 152 realTime = str $ showCosmicTime n
145 cosmicTime = str $ showTime n 153 cosmicTime = str $ showTime n
146 154
147cosmicConversion' :: Widget n 155cosmicConversion' :: Widget n
@@ -221,8 +229,15 @@ countdownWidget isSimulated t =
221 prevEntry = fromMaybe (str "none") $ calendarWidget <$> getLastCalendarEntry t 229 prevEntry = fromMaybe (str "none") $ calendarWidget <$> getLastCalendarEntry t
222 nextEntry = fromMaybe (str "none") $ calendarWidget <$> getNextCalendarEntry t 230 nextEntry = fromMaybe (str "none") $ calendarWidget <$> getNextCalendarEntry t
223 231
224 calendarWidget CalendarEntry{..} = box 232 calendarWidget CalendarEntry{..} = vBox [eventCountdown, str "\n", box]
225 where 233 where
234 timeUntilActive = (calBeginTime `addLocalTime` yearStart t) `diffLocalTime` t
235 eventCountdown = if timeUntilActive >= 0 then
236 hBox [str $ "in " ++ showTime timeUntilActive,
237 padLeft Max $ str $ "in " ++ showCosmicTime timeUntilActive]
238 else
239 hBox [str $ showTime (-timeUntilActive) ++ " ago",
240 padLeft Max $ str $ showCosmicTime (-timeUntilActive) ++ " ago"]
226 years = fromRational $ toCosmicTime calBeginTime 241 years = fromRational $ toCosmicTime calBeginTime
227 box = borderWithLabel (txt calTitle) $ vBox [ 242 box = borderWithLabel (txt calTitle) $ vBox [
228 hCenter $ txt $ calSubtitle, 243 hCenter $ txt $ calSubtitle,
@@ -284,7 +299,7 @@ countdownWidget isSimulated t =
284 -- , (str $ printf "%s thousand years ago" (commasF 3 $ cosmicYearsAgo / 1000)) 299 -- , (str $ printf "%s thousand years ago" (commasF 3 $ cosmicYearsAgo / 1000))
285 -- , (str $ printf "%s days ago" (commas $ (floor $ realToFrac cosmicYearsAgo * daysPerYear :: Integer))) 300 -- , (str $ printf "%s days ago" (commas $ (floor $ realToFrac cosmicYearsAgo * daysPerYear :: Integer)))
286 ]) 301 ])
287 currentTimeBox = (hCenter (borderWithLabel (str $ printf "Current time%s" (if isSimulated then " (SIMULATED)" else "")) $ 302 currentTimeBox = (hCenter (borderWithLabel (str $ printf "Current time%s" (if isSimulated then " (SIMULATED)" else "" :: String)) $
288 padLeftRight 3 $ (str (formatTime defaultTimeLocale "%A, %B %e%n%Y-%m-%d %r" t)))) 303 padLeftRight 3 $ (str (formatTime defaultTimeLocale "%A, %B %e%n%Y-%m-%d %r" t))))
289 cosmicYears = realToFrac ageOfUniverseInYears * (realToFrac $ (yearElapsed // yearLength)) :: Rational 304 cosmicYears = realToFrac ageOfUniverseInYears * (realToFrac $ (yearElapsed // yearLength)) :: Rational
290 cosmicYearsAgo = realToFrac ageOfUniverseInYears * (realToFrac $ 1 - (yearElapsed // yearLength)) :: Rational 305 cosmicYearsAgo = realToFrac ageOfUniverseInYears * (realToFrac $ 1 - (yearElapsed // yearLength)) :: Rational
@@ -306,7 +321,7 @@ countdownWidget isSimulated t =
306 x // y = fromRational $ toRational x / toRational y :: Double 321 x // y = fromRational $ toRational x / toRational y :: Double
307 progressLabel = printf "%.6F%%" (100 * (yearElapsed // yearLength)) 322 progressLabel = printf "%.6F%%" (100 * (yearElapsed // yearLength))
308 323
309commasF' 1 n = if (floor (n * 10) `mod` 10 :: Int) == 0 then commasF 0 n else commasF 1 n 324commasF' 1 n = if (floor (n * 10) `mod` 10 :: Int) == 0 then commasF 0 n `Text.append` " " else commasF 1 n
310commasF' p n = commasF p n 325commasF' p n = commasF p n
311 326
312commasF', commasF :: RealFrac x => Int -> x -> Text 327commasF', commasF :: RealFrac x => Int -> x -> Text