{-# LANGUAGE OverloadedStrings #-}

module PlutusCore.Pretty.Utils
    ( prettyBytes
    ) where

import PlutusPrelude

import Data.ByteString qualified as BS
import Data.Text qualified as T
import Numeric (showHex)
import Prettyprinter.Internal

asBytes :: Word8 -> Doc ann
asBytes :: forall ann. Word8 -> Doc ann
asBytes Word8
x = Int -> Text -> Doc ann
forall ann. Int -> Text -> Doc ann
Text Int
2 (Text -> Doc ann) -> Text -> Doc ann
forall a b. (a -> b) -> a -> b
$ String -> Text
T.pack (String -> Text) -> String -> Text
forall a b. (a -> b) -> a -> b
$ String -> String
addLeadingZero (String -> String) -> String -> String
forall a b. (a -> b) -> a -> b
$ Word8 -> String -> String
forall a. Integral a => a -> String -> String
showHex Word8
x String
forall a. Monoid a => a
mempty
    where addLeadingZero :: String -> String
          addLeadingZero :: String -> String
addLeadingZero
              | Word8
x Word8 -> Word8 -> Bool
forall a. Ord a => a -> a -> Bool
< Word8
16    = (Char
'0' Char -> String -> String
forall a. a -> [a] -> [a]
:)
              | Bool
otherwise = String -> String
forall a. a -> a
id

prettyBytes :: BS.ByteString -> Doc ann
prettyBytes :: forall ann. ByteString -> Doc ann
prettyBytes ByteString
b = Doc ann
"#" Doc ann -> Doc ann -> Doc ann
forall a. Semigroup a => a -> a -> a
<> (Word8 -> Doc ann) -> [Word8] -> Doc ann
forall m a. Monoid m => (a -> m) -> [a] -> m
forall (t :: * -> *) m a.
(Foldable t, Monoid m) =>
(a -> m) -> t a -> m
foldMap Word8 -> Doc ann
forall ann. Word8 -> Doc ann
asBytes (ByteString -> [Word8]
BS.unpack ByteString
b)