pretty-simple-1.1.0.1: pretty printer for data types with a 'Show' instance.

Text.Pretty.Simple

Description

This module contains the functions pPrint, pShow, and pString for pretty-printing any Haskell data type with a Show instance.

pPrint is the main go-to function when debugging Haskell code. pShow and pString are slight variations on pPrint.

The other variations of pPrint, pShow, and pString are for printing without color and changing the indentation amount. Most users can ignore these.

See the Examples section at the end of this module for examples of acutally using pPrint.

Synopsis

# Output With Color

pShow :: Show a => a -> Text Source #

Similar to pPrint, but just return the resulting pretty-printed data type as a Text instead of printing it to the screen.

pPrint :: (MonadIO m, Show a) => a -> m () Source #

Pretty-print any data type that has a Show instance.

If you've never seen MonadIO before, you can think of this function as having the following type signature:

 pPrint :: Show a => a -> IO ()


Similar to pShow, but the first argument is a String representing a data type that has already been showed.

This will work on any String that is similar to a Haskell data type. The only requirement is that the strings are quoted, and braces, parentheses, and brackets are correctly used to represent indentation. For example, pString will correctly pretty-print JSON.

# Output With NO Color

pShowNoColor :: Show a => a -> Text Source #

Like pShow, but without color.

pPrintNoColor :: (MonadIO m, Show a) => a -> m () Source #

Similar to pPrint, but doesn't print in color. However, data types will still be indented nicely.

>>> pPrintNoColor $Just ["hello", "bye"] Just [ "hello" , "bye" ]  LIke pString, but without color. # Output With Output Options pShowOpt :: Show a => OutputOptions -> a -> Text Source # Like pShow but takes OutputOptions to change how the pretty-printing is done. pPrintOpt :: (MonadIO m, Show a) => OutputOptions -> a -> m () Source # Similar to pPrint but takes OutputOptions to change how the pretty-printing is done. For example, pPrintOpt can be used to make the indentation much smaller than normal. This is what the normal indentation looks like: >>> pPrintOpt noColorOutputOptions$ Just ("hello", "bye")
Just
( "hello"
, "bye"
)


This is what smaller indentation looks like:

>>> let smallIndent = noColorOutputOptions {outputOptionsIndentAmount = 1}
>>> pPrintOpt smallIndent $Just ("hello", "bye") Just ( "hello" , "bye" )  Like pString but takes OutputOptions to change how the pretty-printing is done. Data-type wrapping up all the options available when rendering the list of Outputs. Constructors  OutputOptions FieldsoutputOptionsIndentAmount :: IntNumber of spaces to use when indenting. It should probably be either 2 or 4.outputOptionsUseColor :: UseColorWhether or not to use ansi escape sequences to print colors. Instances  Source # Methods Source # Methodsgfoldl :: (forall d b. Data d => c (d -> b) -> d -> c b) -> (forall g. g -> c g) -> OutputOptions -> c OutputOptions #gunfold :: (forall b r. Data b => c (b -> r) -> c r) -> (forall r. r -> c r) -> Constr -> c OutputOptions #dataCast1 :: Typeable (* -> *) t => (forall d. Data d => c (t d)) -> Maybe (c OutputOptions) #dataCast2 :: Typeable (* -> * -> *) t => (forall d e. (Data d, Data e) => c (t d e)) -> Maybe (c OutputOptions) #gmapT :: (forall b. Data b => b -> b) -> OutputOptions -> OutputOptions #gmapQl :: (r -> r' -> r) -> r -> (forall d. Data d => d -> r') -> OutputOptions -> r #gmapQr :: (r' -> r -> r) -> r -> (forall d. Data d => d -> r') -> OutputOptions -> r #gmapQ :: (forall d. Data d => d -> u) -> OutputOptions -> [u] #gmapQi :: Int -> (forall d. Data d => d -> u) -> OutputOptions -> u #gmapM :: Monad m => (forall d. Data d => d -> m d) -> OutputOptions -> m OutputOptions #gmapMp :: MonadPlus m => (forall d. Data d => d -> m d) -> OutputOptions -> m OutputOptions #gmapMo :: MonadPlus m => (forall d. Data d => d -> m d) -> OutputOptions -> m OutputOptions # Source # Methods Source # MethodsshowList :: [OutputOptions] -> ShowS # Source # Associated Typestype Rep OutputOptions :: * -> * # Methods type Rep OutputOptions Source # type Rep OutputOptions = D1 (MetaData "OutputOptions" "Text.Pretty.Simple.Internal.OutputPrinter" "pretty-simple-1.1.0.1-3dcx3PIbCc5BUoeqB7WoPm" False) (C1 (MetaCons "OutputOptions" PrefixI True) ((:*:) (S1 (MetaSel (Just Symbol "outputOptionsIndentAmount") NoSourceUnpackedness NoSourceStrictness DecidedLazy) (Rec0 Int)) (S1 (MetaSel (Just Symbol "outputOptionsUseColor") NoSourceUnpackedness NoSourceStrictness DecidedLazy) (Rec0 UseColor)))) data UseColor Source # UseColor describes whether or not we want to use color when printing the Output list. Constructors  NoColor UseColor Instances  Source # Methods Source # Methodsgfoldl :: (forall d b. Data d => c (d -> b) -> d -> c b) -> (forall g. g -> c g) -> UseColor -> c UseColor #gunfold :: (forall b r. Data b => c (b -> r) -> c r) -> (forall r. r -> c r) -> Constr -> c UseColor #dataCast1 :: Typeable (* -> *) t => (forall d. Data d => c (t d)) -> Maybe (c UseColor) #dataCast2 :: Typeable (* -> * -> *) t => (forall d e. (Data d, Data e) => c (t d e)) -> Maybe (c UseColor) #gmapT :: (forall b. Data b => b -> b) -> UseColor -> UseColor #gmapQl :: (r -> r' -> r) -> r -> (forall d. Data d => d -> r') -> UseColor -> r #gmapQr :: (r' -> r -> r) -> r -> (forall d. Data d => d -> r') -> UseColor -> r #gmapQ :: (forall d. Data d => d -> u) -> UseColor -> [u] #gmapQi :: Int -> (forall d. Data d => d -> u) -> UseColor -> u #gmapM :: Monad m => (forall d. Data d => d -> m d) -> UseColor -> m UseColor #gmapMp :: MonadPlus m => (forall d. Data d => d -> m d) -> UseColor -> m UseColor #gmapMo :: MonadPlus m => (forall d. Data d => d -> m d) -> UseColor -> m UseColor # Source # Methods Source # MethodsshowList :: [UseColor] -> ShowS # Source # Associated Typestype Rep UseColor :: * -> * # Methodsto :: Rep UseColor x -> UseColor # type Rep UseColor Source # type Rep UseColor = D1 (MetaData "UseColor" "Text.Pretty.Simple.Internal.OutputPrinter" "pretty-simple-1.1.0.1-3dcx3PIbCc5BUoeqB7WoPm" False) ((:+:) (C1 (MetaCons "NoColor" PrefixI False) U1) (C1 (MetaCons "UseColor" PrefixI False) U1)) Default values for OutputOptions. outputOptionsIndentAmount defaults to 4, and outputOptionsUseColor defaults to UseColor. noColorOutputOptions is just like defaultOutputOptions, but outputOptionsUseColor is set to NoColor. # Examples Here are some examples of using pPrint on different data types. You can look at these examples to get an idea of what pPrint will output. The following examples are all using pPrintNoColor instead of pPrint because their output is being checked using doctest. pPrint outputs ANSI escape codes in order to produce color, so the following examples would be hard to read had pPrint been used. Simple Haskell data type >>> data Foo a = Foo a String deriving Show  >>> pPrintNoColor$ Foo 3 "hello"
Foo 3 "hello"


List

>>> pPrintNoColor $[1,2,3] [ 1 , 2 , 3 ]  Slightly more complicated list >>> pPrintNoColor$ [ Foo [ (), () ] "hello" ]
[ Foo
[ ()
, ()
] "hello"
]

>>> pPrintNoColor $[ Foo [ "bar", "baz" ] "hello", Foo [] "bye" ] [ Foo [ "bar" , "baz" ] "hello" , Foo [] "bye" ]  Record >>> :{ data Bar b = Bar { barInt :: Int , barA :: b , barList :: [Foo Double] } deriving Show :}  >>> pPrintNoColor$ Bar 1 [10, 11] [Foo 1.1 "", Foo 2.2 "hello"]
Bar
{ barInt = 1
, barA =
[ 10
, 11
]
, barList =
[ Foo 1.1 ""
, Foo 2.2 "hello"
]
}


Newtype

>>> newtype Baz = Baz { unBaz :: [String] } deriving Show

>>> pPrintNoColor \$ Baz ["hello", "bye"]
Baz
{ unBaz =
[ "hello"
, "bye"
]
}