Skip to content

Commit 4097715

Browse files
Custom Prettyprinter
1 parent cc5f1fd commit 4097715

File tree

2 files changed

+21
-3
lines changed

2 files changed

+21
-3
lines changed

bot-plutus-interface.cabal

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,7 @@ library
105105
, directory
106106
, prettyprinter
107107
, either
108+
, vector
108109
, filepath
109110
, freer-extras
110111
, freer-simple

src/BotPlutusInterface/Contract.hs

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,14 +40,16 @@ import Control.Monad.Freer.Extras.Modify (raiseEnd)
4040
import Control.Monad.Freer.Writer (Writer (Tell))
4141
import Control.Monad.Trans.Class (lift)
4242
import Control.Monad.Trans.Either (EitherT, eitherT, firstEitherT, newEitherT)
43-
import Data.Aeson (ToJSON, Value)
43+
import Data.Aeson (ToJSON, Value (String, Number, Bool, Null, Array, Object))
4444
import Data.Aeson.Extras (encodeByteString)
4545
import Data.Either (fromRight)
4646
import Data.Kind (Type)
4747
import Data.Map qualified as Map
4848
import Data.Row (Row)
4949
import Data.Text (Text)
5050
import Data.Text qualified as Text
51+
import qualified Data.Vector as V
52+
import qualified Data.HashMap.Strict as HM
5153
import Ledger (POSIXTime)
5254
import Ledger qualified
5355
import Ledger.Address (PaymentPubKeyHash (PaymentPubKeyHash))
@@ -73,7 +75,7 @@ import PlutusTx.Builtins (fromBuiltin)
7375
import Wallet.Emulator.Error (WalletAPIError (..))
7476
import Prelude
7577
import Prettyprinter
76-
import Data.String (fromString)
78+
import qualified Prettyprinter as PP
7779

7880
runContract ::
7981
forall (w :: Type) (s :: Row Type) (e :: Type) (a :: Type).
@@ -100,7 +102,22 @@ handleContract contractEnv =
100102
. raiseEnd
101103

102104
instance Pretty Value where
103-
pretty = fromString . show
105+
pretty (String s) = pretty s
106+
pretty (Number n) = pretty $ show n
107+
pretty (Bool b) = pretty b
108+
pretty (Array arr) = PP.list $ pretty <$> V.toList arr
109+
pretty (Object obj) = PP.group
110+
. PP.encloseSep (PP.flatAlt "{ " "{") (PP.flatAlt " }" "}") ", "
111+
. map
112+
( \(k, v) ->
113+
PP.hang 2 $
114+
PP.sep
115+
[ pretty k <+> ": "
116+
, pretty v
117+
]
118+
)
119+
$ HM.toList obj
120+
pretty Null = "null"
104121

105122
handleWriter ::
106123
forall (w :: Type) (effs :: [Type -> Type]).

0 commit comments

Comments
 (0)