pdfname-0.1: src/Utilities.hs
-- | Utilities.
module Utilities
( (+++)
, die
, ifM
, isPDF
, progNameVersion
, replace
, unless_
, unlessM
, upperFirst
) where
import Data.Char ( toUpper )
import Data.Text ( Text )
import qualified Data.Text as T
import qualified Data.Text.IO as T
import Data.Version ( showVersion )
import Control.Monad ( replicateM, unless, void )
import Paths_pdfname ( version )
import System.Environment ( getProgName )
import System.Exit ( exitFailure )
import System.IO
( IOMode(ReadMode)
, hGetChar
, stderr
, withBinaryFile
)
------------------------------------------------------------------------------
-- Utilities from the Agda library
ifM ∷ Monad m ⇒ m Bool → m a → m a → m a
ifM c m m' = do
b ← c
if b then m else m'
-- | @unless_@ is just @Control.Monad.unless@ with a more general type.
unless_ ∷ Monad m ⇒ Bool → m a → m ()
unless_ b m = unless b $ void m
unlessM ∷ Monad m ⇒ m Bool → m a → m ()
unlessM c m = c >>= (`unless_` m)
------------------------------------------------------------------------------
-- Utilities
die ∷ Text → IO a
die err = do
progName ← getProgName
T.hPutStrLn stderr (T.pack progName +++ ": " +++ err)
exitFailure
replace ∷ [(Text,Text)] → Text → Text
replace xs ys = foldl (flip (uncurry T.replace)) ys xs
-- | Uppercase the first character of a text.
upperFirst ∷ Text → Text
upperFirst xs =
case T.uncons xs of
Nothing → T.empty
Just (x', xs') → T.cons (toUpper x') (T.toLower xs')
-- | Append synonymous.
(+++) ∷ Text → Text → Text
(+++) = T.append
-- | Return version information.
-- TODO (2017-07-06): To use `getProgName`.
progNameVersion ∷ String
progNameVersion = "pdfname" ++ " version " ++ showVersion version
isPDF ∷ FilePath → IO Bool
isPDF f =
ifM ((==) "%PDF" <$> withBinaryFile f ReadMode (replicateM 4 . hGetChar))
(return True)
(return False)