module Cardano.Api.Pretty
  ( Ann
  , Doc
  , Pretty (..)
  , ShowOf (..)
  , docToLazyText
  , docToText
  , docToString
  , pshow
  , prettyException
  , hsep
  , vsep
  , (<+>)
  , black
  , red
  , green
  , yellow
  , blue
  , magenta
  , cyan
  , white
  )
where

import           Cardano.Api.Via.ShowOf

import           Control.Exception.Safe
import qualified Data.Text as Text
import qualified Data.Text.Lazy as TextLazy
import           Prettyprinter
import           Prettyprinter.Render.Terminal

-- | 'Ann' is the prettyprinter annotation for cardano-api and cardano-cli to enable the printing
-- of colored output. This is a type alias for AnsiStyle.
type Ann = AnsiStyle

docToString :: Doc AnsiStyle -> String
docToString :: Doc AnsiStyle -> String
docToString = Doc AnsiStyle -> String
forall a. Show a => a -> String
show

docToLazyText :: Doc AnsiStyle -> TextLazy.Text
docToLazyText :: Doc AnsiStyle -> Text
docToLazyText = SimpleDocStream AnsiStyle -> Text
renderLazy (SimpleDocStream AnsiStyle -> Text)
-> (Doc AnsiStyle -> SimpleDocStream AnsiStyle)
-> Doc AnsiStyle
-> Text
forall b c a. (b -> c) -> (a -> b) -> a -> c
. LayoutOptions -> Doc AnsiStyle -> SimpleDocStream AnsiStyle
forall ann. LayoutOptions -> Doc ann -> SimpleDocStream ann
layoutPretty LayoutOptions
defaultLayoutOptions

docToText :: Doc AnsiStyle -> Text.Text
docToText :: Doc AnsiStyle -> Text
docToText = Text -> Text
TextLazy.toStrict (Text -> Text) -> (Doc AnsiStyle -> Text) -> Doc AnsiStyle -> Text
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Doc AnsiStyle -> Text
docToLazyText

black :: Doc AnsiStyle -> Doc AnsiStyle
black :: Doc AnsiStyle -> Doc AnsiStyle
black = AnsiStyle -> Doc AnsiStyle -> Doc AnsiStyle
forall ann. ann -> Doc ann -> Doc ann
annotate (Color -> AnsiStyle
color Color
Black)

red :: Doc AnsiStyle -> Doc AnsiStyle
red :: Doc AnsiStyle -> Doc AnsiStyle
red = AnsiStyle -> Doc AnsiStyle -> Doc AnsiStyle
forall ann. ann -> Doc ann -> Doc ann
annotate (Color -> AnsiStyle
color Color
Red)

green :: Doc AnsiStyle -> Doc AnsiStyle
green :: Doc AnsiStyle -> Doc AnsiStyle
green = AnsiStyle -> Doc AnsiStyle -> Doc AnsiStyle
forall ann. ann -> Doc ann -> Doc ann
annotate (Color -> AnsiStyle
color Color
Green)

yellow :: Doc AnsiStyle -> Doc AnsiStyle
yellow :: Doc AnsiStyle -> Doc AnsiStyle
yellow = AnsiStyle -> Doc AnsiStyle -> Doc AnsiStyle
forall ann. ann -> Doc ann -> Doc ann
annotate (Color -> AnsiStyle
color Color
Yellow)

blue :: Doc AnsiStyle -> Doc AnsiStyle
blue :: Doc AnsiStyle -> Doc AnsiStyle
blue = AnsiStyle -> Doc AnsiStyle -> Doc AnsiStyle
forall ann. ann -> Doc ann -> Doc ann
annotate (Color -> AnsiStyle
color Color
Blue)

magenta :: Doc AnsiStyle -> Doc AnsiStyle
magenta :: Doc AnsiStyle -> Doc AnsiStyle
magenta = AnsiStyle -> Doc AnsiStyle -> Doc AnsiStyle
forall ann. ann -> Doc ann -> Doc ann
annotate (Color -> AnsiStyle
color Color
Magenta)

cyan :: Doc AnsiStyle -> Doc AnsiStyle
cyan :: Doc AnsiStyle -> Doc AnsiStyle
cyan = AnsiStyle -> Doc AnsiStyle -> Doc AnsiStyle
forall ann. ann -> Doc ann -> Doc ann
annotate (Color -> AnsiStyle
color Color
Cyan)

white :: Doc AnsiStyle -> Doc AnsiStyle
white :: Doc AnsiStyle -> Doc AnsiStyle
white = AnsiStyle -> Doc AnsiStyle -> Doc AnsiStyle
forall ann. ann -> Doc ann -> Doc ann
annotate (Color -> AnsiStyle
color Color
White)

-- | Short hand for 'viaShow'.
pshow :: Show a => a -> Doc ann
pshow :: forall a ann. Show a => a -> Doc ann
pshow = a -> Doc ann
forall a ann. Show a => a -> Doc ann
viaShow

-- | Short hand for @'pretty' . 'displayException'@
prettyException :: Exception a => a -> Doc ann
prettyException :: forall a ann. Exception a => a -> Doc ann
prettyException = String -> Doc ann
forall ann. String -> Doc ann
forall a ann. Pretty a => a -> Doc ann
pretty (String -> Doc ann) -> (a -> String) -> a -> Doc ann
forall b c a. (b -> c) -> (a -> b) -> a -> c
. a -> String
forall e. Exception e => e -> String
displayException