{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE QuasiQuotes #-}
{-# LANGUAGE OverloadedStrings #-}

-- | The silent formatter does nothing except print the test root folder path, if present.
--
-- This is provided as an explicit formatter so it can print that single line. If you don't want anything at all to be printed, you can just run with no formatters.
--
-- Documentation can be found <https://codedownio.github.io/sandwich/docs/formatters/silent here>.

module Test.Sandwich.Formatters.Silent (
  defaultSilentFormatter

  -- * Options
  , silentFormatterPrintRunRoot
  ) where

import Control.Monad.IO.Class
import Data.String.Interpolate
import Test.Sandwich.Types.RunTree
import Test.Sandwich.Util


data SilentFormatter = SilentFormatter {
  SilentFormatter -> Bool
silentFormatterPrintRunRoot :: Bool
  } deriving (Int -> SilentFormatter -> ShowS
[SilentFormatter] -> ShowS
SilentFormatter -> String
(Int -> SilentFormatter -> ShowS)
-> (SilentFormatter -> String)
-> ([SilentFormatter] -> ShowS)
-> Show SilentFormatter
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [SilentFormatter] -> ShowS
$cshowList :: [SilentFormatter] -> ShowS
show :: SilentFormatter -> String
$cshow :: SilentFormatter -> String
showsPrec :: Int -> SilentFormatter -> ShowS
$cshowsPrec :: Int -> SilentFormatter -> ShowS
Show)

defaultSilentFormatter :: SilentFormatter
defaultSilentFormatter :: SilentFormatter
defaultSilentFormatter = SilentFormatter :: Bool -> SilentFormatter
SilentFormatter {
  silentFormatterPrintRunRoot :: Bool
silentFormatterPrintRunRoot = Bool
True
  }

instance Formatter SilentFormatter where
  formatterName :: SilentFormatter -> String
formatterName SilentFormatter
_ = String
"silent-formatter"
  runFormatter :: SilentFormatter
-> [RunNode BaseContext]
-> Maybe (CommandLineOptions ())
-> BaseContext
-> m ()
runFormatter SilentFormatter
_ [RunNode BaseContext]
_ Maybe (CommandLineOptions ())
_ BaseContext
bc =
    Maybe String -> (String -> m ()) -> m ()
forall (m :: * -> *) a b. Monad m => Maybe a -> (a -> m b) -> m ()
whenJust (BaseContext -> Maybe String
baseContextRunRoot BaseContext
bc) ((String -> m ()) -> m ()) -> (String -> m ()) -> m ()
forall a b. (a -> b) -> a -> b
$ \String
runRoot ->
      IO () -> m ()
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO (IO () -> m ()) -> IO () -> m ()
forall a b. (a -> b) -> a -> b
$ String -> IO ()
putStrLn [i|Run root: #{runRoot}\n|]
  finalizeFormatter :: SilentFormatter -> [RunNode BaseContext] -> BaseContext -> m ()
finalizeFormatter SilentFormatter
_ [RunNode BaseContext]
_ BaseContext
_ = () -> m ()
forall (m :: * -> *) a. Monad m => a -> m a
return ()