{-# 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 :: 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, Show 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 :: ByteString -> Doc ann
prettyBytes ByteString
b = Doc ann
"#" Doc ann -> Doc ann -> Doc ann
forall a. Semigroup a => a -> a -> a
<> [Doc ann] -> Doc ann
forall (t :: * -> *) m. (Foldable t, Monoid m) => t m -> m
fold (Word8 -> Doc ann
forall ann. Word8 -> Doc ann
asBytes (Word8 -> Doc ann) -> [Word8] -> [Doc ann]
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> ByteString -> [Word8]
BS.unpack ByteString
b)