import Control.Monad.IO.Class
import Control.Monad
import Data.Aeson
+import Data.Aeson.Encode.Pretty
import qualified Data.ByteString.Lazy.Char8 as B
import Data.Conduit.Parser
import Data.Maybe
if pred x
then do
- liftIO $ do
- setSGR [SetColor Foreground Vivid Magenta]
- putStrLn $ "<-- " ++ B.unpack (encodeMsg x)
+ shouldLog <- asks $ logMessages . config
+ liftIO $ when shouldLog $ do
+ setSGR [SetColor Foreground Dull Magenta]
+ putStrLn $ "<-- " ++ B.unpack (encodeMsgPretty x)
setSGR [Reset]
return x
else empty
message :: forall a. (Typeable a, FromJSON a) => Session a
message =
let parser = decode . encodeMsg :: FromServerMessage -> Maybe a
- in named (T.pack $ show $ head $ snd $ splitTyConApp $ last $ typeRepArgs $ typeOf parser) $ do
- x <- satisfy (isJust . parser)
- return $ castMsg x
+ in named (T.pack $ show $ head $ snd $ splitTyConApp $ last $ typeRepArgs $ typeOf parser) $
+ castMsg <$> satisfy (isJust . parser)
-- | Matches if the message is a notification.
anyNotification :: Session FromServerMessage
encodeMsg :: FromServerMessage -> B.ByteString
encodeMsg = encode . genericToJSON (defaultOptions { sumEncoding = UntaggedValue })
+encodeMsgPretty :: FromServerMessage -> B.ByteString
+encodeMsgPretty = encodePretty . genericToJSON (defaultOptions { sumEncoding = UntaggedValue })
+
-- | Matches if the message is a log message notification or a show message notification/request.
loggingNotification :: Session FromServerMessage
loggingNotification = named "Logging notification" $ satisfy shouldSkip