module Terminal ( hPutRedLn, putGreenLn) where import Control.Monad.IO.Class (MonadIO(..)) import System.Console.ANSI ( SGR( SetColor ), Color(..), ColorIntensity( Vivid ), ConsoleLayer( Foreground ), setSGR ) import System.IO ( Handle, hPutStrLn ) -- | 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. hPutRedLn :: Handle -> String -> IO () hPutRedLn h = with_color Red . hPutStrLn h -- | Output the given line to stdout. The silly camelCase name is for -- consistency with e.g. putStrLn. putGreenLn :: String -> IO () putGreenLn = with_color Green . putStrLn