-- | Utility functions for printing

module Test.Sandwich.Formatters.Print.Printing where

import Control.Monad
import Control.Monad.IO.Class
import Control.Monad.Reader
import qualified Data.List as L
import System.Console.ANSI
import System.IO
import Test.Sandwich.Formatters.Print.Types
import Test.Sandwich.Util


-- * Printing functions for indented, colored, and with newline

pi :: String -> m ()
pi String
msg = forall {m :: * -> *}.
(MonadReader (PrintFormatter, Int, Handle) m, MonadIO m) =>
Maybe SGR -> String -> m ()
printIndentedWithColor forall a. Maybe a
Nothing String
msg
pic :: Colour Float -> String -> m ()
pic Colour Float
color String
msg = forall {m :: * -> *}.
(MonadReader (PrintFormatter, Int, Handle) m, MonadIO m) =>
Maybe SGR -> String -> m ()
printIndentedWithColor (forall a. a -> Maybe a
Just (ConsoleLayer -> Colour Float -> SGR
SetRGBColor ConsoleLayer
Foreground Colour Float
color)) String
msg
pin :: String -> m ()
pin String
msg = forall {m :: * -> *}.
(MonadReader (PrintFormatter, Int, Handle) m, MonadIO m) =>
Maybe SGR -> String -> m ()
printIndentedWithColor forall a. Maybe a
Nothing (String
msg forall a. Semigroup a => a -> a -> a
<> String
"\n")
picn :: Colour Float -> String -> m ()
picn Colour Float
color String
msg = forall {m :: * -> *}.
(MonadReader (PrintFormatter, Int, Handle) m, MonadIO m) =>
Maybe SGR -> String -> m ()
printIndentedWithColor (forall a. a -> Maybe a
Just (ConsoleLayer -> Colour Float -> SGR
SetRGBColor ConsoleLayer
Foreground Colour Float
color)) (String
msg forall a. Semigroup a => a -> a -> a
<> String
"\n")

p :: String -> m ()
p String
msg = forall {m :: * -> *} {b}.
(MonadReader (PrintFormatter, b, Handle) m, MonadIO m) =>
Maybe SGR -> String -> m ()
printWithColor forall a. Maybe a
Nothing String
msg
pc :: Colour Float -> String -> m ()
pc Colour Float
color String
msg = forall {m :: * -> *} {b}.
(MonadReader (PrintFormatter, b, Handle) m, MonadIO m) =>
Maybe SGR -> String -> m ()
printWithColor (forall a. a -> Maybe a
Just (ConsoleLayer -> Colour Float -> SGR
SetRGBColor ConsoleLayer
Foreground Colour Float
color)) String
msg
pn :: String -> m ()
pn String
msg = forall {m :: * -> *} {b}.
(MonadReader (PrintFormatter, b, Handle) m, MonadIO m) =>
Maybe SGR -> String -> m ()
printWithColor forall a. Maybe a
Nothing (String
msg forall a. Semigroup a => a -> a -> a
<> String
"\n")
pcn :: Colour Float -> String -> m ()
pcn Colour Float
color String
msg = forall {m :: * -> *} {b}.
(MonadReader (PrintFormatter, b, Handle) m, MonadIO m) =>
Maybe SGR -> String -> m ()
printWithColor (forall a. a -> Maybe a
Just (ConsoleLayer -> Colour Float -> SGR
SetRGBColor ConsoleLayer
Foreground Colour Float
color)) (String
msg forall a. Semigroup a => a -> a -> a
<> String
"\n")

pGreenLn :: String -> m ()
pGreenLn String
msg = forall {m :: * -> *}.
(MonadReader (PrintFormatter, Int, Handle) m, MonadIO m) =>
Maybe SGR -> String -> m ()
printIndentedWithColor (forall a. a -> Maybe a
Just (ConsoleLayer -> ColorIntensity -> Color -> SGR
SetColor ConsoleLayer
Foreground ColorIntensity
Dull Color
Green)) (String
msg forall a. Semigroup a => a -> a -> a
<> String
"\n")
pYellowLn :: String -> m ()
pYellowLn String
msg = forall {m :: * -> *}.
(MonadReader (PrintFormatter, Int, Handle) m, MonadIO m) =>
Maybe SGR -> String -> m ()
printIndentedWithColor (forall a. a -> Maybe a
Just (ConsoleLayer -> ColorIntensity -> Color -> SGR
SetColor ConsoleLayer
Foreground ColorIntensity
Dull Color
Yellow)) (String
msg forall a. Semigroup a => a -> a -> a
<> String
"\n")
pRedLn :: String -> m ()
pRedLn String
msg = forall {m :: * -> *}.
(MonadReader (PrintFormatter, Int, Handle) m, MonadIO m) =>
Maybe SGR -> String -> m ()
printIndentedWithColor (forall a. a -> Maybe a
Just (ConsoleLayer -> ColorIntensity -> Color -> SGR
SetColor ConsoleLayer
Foreground ColorIntensity
Dull Color
Red)) (String
msg forall a. Semigroup a => a -> a -> a
<> String
"\n") -- Tried solarizedRed here but it was too orange

printIndentedWithColor :: Maybe SGR -> String -> m ()
printIndentedWithColor Maybe SGR
maybeColor String
msg = do
  (PrintFormatter {}, Int
indent, Handle
h) <- forall r (m :: * -> *). MonadReader r m => m r
ask
  forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO forall a b. (a -> b) -> a -> b
$ Handle -> String -> IO ()
hPutStr Handle
h forall a b. (a -> b) -> a -> b
$ forall a. Int -> a -> [a]
L.replicate Int
indent Char
' '
  forall {m :: * -> *} {b}.
(MonadReader (PrintFormatter, b, Handle) m, MonadIO m) =>
Maybe SGR -> String -> m ()
printWithColor Maybe SGR
maybeColor String
msg

printWithColor :: Maybe SGR -> String -> m ()
printWithColor Maybe SGR
maybeColor String
msg = do
  (PrintFormatter {Bool
printFormatterUseColor :: PrintFormatter -> Bool
printFormatterUseColor :: Bool
printFormatterUseColor}, b
_, Handle
h) <- forall r (m :: * -> *). MonadReader r m => m r
ask
  forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
when (Bool
printFormatterUseColor) forall a b. (a -> b) -> a -> b
$ forall (m :: * -> *) a b. Monad m => Maybe a -> (a -> m b) -> m ()
whenJust Maybe SGR
maybeColor forall a b. (a -> b) -> a -> b
$ \SGR
color -> forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO forall a b. (a -> b) -> a -> b
$ [SGR] -> IO ()
setSGR [SGR
color]
  forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO forall a b. (a -> b) -> a -> b
$ Handle -> String -> IO ()
hPutStr Handle
h String
msg
  forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
when (Bool
printFormatterUseColor) forall a b. (a -> b) -> a -> b
$ forall (m :: * -> *) a b. Monad m => Maybe a -> (a -> m b) -> m ()
whenJust Maybe SGR
maybeColor forall a b. (a -> b) -> a -> b
$ \SGR
_ -> forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO forall a b. (a -> b) -> a -> b
$ [SGR] -> IO ()
setSGR [SGR
Reset]