diff options
author | Andrew Cady <d@cryptonomic.net> | 2022-09-16 21:05:25 -0400 |
---|---|---|
committer | Andrew Cady <d@cryptonomic.net> | 2022-09-16 21:05:25 -0400 |
commit | 49ad29c21b5b86e6e2f9837d041005c0b3e560a5 (patch) | |
tree | dd5035c6ccd9a680cd196bc33a7a83755b578bd4 | |
parent | e31a8e857dbbadd0cd91749220a5d21d3b48cbfb (diff) |
output tweaking
-rwxr-xr-x | countdown.hs | 29 |
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 #-} |
14 | import Rebase.Prelude hiding (toList, on, (<+>)) | 14 | {-# language OverloadedStrings #-} |
15 | import Rebase.Prelude hiding (toList, on, (<+>), Max) | ||
15 | import qualified Rebase.Prelude as Prelude | 16 | import qualified Rebase.Prelude as Prelude |
16 | import Control.Lens hiding ((<|)) | 17 | import Control.Lens hiding ((<|)) |
17 | import Data.Foldable (toList) | 18 | import Data.Foldable (toList) |
@@ -21,10 +22,13 @@ import Graphics.Vty | |||
21 | import Data.Time.LocalTime | 22 | import Data.Time.LocalTime |
22 | import Control.Monad.RWS | 23 | import Control.Monad.RWS |
23 | import Data.Time.Calendar.OrdinalDate | 24 | import Data.Time.Calendar.OrdinalDate |
25 | import qualified Data.Text as Text | ||
24 | import Data.Text.Format.Numbers | 26 | import Data.Text.Format.Numbers |
25 | import Rebase.Data.Map.Strict (Map) | 27 | import Rebase.Data.Map.Strict (Map) |
26 | import qualified Rebase.Data.Map.Strict as Map | 28 | import qualified Rebase.Data.Map.Strict as Map |
27 | 29 | ||
30 | import Brick | ||
31 | import Brick.Types | ||
28 | import Data.Text (unpack) | 32 | import Data.Text (unpack) |
29 | import Control.Lens | 33 | import Control.Lens |
30 | import Control.Monad (void, forever) | 34 | import Control.Monad (void, forever) |
@@ -109,11 +113,12 @@ drawUI st = [a] | |||
109 | 113 | ||
110 | showTime :: NominalDiffTime -> String | 114 | showTime :: NominalDiffTime -> String |
111 | showTime t | t < 1 = printf "%.3f seconds" (realToFrac t :: Float) | 115 | showTime t | t < 1 = printf "%.3f seconds" (realToFrac t :: Float) |
112 | showTime t | t == 1 = "1 second" | 116 | showTime 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! |
114 | showTime t | t < 10 = printf "%.2f seconds" (realToFrac t :: Float) | 118 | -- showTime t | t < 10 = printf "%.2f seconds" (realToFrac t :: Float) |
115 | showTime t | t < 60 = printf "%.1f seconds" (realToFrac t :: Float) | 119 | showTime t | t < 60 = printf "%.1f seconds" (realToFrac t :: Float) |
116 | showTime t | t == 60 = formatTime defaultTimeLocale "%M minute" t | 120 | showTime t | t == 60 = formatTime defaultTimeLocale "%M minute" t |
121 | showTime t | t < 60*2 = formatTime defaultTimeLocale "%M minute %Ss" t | ||
117 | showTime t | t < 60*10 = formatTime defaultTimeLocale "%M minutes %Ss" t | 122 | showTime t | t < 60*10 = formatTime defaultTimeLocale "%M minutes %Ss" t |
118 | showTime t | t < 60*60 = formatTime defaultTimeLocale "%M minutes" t | 123 | showTime t | t < 60*60 = formatTime defaultTimeLocale "%M minutes" t |
119 | showTime t | t == 60*60 = "1 hour" | 124 | showTime 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 | ||
146 | showCosmicTime :: NominalDiffTime -> String | ||
147 | showCosmicTime n = showLarge (fromRational $ toCosmicTime n) ++ " years" | ||
148 | |||
141 | conversionTableRowFromCosmicSeconds :: NominalDiffTime -> [Widget n] | 149 | conversionTableRowFromCosmicSeconds :: NominalDiffTime -> [Widget n] |
142 | conversionTableRowFromCosmicSeconds n = [cosmicTime, realTime] | 150 | conversionTableRowFromCosmicSeconds 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 | ||
147 | cosmicConversion' :: Widget n | 155 | cosmicConversion' :: 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 | ||
309 | commasF' 1 n = if (floor (n * 10) `mod` 10 :: Int) == 0 then commasF 0 n else commasF 1 n | 324 | commasF' 1 n = if (floor (n * 10) `mod` 10 :: Int) == 0 then commasF 0 n `Text.append` " " else commasF 1 n |
310 | commasF' p n = commasF p n | 325 | commasF' p n = commasF p n |
311 | 326 | ||
312 | commasF', commasF :: RealFrac x => Int -> x -> Text | 327 | commasF', commasF :: RealFrac x => Int -> x -> Text |