module Terminal ( hPutBlueStr, hPutRedStr, putGreenLn ) where import Control.Monad.IO.Class (MonadIO(..)) import System.Console.ANSI ( SGR( SetColor ), Color(..), ColorIntensity( Vivid ), ConsoleLayer( Foreground ), setSGR ) import System.IO ( Handle, hPutStr ) -- | Perform a computation (anything in MonadIO) with the given -- graphics mode(s) enabled. Revert to the previous graphics mode -- after the computation has finished. with_sgr :: (MonadIO m) => [SGR] -> m a -> m a with_sgr sgrs computation = do liftIO $ setSGR sgrs x <- computation liftIO $ setSGR [] return x -- | Perform a computation (anything in MonadIO) with the terminal -- output set to a certain color. Reset to the default color after -- the computation has finished. with_color :: (MonadIO m) => Color -> m a -> m a with_color color = with_sgr [SetColor Foreground Vivid color] -- | Output the given line to the given handle, in red. The silly -- camelCase name is for consistency with e.g. hPutStrLn. hPutRedStr :: Handle -> String -> IO () hPutRedStr h = with_color Red . hPutStr h -- | Output the given line to the given handle, in blue. The silly -- camelCase name is for consistency with e.g. hPutStrLn. hPutBlueStr :: Handle -> String -> IO () hPutBlueStr h = with_color Blue . hPutStr h -- | Output the given line to stdout, in green. The silly camelCase -- name is for consistency with e.g. putStrLn. putGreenLn :: String -> IO () putGreenLn = with_color Green . putStrLn