{- This file is part of irc-fun-color.
 -
 - Written in 2016 by fr33domlover <fr33domlover@riseup.net>.
 -
 - ♡ Copying is an act of love. Please copy, reuse and share.
 -
 - The author(s) have dedicated all copyright and related and neighboring
 - rights to this software to the public domain worldwide. This software is
 - distributed without any warranty.
 -
 - You should have received a copy of the CC0 Public Domain Dedication along
 - with this software. If not, see
 - <http://creativecommons.org/publicdomain/zero/1.0/>.
 -}

-- | This module provides functions which insert values into format templates,
-- much like @printf@. They are simply the same ones found in "Formatting", but
-- with slightly different names which are more convenient when strict Text is
-- a package's default (e.g. in IRC related packages where strings are likely
-- short). Suggested usage:
--
-- > import Network.IRC.Fun.Color.Format
-- > import Formatting hiding (format, sformat)
--
-- You may wish to use some formatters as well:
--
-- > import Network.IRC.Fun.Color.Format
-- > import Network.IRC.Fun.Color.Format.Long
-- > import Formatting hiding (format, sformat, text, stext)
module Network.IRC.Fun.Color.Format
    ( format
    , lformat
    , formatMsg
    )
where

import Formatting.Internal (Format (..))
import Network.IRC.Fun.Types.Base (MsgContent (..))

import qualified Data.Text as T (Text)
import qualified Data.Text.Lazy as TL (Text, toStrict)
import qualified Data.Text.Lazy.Builder as TLB (toLazyText)
import qualified Formatting as F

-- | Run the formatter and return a strict 'T.Text' value.
format :: F.Format T.Text a -> a
format = F.sformat

-- | Run the formatter and return a lazy 'TL.Text' value.
lformat :: F.Format TL.Text a -> a
lformat = F.format

-- | Run the formatter and return 'MsgContent', which is simply a wrapper of
-- strict 'T.Text'. This is s shortcut for @MsgContent . format@.
formatMsg :: F.Format MsgContent a -> a
formatMsg f = runFormat f $ MsgContent . TL.toStrict . TLB.toLazyText