module Hcompta.CLI.Write where import Control.Monad (when) import qualified System.IO as IO import qualified System.Console.ANSI as ANSI import System.Exit (exitWith, ExitCode(..)) import qualified Hcompta.CLI.Context as Context import Hcompta.CLI.Context (Context) with_color :: Context -> IO.Handle -> IO Bool with_color context h = case Context.color context of Nothing -> IO.hIsTerminalDevice h Just b -> return b error :: Context -> String -> IO () error context msg = do color <- with_color context IO.stderr when color $ ANSI.hSetSGR IO.stderr [ANSI.SetColor ANSI.Foreground ANSI.Dull ANSI.Red] IO.hPutStr IO.stderr "ERROR" when color $ ANSI.hSetSGR IO.stderr [ANSI.Reset] IO.hPutStr IO.stderr $ concat [": ", msg, "\n"] fatal :: Context -> String -> IO a fatal context msg = do Hcompta.CLI.Write.error context msg exitWith $ ExitFailure 1