-- |
--
-- Copyright:
--   This file is part of the package vimeta. It is subject to the
--   license terms in the LICENSE file found in the top-level
--   directory of this distribution and at:
--
--     https://github.com/pjones/vimeta
--
--   No part of this package, including this file, may be copied,
--   modified, propagated, or distributed except according to the terms
--   contained in the LICENSE file.
--
-- License: BSD-2-Clause
module Vimeta.UI.Common.Util
  ( parens,
    dayAsYear,
    dayRange,
  )
where

import Data.Time (Day, defaultTimeLocale, formatTime)

-- | Wrap some text with parenthesis.
parens :: Text -> Text
parens :: Text -> Text
parens Text
t = Text
" (" Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text
t Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text
")"

-- | Format a 'Maybe Day' as a year ('Text').
dayAsYear :: Maybe Day -> Text
dayAsYear :: Maybe Day -> Text
dayAsYear Maybe Day
Nothing = Text
"----"
dayAsYear (Just Day
d) = String -> Text
forall a. ToText a => a -> Text
toText (TimeLocale -> String -> Day -> String
forall t. FormatTime t => TimeLocale -> String -> t -> String
formatTime TimeLocale
defaultTimeLocale String
"%Y" Day
d)

-- | Given a start 'Day' and an end 'Day', produce a string
-- representing a range.
dayRange :: Maybe Day -> Maybe Day -> Text
dayRange :: Maybe Day -> Maybe Day -> Text
dayRange Maybe Day
d1 Maybe Day
d2 = Maybe Day -> Text
dayAsYear Maybe Day
d1 Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text
" - " Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Maybe Day -> Text
dayAsYear Maybe Day
d2