1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
--- ----------------------------------------------------------------------------
--- This module defines some auxiliary functions for intermediate output.
---
--- @author  Björn Peemöller
--- @version September 2015
--- ----------------------------------------------------------------------------
module Output where

import IO        (hPutStrLn, stderr)
import List      (intercalate)
import System    (exitWith)
import Unsafe    (unsafePerformIO)

import PevalOpts (Options (..), ColorMode (..), Verbosity (..))

--- Trace details in pure computations.
traceDetail :: Options -> String -> a -> a
traceDetail o m x = unsafePerformIO (detail o m >> return x)

--- Trace debug information in pure computations.
traceDebug :: Options -> String -> a -> a
traceDebug o m x = unsafePerformIO (debug o m >> return x)

--- Print status information.
status :: Options -> String -> IO ()
status opts msg = when (optVerbosity opts >= Status) (putStrLn msg)

--- Print additional information.
info :: Options -> String -> IO ()
info opts msg = when (optVerbosity opts >= Info) (putStrLn msg)

--- Print detailed information.
detail :: Options -> String -> IO ()
detail opts msg = when (optVerbosity opts >= Detail) (putStrLn msg)

--- Print debug information.
debug :: Options -> String -> IO ()
debug opts msg = when (optVerbosity opts >= Debug) (putStrLn msg)

--- Assert the truth of the first value or throw an error with the second
--- argument as the information describing the failed assertion.
assert :: Bool -> String -> a -> a
assert b m x = if b then x
                    else unsafePerformIO (hPutStrLn stderr err >> exitWith 1)
  where err = "Assertion failed: " ++ m

--- Conditionally color the string, based on the given options.
colorWith :: Options -> (String -> String) -> String -> String
colorWith opts color s = if optColorMode opts == CMAlways then color s else s