{-|
Module      : Htcc.Utils.Print
Description : Utilities
Copyright   : (c) roki, 2019
License     : MIT
Maintainer  : falgon53@yahoo.co.jp
Stability   : experimental
Portability : POSIX

Utilities of print
-}
{-# LANGUAGE ScopedTypeVariables #-}
module Htcc.Utils.Print (
    -- * Shortcuts of print
    putStrErr, putStrLnErr, err,
    putDocLn, putDocErr, putDocLnErr,
    errTxtDoc, errCharDoc, warnTxtDoc,
    warnCharDoc, locTxtDoc, locCharDoc,
) where

import qualified Data.Text                    as T
import qualified Data.Text.IO                 as T
import           Prelude                      hiding (toInteger)
import           System.Exit                  (exitFailure)
import           System.IO                    (stderr)
import           Text.PrettyPrint.ANSI.Leijen (Doc, bold, char, hPutDoc,
                                               linebreak, magenta, putDoc, red,
                                               text)

{-# INLINE putDocLn #-}
-- | Execute `Text.PrettyPrint.ANSI.Leijen.putDoc` by applying `Text.PrettyPrint.ANSI.Leijen.linebreak`
-- to `Text.PrettyPrint.ANSI.Leijen.<>` at the end of given `Text.PrettyPrint.ANSI.Leijen.Doc`
putDocLn :: Doc -> IO ()
putDocLn :: Doc -> IO ()
putDocLn = Doc -> IO ()
putDoc (Doc -> IO ()) -> (Doc -> Doc) -> Doc -> IO ()
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (Doc -> Doc -> Doc) -> Doc -> Doc -> Doc
forall a b c. (a -> b -> c) -> b -> a -> c
flip Doc -> Doc -> Doc
forall a. Semigroup a => a -> a -> a
(<>) Doc
linebreak

{-# INLINE putDocErr #-}
-- | The shortcut of @hPutDoc stderr@
putDocErr :: Doc -> IO ()
putDocErr :: Doc -> IO ()
putDocErr = Handle -> Doc -> IO ()
hPutDoc Handle
stderr

{-# INLINE putDocLnErr #-}
-- | Execute `putDocErr` by applying `Text.PrettyPrint.ANSI.Leijen.linebreak`
-- to `Text.PrettyPrint.ANSI.Leijen.<>` at the end of given `Text.PrettyPrint.ANSI.Leijen.Doc`
putDocLnErr :: Doc -> IO ()
putDocLnErr :: Doc -> IO ()
putDocLnErr = Doc -> IO ()
putDocErr (Doc -> IO ()) -> (Doc -> Doc) -> Doc -> IO ()
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (Doc -> Doc -> Doc) -> Doc -> Doc -> Doc
forall a b c. (a -> b -> c) -> b -> a -> c
flip Doc -> Doc -> Doc
forall a. Semigroup a => a -> a -> a
(<>) Doc
linebreak

{-# INLINE errTxtDoc #-}
-- | The `Text.PrettyPrint.ANSI.Leijen.Doc` used to output an error message (`String`),
-- it is shortcut of @red . text@
errTxtDoc :: String -> Doc
errTxtDoc :: String -> Doc
errTxtDoc = Doc -> Doc
red (Doc -> Doc) -> (String -> Doc) -> String -> Doc
forall b c a. (b -> c) -> (a -> b) -> a -> c
. String -> Doc
text

{-# INLINE errCharDoc #-}
-- | The `Text.PrettyPrint.ANSI.Leijen.Doc` used to output an error message (`Char`),
-- it is shortcut of @red. char@
errCharDoc :: Char -> Doc
errCharDoc :: Char -> Doc
errCharDoc = Doc -> Doc
red (Doc -> Doc) -> (Char -> Doc) -> Char -> Doc
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Char -> Doc
char

{-# INLINE warnTxtDoc #-}
-- | The `Text.PrettyPrint.ANSI.Leijen.Doc` used to output an warning message (`String`),
-- it is shortcut of @magenta . text@
warnTxtDoc :: String -> Doc
warnTxtDoc :: String -> Doc
warnTxtDoc = Doc -> Doc
magenta (Doc -> Doc) -> (String -> Doc) -> String -> Doc
forall b c a. (b -> c) -> (a -> b) -> a -> c
. String -> Doc
text

{-# INLINE warnCharDoc #-}
-- | The `Text.PrettyPrint.ANSI.Leijen.Doc` used to output an warning message (`Char`),
-- it is shortcut of @magenta . char@
warnCharDoc :: Char -> Doc
warnCharDoc :: Char -> Doc
warnCharDoc = Doc -> Doc
magenta (Doc -> Doc) -> (Char -> Doc) -> Char -> Doc
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Char -> Doc
char

{-# INLINE locTxtDoc #-}
-- | Doc used to output a message (`String`) about the location, such as the file name and its location,
-- it is shortcut of @bold . text@
locTxtDoc :: String -> Doc
locTxtDoc :: String -> Doc
locTxtDoc = Doc -> Doc
bold (Doc -> Doc) -> (String -> Doc) -> String -> Doc
forall b c a. (b -> c) -> (a -> b) -> a -> c
. String -> Doc
text

{-# INLINE locCharDoc #-}
-- | Doc used to output a message (`Char`) about the location, such as the file name and its location,
-- it is shortcut of @bold . char@
locCharDoc :: Char -> Doc
locCharDoc :: Char -> Doc
locCharDoc = Doc -> Doc
bold (Doc -> Doc) -> (Char -> Doc) -> Char -> Doc
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Char -> Doc
char

-- | Standard error output shortcut (with new line).
putStrLnErr :: T.Text -> IO ()
putStrLnErr :: Text -> IO ()
putStrLnErr = Handle -> Text -> IO ()
T.hPutStrLn Handle
stderr

-- | Standard error output shortcut.
putStrErr :: T.Text -> IO ()
putStrErr :: Text -> IO ()
putStrErr = Handle -> Text -> IO ()
T.hPutStr Handle
stderr

-- | Standard error output and exit shortcut.
err :: T.Text -> IO ()
err :: Text -> IO ()
err = (IO () -> IO () -> IO ()) -> IO () -> IO () -> IO ()
forall a b c. (a -> b -> c) -> b -> a -> c
flip IO () -> IO () -> IO ()
forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
(>>) IO ()
forall a. IO a
exitFailure (IO () -> IO ()) -> (Text -> IO ()) -> Text -> IO ()
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Text -> IO ()
putStrLnErr