1 module Hcompta.CLI.Write where
3 import Control.Monad (when)
4 import qualified System.IO as IO
5 import qualified System.Console.ANSI as ANSI
6 import System.Exit (exitWith, ExitCode(..))
8 import qualified Hcompta.CLI.Context as Context
9 import Hcompta.CLI.Context (Context)
11 with_color :: Context -> IO.Handle -> IO Bool
12 with_color context h =
13 case Context.color context of
14 Nothing -> IO.hIsTerminalDevice h
17 error :: Context -> String -> IO ()
18 error context msg = do
19 color <- with_color context IO.stderr
21 ANSI.hSetSGR IO.stderr [ANSI.SetColor ANSI.Foreground ANSI.Dull ANSI.Red]
22 IO.hPutStr IO.stderr "ERROR"
24 ANSI.hSetSGR IO.stderr [ANSI.Reset]
25 IO.hPutStr IO.stderr $ concat [": ", msg, "\n"]
27 fatal :: Context -> String -> IO a
28 fatal context msg = do
29 Hcompta.CLI.Write.error context msg
30 exitWith $ ExitFailure 1