Profpatsch/users/Profpatsch/my-prelude/src/Debug.hs
 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
{-# LANGUAGE QuasiQuotes #-}

module Debug where

import Data.Text qualified as Text
import Data.Text.IO qualified as Text.IO
import Debug.Trace as Debug
import MyPrelude
import Pretty qualified

-- | 'Debug.trace' a showable value when (and only if!) it is evaluated, and pretty print it.
traceShowPretty :: (Show a) => a -> a
traceShowPretty a = Debug.trace (textToString $ Pretty.showPretty a) a

-- | 'Debug.trace' a showable value when (and only if!) it is evaluated, and pretty print it. In addition, the given prefix is put before the value for easier recognition.
traceShowPrettyPrefix :: (Show a) => Text -> a -> a
traceShowPrettyPrefix prefix a = Debug.trace ([fmt|{prefix}: {Pretty.showPretty a}|]) a

-- | Display non-printable characters as their unicode Control Pictures
-- https://en.wikipedia.org/wiki/Unicode_control_characters#Control_pictures
--
-- Not all implemented.
putStrLnShowNPr :: Text -> IO ()
putStrLnShowNPr t =
  Text.IO.putStrLn $
    -- newlines will actually print a newline for convenience
    t
      & Text.replace "\n" "␤\n"
      & Text.replace "\r\n" "␤\n"
      & Text.replace "\t" "␉"