{-# LANGUAGE CPP #-}
{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE RankNTypes #-}
{-# LANGUAGE ScopedTypeVariables #-}
module Text.Pretty.Simple
(
pPrint
, pHPrint
, pPrintString
, pHPrintString
, pPrintForceColor
, pHPrintForceColor
, pPrintStringForceColor
, pHPrintStringForceColor
, pShow
, pString
, pPrintDarkBg
, pHPrintDarkBg
, pPrintStringDarkBg
, pHPrintStringDarkBg
, pPrintForceColorDarkBg
, pHPrintForceColorDarkBg
, pPrintStringForceColorDarkBg
, pHPrintStringForceColorDarkBg
, pShowDarkBg
, pStringDarkBg
, pPrintLightBg
, pHPrintLightBg
, pPrintStringLightBg
, pHPrintStringLightBg
, pPrintForceColorLightBg
, pHPrintForceColorLightBg
, pPrintStringForceColorLightBg
, pHPrintStringForceColorLightBg
, pShowLightBg
, pStringLightBg
, pPrintNoColor
, pHPrintNoColor
, pPrintStringNoColor
, pHPrintStringNoColor
, pShowNoColor
, pStringNoColor
, pPrintOpt
, pHPrintOpt
, pPrintStringOpt
, pHPrintStringOpt
, pShowOpt
, pStringOpt
, OutputOptions(..)
, StringOutputStyle(..)
, defaultOutputOptionsDarkBg
, defaultOutputOptionsLightBg
, defaultOutputOptionsNoColor
, CheckColorTty(..)
, defaultColorOptionsDarkBg
, defaultColorOptionsLightBg
, ColorOptions(..)
, Style(..)
, Color(..)
, Intensity(..)
, colorNull
) where
#if __GLASGOW_HASKELL__ < 710
import Control.Applicative
#endif
import Control.Monad.IO.Class (MonadIO, liftIO)
import Data.Text.Lazy (Text)
import Prettyprinter (SimpleDocStream)
import Prettyprinter.Render.Terminal
(Color (..), Intensity(Vivid,Dull), AnsiStyle,
renderLazy, renderIO)
import System.IO (Handle, stdout)
import Text.Pretty.Simple.Internal
(ColorOptions(..), Style(..), CheckColorTty(..),
OutputOptions(..), StringOutputStyle(..),
convertStyle, colorNull,
defaultColorOptionsDarkBg, defaultColorOptionsLightBg,
defaultOutputOptionsDarkBg, defaultOutputOptionsLightBg,
defaultOutputOptionsNoColor, hCheckTTY, layoutString)
pPrint :: (MonadIO m, Show a) => a -> m ()
pPrint :: a -> m ()
pPrint = CheckColorTty -> OutputOptions -> a -> m ()
forall (m :: * -> *) a.
(MonadIO m, Show a) =>
CheckColorTty -> OutputOptions -> a -> m ()
pPrintOpt CheckColorTty
CheckColorTty OutputOptions
defaultOutputOptionsDarkBg
pHPrint :: (MonadIO m, Show a) => Handle -> a -> m ()
pHPrint :: Handle -> a -> m ()
pHPrint = CheckColorTty -> OutputOptions -> Handle -> a -> m ()
forall (m :: * -> *) a.
(MonadIO m, Show a) =>
CheckColorTty -> OutputOptions -> Handle -> a -> m ()
pHPrintOpt CheckColorTty
CheckColorTty OutputOptions
defaultOutputOptionsDarkBg
pPrintString :: MonadIO m => String -> m ()
pPrintString :: String -> m ()
pPrintString = CheckColorTty -> OutputOptions -> String -> m ()
forall (m :: * -> *).
MonadIO m =>
CheckColorTty -> OutputOptions -> String -> m ()
pPrintStringOpt CheckColorTty
CheckColorTty OutputOptions
defaultOutputOptionsDarkBg
pHPrintString :: MonadIO m => Handle -> String -> m ()
pHPrintString :: Handle -> String -> m ()
pHPrintString = CheckColorTty -> OutputOptions -> Handle -> String -> m ()
forall (m :: * -> *).
MonadIO m =>
CheckColorTty -> OutputOptions -> Handle -> String -> m ()
pHPrintStringOpt CheckColorTty
CheckColorTty OutputOptions
defaultOutputOptionsDarkBg
pPrintForceColor :: (MonadIO m, Show a) => a -> m ()
pPrintForceColor :: a -> m ()
pPrintForceColor = CheckColorTty -> OutputOptions -> a -> m ()
forall (m :: * -> *) a.
(MonadIO m, Show a) =>
CheckColorTty -> OutputOptions -> a -> m ()
pPrintOpt CheckColorTty
NoCheckColorTty OutputOptions
defaultOutputOptionsDarkBg
pHPrintForceColor :: (MonadIO m, Show a) => Handle -> a -> m ()
pHPrintForceColor :: Handle -> a -> m ()
pHPrintForceColor = CheckColorTty -> OutputOptions -> Handle -> a -> m ()
forall (m :: * -> *) a.
(MonadIO m, Show a) =>
CheckColorTty -> OutputOptions -> Handle -> a -> m ()
pHPrintOpt CheckColorTty
NoCheckColorTty OutputOptions
defaultOutputOptionsDarkBg
pPrintStringForceColor :: MonadIO m => String -> m ()
pPrintStringForceColor :: String -> m ()
pPrintStringForceColor = CheckColorTty -> OutputOptions -> String -> m ()
forall (m :: * -> *).
MonadIO m =>
CheckColorTty -> OutputOptions -> String -> m ()
pPrintStringOpt CheckColorTty
NoCheckColorTty OutputOptions
defaultOutputOptionsDarkBg
pHPrintStringForceColor :: MonadIO m => Handle -> String -> m ()
pHPrintStringForceColor :: Handle -> String -> m ()
pHPrintStringForceColor = CheckColorTty -> OutputOptions -> Handle -> String -> m ()
forall (m :: * -> *).
MonadIO m =>
CheckColorTty -> OutputOptions -> Handle -> String -> m ()
pHPrintStringOpt CheckColorTty
NoCheckColorTty OutputOptions
defaultOutputOptionsDarkBg
pShow :: Show a => a -> Text
pShow :: a -> Text
pShow = OutputOptions -> a -> Text
forall a. Show a => OutputOptions -> a -> Text
pShowOpt OutputOptions
defaultOutputOptionsDarkBg
pString :: String -> Text
pString :: String -> Text
pString = OutputOptions -> String -> Text
pStringOpt OutputOptions
defaultOutputOptionsDarkBg
pPrintDarkBg :: (MonadIO m, Show a) => a -> m ()
pPrintDarkBg :: a -> m ()
pPrintDarkBg = a -> m ()
forall (m :: * -> *) a. (MonadIO m, Show a) => a -> m ()
pPrint
pHPrintDarkBg :: (MonadIO m, Show a) => Handle -> a -> m ()
pHPrintDarkBg :: Handle -> a -> m ()
pHPrintDarkBg = Handle -> a -> m ()
forall (m :: * -> *) a. (MonadIO m, Show a) => Handle -> a -> m ()
pHPrint
pPrintStringDarkBg :: MonadIO m => String -> m ()
pPrintStringDarkBg :: String -> m ()
pPrintStringDarkBg = String -> m ()
forall (m :: * -> *). MonadIO m => String -> m ()
pPrintString
pHPrintStringDarkBg :: MonadIO m => Handle -> String -> m ()
pHPrintStringDarkBg :: Handle -> String -> m ()
pHPrintStringDarkBg = Handle -> String -> m ()
forall (m :: * -> *). MonadIO m => Handle -> String -> m ()
pHPrintString
pPrintForceColorDarkBg :: (MonadIO m, Show a) => a -> m ()
pPrintForceColorDarkBg :: a -> m ()
pPrintForceColorDarkBg = a -> m ()
forall (m :: * -> *) a. (MonadIO m, Show a) => a -> m ()
pPrintForceColor
pHPrintForceColorDarkBg :: (MonadIO m, Show a) => Handle -> a -> m ()
pHPrintForceColorDarkBg :: Handle -> a -> m ()
pHPrintForceColorDarkBg = Handle -> a -> m ()
forall (m :: * -> *) a. (MonadIO m, Show a) => Handle -> a -> m ()
pHPrintForceColor
pPrintStringForceColorDarkBg :: MonadIO m => String -> m ()
pPrintStringForceColorDarkBg :: String -> m ()
pPrintStringForceColorDarkBg = String -> m ()
forall (m :: * -> *). MonadIO m => String -> m ()
pPrintStringForceColor
pHPrintStringForceColorDarkBg :: MonadIO m => Handle -> String -> m ()
pHPrintStringForceColorDarkBg :: Handle -> String -> m ()
pHPrintStringForceColorDarkBg = Handle -> String -> m ()
forall (m :: * -> *). MonadIO m => Handle -> String -> m ()
pHPrintStringForceColor
pShowDarkBg :: Show a => a -> Text
pShowDarkBg :: a -> Text
pShowDarkBg = a -> Text
forall a. Show a => a -> Text
pShow
pStringDarkBg :: String -> Text
pStringDarkBg :: String -> Text
pStringDarkBg = String -> Text
pString
pPrintLightBg :: (MonadIO m, Show a) => a -> m ()
pPrintLightBg :: a -> m ()
pPrintLightBg = CheckColorTty -> OutputOptions -> a -> m ()
forall (m :: * -> *) a.
(MonadIO m, Show a) =>
CheckColorTty -> OutputOptions -> a -> m ()
pPrintOpt CheckColorTty
CheckColorTty OutputOptions
defaultOutputOptionsLightBg
pHPrintLightBg :: (MonadIO m, Show a) => Handle -> a -> m ()
pHPrintLightBg :: Handle -> a -> m ()
pHPrintLightBg = CheckColorTty -> OutputOptions -> Handle -> a -> m ()
forall (m :: * -> *) a.
(MonadIO m, Show a) =>
CheckColorTty -> OutputOptions -> Handle -> a -> m ()
pHPrintOpt CheckColorTty
CheckColorTty OutputOptions
defaultOutputOptionsLightBg
pPrintStringLightBg :: MonadIO m => String -> m ()
pPrintStringLightBg :: String -> m ()
pPrintStringLightBg = CheckColorTty -> OutputOptions -> String -> m ()
forall (m :: * -> *).
MonadIO m =>
CheckColorTty -> OutputOptions -> String -> m ()
pPrintStringOpt CheckColorTty
CheckColorTty OutputOptions
defaultOutputOptionsLightBg
pHPrintStringLightBg :: MonadIO m => Handle -> String -> m ()
pHPrintStringLightBg :: Handle -> String -> m ()
pHPrintStringLightBg = CheckColorTty -> OutputOptions -> Handle -> String -> m ()
forall (m :: * -> *).
MonadIO m =>
CheckColorTty -> OutputOptions -> Handle -> String -> m ()
pHPrintStringOpt CheckColorTty
CheckColorTty OutputOptions
defaultOutputOptionsLightBg
pPrintForceColorLightBg :: (MonadIO m, Show a) => a -> m ()
pPrintForceColorLightBg :: a -> m ()
pPrintForceColorLightBg = CheckColorTty -> OutputOptions -> a -> m ()
forall (m :: * -> *) a.
(MonadIO m, Show a) =>
CheckColorTty -> OutputOptions -> a -> m ()
pPrintOpt CheckColorTty
NoCheckColorTty OutputOptions
defaultOutputOptionsLightBg
pHPrintForceColorLightBg :: (MonadIO m, Show a) => Handle -> a -> m ()
pHPrintForceColorLightBg :: Handle -> a -> m ()
pHPrintForceColorLightBg = CheckColorTty -> OutputOptions -> Handle -> a -> m ()
forall (m :: * -> *) a.
(MonadIO m, Show a) =>
CheckColorTty -> OutputOptions -> Handle -> a -> m ()
pHPrintOpt CheckColorTty
NoCheckColorTty OutputOptions
defaultOutputOptionsLightBg
pPrintStringForceColorLightBg :: MonadIO m => String -> m ()
pPrintStringForceColorLightBg :: String -> m ()
pPrintStringForceColorLightBg = CheckColorTty -> OutputOptions -> String -> m ()
forall (m :: * -> *).
MonadIO m =>
CheckColorTty -> OutputOptions -> String -> m ()
pPrintStringOpt CheckColorTty
NoCheckColorTty OutputOptions
defaultOutputOptionsLightBg
pHPrintStringForceColorLightBg :: MonadIO m => Handle -> String -> m ()
pHPrintStringForceColorLightBg :: Handle -> String -> m ()
pHPrintStringForceColorLightBg = CheckColorTty -> OutputOptions -> Handle -> String -> m ()
forall (m :: * -> *).
MonadIO m =>
CheckColorTty -> OutputOptions -> Handle -> String -> m ()
pHPrintStringOpt CheckColorTty
NoCheckColorTty OutputOptions
defaultOutputOptionsLightBg
pShowLightBg :: Show a => a -> Text
pShowLightBg :: a -> Text
pShowLightBg = OutputOptions -> a -> Text
forall a. Show a => OutputOptions -> a -> Text
pShowOpt OutputOptions
defaultOutputOptionsLightBg
pStringLightBg :: String -> Text
pStringLightBg :: String -> Text
pStringLightBg = OutputOptions -> String -> Text
pStringOpt OutputOptions
defaultOutputOptionsLightBg
pPrintNoColor :: (MonadIO m, Show a) => a -> m ()
pPrintNoColor :: a -> m ()
pPrintNoColor = CheckColorTty -> OutputOptions -> a -> m ()
forall (m :: * -> *) a.
(MonadIO m, Show a) =>
CheckColorTty -> OutputOptions -> a -> m ()
pPrintOpt CheckColorTty
NoCheckColorTty OutputOptions
defaultOutputOptionsNoColor
pHPrintNoColor :: (MonadIO m, Show a) => Handle -> a -> m ()
pHPrintNoColor :: Handle -> a -> m ()
pHPrintNoColor = CheckColorTty -> OutputOptions -> Handle -> a -> m ()
forall (m :: * -> *) a.
(MonadIO m, Show a) =>
CheckColorTty -> OutputOptions -> Handle -> a -> m ()
pHPrintOpt CheckColorTty
NoCheckColorTty OutputOptions
defaultOutputOptionsNoColor
pPrintStringNoColor :: MonadIO m => String -> m ()
pPrintStringNoColor :: String -> m ()
pPrintStringNoColor = CheckColorTty -> OutputOptions -> String -> m ()
forall (m :: * -> *).
MonadIO m =>
CheckColorTty -> OutputOptions -> String -> m ()
pPrintStringOpt CheckColorTty
NoCheckColorTty OutputOptions
defaultOutputOptionsNoColor
pHPrintStringNoColor :: MonadIO m => Handle -> String -> m ()
pHPrintStringNoColor :: Handle -> String -> m ()
pHPrintStringNoColor = CheckColorTty -> OutputOptions -> Handle -> String -> m ()
forall (m :: * -> *).
MonadIO m =>
CheckColorTty -> OutputOptions -> Handle -> String -> m ()
pHPrintStringOpt CheckColorTty
NoCheckColorTty OutputOptions
defaultOutputOptionsNoColor
pShowNoColor :: Show a => a -> Text
pShowNoColor :: a -> Text
pShowNoColor = OutputOptions -> a -> Text
forall a. Show a => OutputOptions -> a -> Text
pShowOpt OutputOptions
defaultOutputOptionsNoColor
pStringNoColor :: String -> Text
pStringNoColor :: String -> Text
pStringNoColor = OutputOptions -> String -> Text
pStringOpt OutputOptions
defaultOutputOptionsNoColor
pPrintOpt :: (MonadIO m, Show a) => CheckColorTty -> OutputOptions -> a -> m ()
pPrintOpt :: CheckColorTty -> OutputOptions -> a -> m ()
pPrintOpt CheckColorTty
checkColorTty OutputOptions
outputOptions =
CheckColorTty -> OutputOptions -> Handle -> a -> m ()
forall (m :: * -> *) a.
(MonadIO m, Show a) =>
CheckColorTty -> OutputOptions -> Handle -> a -> m ()
pHPrintOpt CheckColorTty
checkColorTty OutputOptions
outputOptions Handle
stdout
pHPrintOpt ::
(MonadIO m, Show a)
=> CheckColorTty
-> OutputOptions
-> Handle
-> a
-> m ()
pHPrintOpt :: CheckColorTty -> OutputOptions -> Handle -> a -> m ()
pHPrintOpt CheckColorTty
checkColorTty OutputOptions
outputOptions Handle
handle a
a =
CheckColorTty -> OutputOptions -> Handle -> String -> m ()
forall (m :: * -> *).
MonadIO m =>
CheckColorTty -> OutputOptions -> Handle -> String -> m ()
pHPrintStringOpt CheckColorTty
checkColorTty OutputOptions
outputOptions Handle
handle (String -> m ()) -> String -> m ()
forall a b. (a -> b) -> a -> b
$ a -> String
forall a. Show a => a -> String
show a
a
pPrintStringOpt :: MonadIO m => CheckColorTty -> OutputOptions -> String -> m ()
pPrintStringOpt :: CheckColorTty -> OutputOptions -> String -> m ()
pPrintStringOpt CheckColorTty
checkColorTty OutputOptions
outputOptions =
CheckColorTty -> OutputOptions -> Handle -> String -> m ()
forall (m :: * -> *).
MonadIO m =>
CheckColorTty -> OutputOptions -> Handle -> String -> m ()
pHPrintStringOpt CheckColorTty
checkColorTty OutputOptions
outputOptions Handle
stdout
pHPrintStringOpt ::
MonadIO m
=> CheckColorTty
-> OutputOptions
-> Handle
-> String
-> m ()
pHPrintStringOpt :: CheckColorTty -> OutputOptions -> Handle -> String -> m ()
pHPrintStringOpt CheckColorTty
checkColorTty OutputOptions
outputOptions Handle
handle String
str = do
OutputOptions
realOutputOpts <-
case CheckColorTty
checkColorTty of
CheckColorTty
CheckColorTty -> Handle -> OutputOptions -> m OutputOptions
forall (m :: * -> *).
MonadIO m =>
Handle -> OutputOptions -> m OutputOptions
hCheckTTY Handle
handle OutputOptions
outputOptions
CheckColorTty
NoCheckColorTty -> OutputOptions -> m OutputOptions
forall (f :: * -> *) a. Applicative f => a -> f a
pure OutputOptions
outputOptions
IO () -> m ()
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO (IO () -> m ()) -> IO () -> m ()
forall a b. (a -> b) -> a -> b
$ do
Handle -> SimpleDocStream AnsiStyle -> IO ()
renderIO Handle
handle (SimpleDocStream AnsiStyle -> IO ())
-> SimpleDocStream AnsiStyle -> IO ()
forall a b. (a -> b) -> a -> b
$ OutputOptions -> String -> SimpleDocStream AnsiStyle
layoutStringAnsi OutputOptions
realOutputOpts String
str
String -> IO ()
putStrLn String
""
pShowOpt :: Show a => OutputOptions -> a -> Text
pShowOpt :: OutputOptions -> a -> Text
pShowOpt OutputOptions
outputOptions = OutputOptions -> String -> Text
pStringOpt OutputOptions
outputOptions (String -> Text) -> (a -> String) -> a -> Text
forall b c a. (b -> c) -> (a -> b) -> a -> c
. a -> String
forall a. Show a => a -> String
show
pStringOpt :: OutputOptions -> String -> Text
pStringOpt :: OutputOptions -> String -> Text
pStringOpt OutputOptions
outputOptions = SimpleDocStream AnsiStyle -> Text
renderLazy (SimpleDocStream AnsiStyle -> Text)
-> (String -> SimpleDocStream AnsiStyle) -> String -> Text
forall b c a. (b -> c) -> (a -> b) -> a -> c
. OutputOptions -> String -> SimpleDocStream AnsiStyle
layoutStringAnsi OutputOptions
outputOptions
layoutStringAnsi :: OutputOptions -> String -> SimpleDocStream AnsiStyle
layoutStringAnsi :: OutputOptions -> String -> SimpleDocStream AnsiStyle
layoutStringAnsi OutputOptions
opts = (Style -> AnsiStyle)
-> SimpleDocStream Style -> SimpleDocStream AnsiStyle
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap Style -> AnsiStyle
convertStyle (SimpleDocStream Style -> SimpleDocStream AnsiStyle)
-> (String -> SimpleDocStream Style)
-> String
-> SimpleDocStream AnsiStyle
forall b c a. (b -> c) -> (a -> b) -> a -> c
. OutputOptions -> String -> SimpleDocStream Style
layoutString OutputOptions
opts