blockfrost-pretty (empty) → 0.1.0.0
raw patch · 11 files changed
+378/−0 lines, 11 filesdep +basedep +blockfrost-apidep +data-defaultsetup-changed
Dependencies added: base, blockfrost-api, data-default, lens, prettyprinter, prettyprinter-ansi-terminal, safe-money, text, time
Files
- CHANGELOG.md +10/−0
- LICENSE +13/−0
- README.md +36/−0
- Setup.hs +2/−0
- blockfrost-pretty.cabal +58/−0
- src/Blockfrost/Pretty.hs +15/−0
- src/Blockfrost/Pretty/Ada.hs +60/−0
- src/Blockfrost/Pretty/Block.hs +56/−0
- src/Blockfrost/Pretty/Config.hs +63/−0
- src/Blockfrost/Pretty/POSIXTime.hs +28/−0
- src/Blockfrost/Pretty/Shared.hs +37/−0
+ CHANGELOG.md view
@@ -0,0 +1,10 @@+# Version [0.1.0.0](https://github.com/blockfrost/blockfrost-haskell/compare/initial...0.1.0.0) (2021-09-14)++* Initial release++---++`blockfrost-pretty` uses [PVP Versioning][1].++[1]: https://pvp.haskell.org+
+ LICENSE view
@@ -0,0 +1,13 @@+Copyright 2021 blockfrost.io++Licensed under the Apache License, Version 2.0 (the "License");+you may not use this file except in compliance with the License.+You may obtain a copy of the License at++ http://www.apache.org/licenses/LICENSE-2.0++Unless required by applicable law or agreed to in writing, software+distributed under the License is distributed on an "AS IS" BASIS,+WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.+See the License for the specific language governing permissions and+limitations under the License.
+ README.md view
@@ -0,0 +1,36 @@+# blockfrost-pretty++Pretty printing for Ada/Lovelace values and+some Blockfrost types.++Example of pretty printing lovelace values.++```haskell+λ: :set -XNumericUnderscores+λ: import Blockfrost.Pretty+λ: import Data.Text.IO+λ: Data.Text.IO.putStrLn $ prettyLovelaces 123_456+123 456 lovelaces+λ: Data.Text.IO.putStrLn $ prettyLovelaces 42_123_456+42.123456 ₳+λ: Data.Text.IO.putStrLn $ prettyLovelaces' testnetConfig 42_123_456+42.123456 t₳+λ: Data.Text.IO.putStrLn $ prettyLovelaces' (testnetConfig { pcUnicode = False }) 42_123_456+42.123456 tADA+```++## Using `Prettyprinter`++To pretty print a `Block` to terminal with colors,+we use [prettyprinter](https://hackage.haskell.org/package/prettyprinter) package+with [prettyprinter-ansi-terminal](https://hackage.haskell.org/package/prettyprinter-ansi-terminal).++Example:++```haskell+λ: import Prettyprinter+λ: import Prettyprinter.Render.Terminal+λ: import Data.Text.IO++λ: Data.Text.IO.putStrLn $ renderStrict $ layoutPretty defaultLayoutOptions $ prettyBlock b+```
+ Setup.hs view
@@ -0,0 +1,2 @@+import Distribution.Simple+main = defaultMain
+ blockfrost-pretty.cabal view
@@ -0,0 +1,58 @@+cabal-version: 2.2+name: blockfrost-pretty+version: 0.1.0.0+synopsis: blockfrost.io pretty-printing utilities+description: prettyprinter Docs and standalone prettyPrinters+homepage: https://github.com/blockfrost/blockfrost-haskell+license: Apache-2.0+license-file: LICENSE+author: blockfrost.io+maintainer: srk@48.io+copyright: 2021 blockfrost.io+category: Web+build-type: Simple++extra-source-files:+ CHANGELOG.md+ LICENSE+ README.md++flag BuildFast+ Default: True+ Description: Turn off optimizations++flag Production+ Default: False+ Manual: True+ Description: Production build++common libstuff+ default-language: Haskell2010+ ghc-options: -Wall -Wunused-packages -fno-warn-orphans+ if flag(BuildFast)+ ghc-options: -O0+ if flag(Production)+ ghc-options: -Werror++library+ import: libstuff+ hs-source-dirs: src+ exposed-modules: Blockfrost.Pretty+ , Blockfrost.Pretty.Ada+ , Blockfrost.Pretty.Block+ , Blockfrost.Pretty.Config+ , Blockfrost.Pretty.POSIXTime+ , Blockfrost.Pretty.Shared+ build-depends: base >= 4.7 && < 5+ , blockfrost-api+ , data-default+ , lens+ , prettyprinter+ , prettyprinter-ansi-terminal+ , safe-money+ , text+ , time++source-repository head+ type: git+ location: https://github.com/blockfrost/blockfrost-haskell
+ src/Blockfrost/Pretty.hs view
@@ -0,0 +1,15 @@+-- | Pretty printing for Blockfrost types++module Blockfrost.Pretty+ ( module Blockfrost.Pretty.Ada+ , module Blockfrost.Pretty.Block+ , module Blockfrost.Pretty.Config+ , module Blockfrost.Pretty.POSIXTime+ , module Blockfrost.Pretty.Shared+ ) where++import Blockfrost.Pretty.Ada+import Blockfrost.Pretty.Block+import Blockfrost.Pretty.Config+import Blockfrost.Pretty.POSIXTime+import Blockfrost.Pretty.Shared
+ src/Blockfrost/Pretty/Ada.hs view
@@ -0,0 +1,60 @@+-- | Pretty printing for safe-money Lovelace values++{-# LANGUAGE NumericUnderscores #-}+{-# LANGUAGE OverloadedStrings #-}+{-# LANGUAGE RecordWildCards #-}++module Blockfrost.Pretty.Ada+ ( prettyLovelaces+ , prettyLovelaces'+ , prettyLovelacesDoc+ , prettyLovelacesDoc'+ ) where++import Blockfrost.Pretty.Config+import Blockfrost.Types.Shared.Ada+import Data.Default (Default (def))+import Data.Text (Text)+import qualified Money++import Prettyprinter+import Prettyprinter.Render.Terminal++-- | Pretty print `Lovelaces` using default configuration+prettyLovelaces :: Lovelaces -> Text+prettyLovelaces = prettyLovelaces' def++-- | Pretty print `Lovelaces` using supplied `PrettyConfig`+prettyLovelaces' :: PrettyConfig -> Lovelaces -> Text+prettyLovelaces' PrettyConfig{..} x | x > Money.discrete 1_000_000 =+ Money.denseToDecimal+ pcAdaDecimalConf+ Money.Round+ (Money.denseFromDiscrete x)+ <> " "+ <> prefixIfTestAda pcTestAda+ <> (if pcUnicode then adaSymbol else "ADA")+prettyLovelaces' PrettyConfig{..} x =+ Money.discreteToDecimal+ pcLovelaceDecimalConf+ Money.Round+ x+ <> " "+ <> prefixIfTestAda pcTestAda+ <> "lovelace"+ <> (if x == 1 then mempty else "s")++-- | Pretty print `Lovelaces` to `Doc` using supplied `PrettyConfig`+prettyLovelacesDoc' :: PrettyConfig -> Lovelaces -> Doc AnsiStyle+prettyLovelacesDoc' cfg =+ annotate (color Magenta)+ . pretty+ . prettyLovelaces' cfg++-- | Pretty print `Lovelaces` to `Doc` using default config+prettyLovelacesDoc :: Lovelaces -> Doc AnsiStyle+prettyLovelacesDoc = prettyLovelacesDoc' def++prefixIfTestAda :: Bool -> Text+prefixIfTestAda False = mempty+prefixIfTestAda True = "t"
+ src/Blockfrost/Pretty/Block.hs view
@@ -0,0 +1,56 @@+-- | Pretty printing Block++{-# LANGUAGE OverloadedStrings #-}++module Blockfrost.Pretty.Block+ ( prettyBlock+ , prettyBlock'+ ) where++import Blockfrost.Lens+import Blockfrost.Pretty.Ada+import Blockfrost.Pretty.Config+import Blockfrost.Pretty.POSIXTime+import Blockfrost.Pretty.Shared+import Blockfrost.Types+import Control.Lens+import Data.Default+import Data.Maybe++import Prettyprinter+import Prettyprinter.Render.Terminal++instance Pretty Block where+ pretty = unAnnotate . prettyBlock++-- | Pretty print `Block` with custom `PrettyConfig`+prettyBlock' :: PrettyConfig -> Block -> Doc AnsiStyle+prettyBlock' cfg b = vcat $+ [ annotate (color Yellow) ("block" <+> prettyBlockHash (b ^. hash))+ , "Date:" <+> prettyTime (b ^. time)+ ]+ +++ (catMaybes+ [+ ("Height:"<+>) . pretty <$> b ^. height+ , ("Slot:"<+>) . prettySlot <$> b ^. slot+ , ("Epoch:"<+>) . prettyEpoch <$> b ^. epoch+ , ("Slot within the epoch:"<+>) . pretty <$> b ^. epochSlot+ , ("Fees:" <+>) . prettyLovelacesDoc' cfg <$> b ^. fees+ , ("Output:" <+>) . prettyLovelacesDoc' cfg <$> b ^. output+ ]+ )+ +++ [ "Size:" <+> pretty (b ^. size)+ , "TxCount:" <+> pretty (b ^. txCount)+ , "Confirmations:" <+> pretty (b ^. confirmations)+ , indent 4 $ vsep $ catMaybes+ [ ("VRF:" <+> ) . pretty <$> b ^. blockVrf+ , ("Previous block:" <+> ) . prettyBlockHash <$> b ^. previousBlock+ , ("Next block:" <+> ) . prettyBlockHash <$> b ^. nextBlock+ ]+ ]++-- | Pretty print `Block` using default `PrettyConfig`+prettyBlock :: Block -> Doc AnsiStyle+prettyBlock = prettyBlock' def
+ src/Blockfrost/Pretty/Config.hs view
@@ -0,0 +1,63 @@+-- | Pretty printing config++{-# LANGUAGE DataKinds #-}+{-# LANGUAGE OverloadedStrings #-}+{-# LANGUAGE TypeApplications #-}++module Blockfrost.Pretty.Config+ ( PrettyConfig (..)+ , adaDecimalConf+ , adaSymbol+ , lovelaceDecimalConf+ , testnetConfig+ ) where++import Data.Default (Default (def))+import Data.Proxy (Proxy (..))+import Data.Text (Text)+import qualified Money++-- | Ada Unicode symbol+adaSymbol :: Text+adaSymbol = "₳"++-- | Pretty printing configuration+data PrettyConfig = PrettyConfig {+ pcTestAda :: Bool -- ^ Set if working with test Ada+ , pcUnicode :: Bool -- ^ Set to use unicode symbol for Ada+ , pcAdaDecimalConf :: Money.DecimalConf -- ^ Decimal config for printing values over 1 Ada+ , pcLovelaceDecimalConf :: Money.DecimalConf -- ^ Decimal config for printing lovelace values (<1 Ada)+ }++instance Default PrettyConfig where+ def =+ PrettyConfig {+ pcTestAda = False+ , pcUnicode = True+ , pcAdaDecimalConf = adaDecimalConf+ , pcLovelaceDecimalConf = lovelaceDecimalConf+ }++-- | `PrettyConfig` for testnet+testnetConfig :: PrettyConfig+testnetConfig = def { pcTestAda = True }++-- | Decimal config for printing Ada value+adaDecimalConf :: Money.DecimalConf+adaDecimalConf =+ Money.defaultDecimalConf+ { Money.decimalConf_digits = 6+ , Money.decimalConf_separators = Money.separatorsDotNarrownbsp+ , Money.decimalConf_scale =+ Money.scale (Proxy @(Money.UnitScale "ADA" "ada"))+ }++-- | Decimal config for printing lovelace values+lovelaceDecimalConf :: Money.DecimalConf+lovelaceDecimalConf =+ Money.defaultDecimalConf+ { Money.decimalConf_digits = 0+ , Money.decimalConf_separators = Money.separatorsDotNarrownbsp+ , Money.decimalConf_scale =+ Money.scale (Proxy @(Money.UnitScale "ADA" "lovelace"))+ }
+ src/Blockfrost/Pretty/POSIXTime.hs view
@@ -0,0 +1,28 @@+-- | Pretty POSIXTime++{-# LANGUAGE TypeSynonymInstances #-}++module Blockfrost.Pretty.POSIXTime+ where++import Data.Time+import Data.Time.Clock.POSIX+import Prettyprinter+import Prettyprinter.Render.Terminal++instance Pretty POSIXTime where+ pretty = unAnnotate . prettyTime++-- | Pretty print `POSIXTime` as UTC time+prettyTime :: POSIXTime -> Doc AnsiStyle+prettyTime pt =+ let+ ut = posixSecondsToUTCTime pt+ ymd = formatTime defaultTimeLocale "%F" ut+ hms = formatTime defaultTimeLocale "%X" ut+ in+ annotate (color Green) (pretty ymd)+ <+>+ annotate (color Blue) (pretty hms)+ <+>+ pretty "UTC"
@@ -0,0 +1,37 @@+-- | Pretty shared types++{-# LANGUAGE OverloadedStrings #-}++module Blockfrost.Pretty.Shared+ ( prettyBlockHash+ , prettyEpoch+ , prettySlot+ ) where++import Blockfrost.Types+import Prettyprinter+import Prettyprinter.Render.Terminal++-- | Pretty print `BlockHash`+prettyBlockHash :: BlockHash -> Doc AnsiStyle+prettyBlockHash (BlockHash bh) =+ annotate (color Yellow) ("#" <+> pretty bh)++instance Pretty BlockHash where+ pretty = unAnnotate . prettyBlockHash++-- | Pretty print `Slot`+prettySlot :: Slot -> Doc AnsiStyle+prettySlot =+ annotate (color Cyan) . pretty . unSlot++instance Pretty Slot where+ pretty = unAnnotate . prettySlot++-- | Pretty print `Epoch`+prettyEpoch :: Epoch -> Doc AnsiStyle+prettyEpoch =+ annotate (color Green) . pretty . unEpoch++instance Pretty Epoch where+ pretty = unAnnotate . prettyEpoch