irc-fun-bot-0.1.0.0: src/Network/IRC/Fun/Bot/Util.hs
{- This file is part of irc-fun-bot.
-
- Written in 2015 by fr33domlover <fr33domlover@rel4tion.org>.
-
- ♡ 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 collects smaller general purpose utilities which may be useful
-- to bot authors.
module Network.IRC.Fun.Bot.Util
( quote
, maybeQuote
, showNames
)
where
import Data.List (intercalate)
import Network.IRC.Fun.Bot.Types (Quotes (..))
-- | Add quotes to a string.
quote :: Quotes -> String -> String
quote (SingleQuotes True) s = '‘' : s ++ "’"
quote (SingleQuotes False) s = '\'' : s ++ "'"
quote (DoubleQuotes True) s = '“' : s ++ "”"
quote (DoubleQuotes False) s = '"' : s ++ "\""
quote BackTicks s = '`' : s ++ "`"
-- | Optionally add quotes to a string.
maybeQuote :: Maybe Quotes -> String -> String
maybeQuote = maybe id quote
-- | Format a list of labels.
--
-- >>> putStrLn $ showNames (Just $ SingleQuotes False)
-- >>> ", "
-- >>> ["one", "two", "three"]
-- 'one', 'two', 'three'
showNames :: Maybe Quotes -- ^ Optionally wrap labels with quotes
-> String -- ^ Separate between labels, e.g. spaces or newline
-> [String] -- ^ List of labels
-> String
showNames qs sep = intercalate sep . map (maybeQuote qs)