module Rob.Logger where

import System.Console.ANSI
import Data.Time.Clock (getCurrentTime, UTCTime)
import Data.Time.Format (formatTime, defaultTimeLocale)

-- | Print a message highlighting it
log' :: Color -> ColorIntensity -> String -> IO()
log' :: Color -> ColorIntensity -> String -> IO ()
log' Color
color ColorIntensity
intensity String
message = do
  [SGR] -> IO ()
setSGR [ConsoleLayer -> ColorIntensity -> Color -> SGR
SetColor ConsoleLayer
Foreground ColorIntensity
Dull Color
White]
  UTCTime
time <- IO UTCTime
getCurrentTime
  String -> IO ()
print' (String -> IO ()) -> String -> IO ()
forall a b. (a -> b) -> a -> b
$ UTCTime -> String
formatTime' UTCTime
time
  [SGR] -> IO ()
setSGR [SGR
Reset]
  [SGR] -> IO ()
setSGR [ConsoleLayer -> ColorIntensity -> Color -> SGR
SetColor ConsoleLayer
Foreground ColorIntensity
intensity Color
color]
  String -> IO ()
print' (String -> IO ()) -> String -> IO ()
forall a b. (a -> b) -> a -> b
$ String
message String -> String -> String
forall a. [a] -> [a] -> [a]
++ String
"\n"
  [SGR] -> IO ()
setSGR [SGR
Reset]

-- | Format the time, showing it into the log message
formatTime' :: UTCTime -> String
formatTime' :: UTCTime -> String
formatTime' = TimeLocale -> String -> UTCTime -> String
forall t. FormatTime t => TimeLocale -> String -> t -> String
formatTime TimeLocale
defaultTimeLocale String
"[%T] "

-- | Print a message in the console
print' :: String -> IO()
print' :: String -> IO ()
print' = String -> IO ()
putStr

-- | Log success messages
success :: String -> IO()
success :: String -> IO ()
success = Color -> ColorIntensity -> String -> IO ()
log' Color
Green ColorIntensity
Dull

-- | Log error messages
err :: String -> IO()
err :: String -> IO ()
err = Color -> ColorIntensity -> String -> IO ()
log' Color
Red ColorIntensity
Vivid

-- | Log info messages
info :: String -> IO()
info :: String -> IO ()
info = Color -> ColorIntensity -> String -> IO ()
log' Color
Cyan ColorIntensity
Dull

-- | Log warning messages
warning :: String -> IO()
warning :: String -> IO ()
warning = Color -> ColorIntensity -> String -> IO ()
log' Color
Yellow ColorIntensity
Dull

-- | Log a raw message without using colors
raw :: String -> IO()
raw :: String -> IO ()
raw = String -> IO ()
print'

-- | Execute log on a list of messages
flatten :: Monad m => (a -> m b) -> [a] -> m ()
flatten :: (a -> m b) -> [a] -> m ()
flatten = (a -> m b) -> [a] -> m ()
forall (t :: * -> *) (m :: * -> *) a b.
(Foldable t, Monad m) =>
(a -> m b) -> t a -> m ()
mapM_