text-printer-0.5: Abstract interface for text builders/printers.

Safe Haskell | None
Language | Haskell2010

Text.Printer

Contents

- The class
- Builders
- Combinators
- Multiline printers

Description

Monoids with a homomorphism from `String` to themselves.

Synopsis

- class (IsString p, Semigroup p, Monoid p) => Printer p where
- newtype StringBuilder = StringBuilder {
  - stringBuilder :: String -> String
  }
- buildString :: StringBuilder -> String
- buildText :: Builder -> Text
- buildLazyText :: Builder -> Text
- newtype AsciiBuilder = AsciiBuilder {
  - asciiBuilder :: Builder
  }
- buildAscii :: AsciiBuilder -> ByteString
- buildLazyAscii :: AsciiBuilder -> ByteString
- newtype Utf8Builder = Utf8Builder {
  - utf8Builder :: Builder
  }
- buildUtf8 :: Utf8Builder -> ByteString
- buildLazyUtf8 :: Utf8Builder -> ByteString
- newtype PrettyPrinter = PrettyPrinter {
  - prettyPrinter :: Doc
  }
- renderPretty :: PrettyPrinter -> String
- (<>) :: Monoid m => m -> m -> m
- hcat :: (Printer p, Foldable f) => f p -> p
- fcat :: (Foldable f, Printer p) => (p -> p -> p) -> f p -> p
- separate :: Printer p => p -> p -> p -> p
- (<+>) :: Printer p => p -> p -> p
- hsep :: (Printer p, Foldable f) => f p -> p
- fsep :: (Foldable f, Printer p) => p -> f p -> p
- list :: (Foldable f, Printer p) => f p -> p
- parens :: Printer p => p -> p
- brackets :: Printer p => p -> p
- braces :: Printer p => p -> p
- angles :: Printer p => p -> p
- squotes :: Printer p => p -> p
- dquotes :: Printer p => p -> p
- punctuateL :: (Traversable t, Printer p) => p -> t p -> t p
- punctuateR :: (Traversable t, Printer p) => p -> t p -> t p
- class Printer p => MultilinePrinter p where
- lines :: (MultilinePrinter p, Foldable f) => f p -> p
- newLine :: Printer p => p
- crlf :: Printer p => p
- newtype LinePrinter p = LinePrinter {
  - linePrinter :: (p -> p -> p) -> p
  }
- lfPrinter :: Printer p => LinePrinter p -> p
- crlfPrinter :: Printer p => LinePrinter p -> p

# The class

class (IsString p, Semigroup p, Monoid p) => Printer p where #

Text monoid. `string` must be equivalent to `fromString` and be a monoid homomorphism, i.e. ``string` `mempty` = `mempty`` and ``mappend` (`string` x) (`string` y) = `string` (`mappend` x y)`. Other operations must be monoid homomorphisms that are eqiuvalent (but possibly faster) to the composition of `string` and the corresponding embedding, e.g. ``text` = `string` . `unpack``.

Methods

char :: Char -> p #

Print a character. ``char` c` must be equivalent to ``string` [c]`, but hopefully is faster.

char7 :: Char -> p #

Print an ASCII character, can be faster than `char`.

string :: String -> p #

Print a string.

string7 :: String -> p #

Print an ASCII string, can be faster than `string`.

text :: Text -> p #

Print a `Text`.

lazyText :: Text -> p #

Print a lazy `Text`.

ascii :: ByteString -> p #

Print an ASCII `ByteString`.

lazyAscii :: ByteString -> p #

Print a lazy ASCII `ByteString`.

utf8 :: ByteString -> p #

Print a UTF-8 `ByteString`.

lazyUtf8 :: ByteString -> p #

Print a lazy UTF-8 `ByteString`

Instances

Printer String # |
Methods char :: Char -> String # char7 :: Char -> String # string :: String -> String # string7 :: String -> String # text :: Text -> String # lazyText :: Text -> String # ascii :: ByteString -> String # lazyAscii :: ByteString -> String # utf8 :: ByteString -> String # lazyUtf8 :: ByteString -> String #
Printer Builder # |
Methods char :: Char -> Builder # char7 :: Char -> Builder # string :: String -> Builder # string7 :: String -> Builder # text :: Text -> Builder # lazyText :: Text -> Builder # ascii :: ByteString -> Builder # lazyAscii :: ByteString -> Builder # utf8 :: ByteString -> Builder # lazyUtf8 :: ByteString -> Builder #
Printer PrettyPrinter # |
Methods char :: Char -> PrettyPrinter # char7 :: Char -> PrettyPrinter # string :: String -> PrettyPrinter # string7 :: String -> PrettyPrinter # text :: Text -> PrettyPrinter # lazyText :: Text -> PrettyPrinter # ascii :: ByteString -> PrettyPrinter # lazyAscii :: ByteString -> PrettyPrinter # utf8 :: ByteString -> PrettyPrinter # lazyUtf8 :: ByteString -> PrettyPrinter #
Printer Utf8Builder # |
Methods char :: Char -> Utf8Builder # char7 :: Char -> Utf8Builder # string :: String -> Utf8Builder # string7 :: String -> Utf8Builder # text :: Text -> Utf8Builder # lazyText :: Text -> Utf8Builder # ascii :: ByteString -> Utf8Builder # lazyAscii :: ByteString -> Utf8Builder # utf8 :: ByteString -> Utf8Builder # lazyUtf8 :: ByteString -> Utf8Builder #
Printer AsciiBuilder # |
Methods char :: Char -> AsciiBuilder # char7 :: Char -> AsciiBuilder # string :: String -> AsciiBuilder # string7 :: String -> AsciiBuilder # text :: Text -> AsciiBuilder # lazyText :: Text -> AsciiBuilder # ascii :: ByteString -> AsciiBuilder # lazyAscii :: ByteString -> AsciiBuilder # utf8 :: ByteString -> AsciiBuilder # lazyUtf8 :: ByteString -> AsciiBuilder #
Printer StringBuilder # |
Methods char :: Char -> StringBuilder # char7 :: Char -> StringBuilder # string :: String -> StringBuilder # string7 :: String -> StringBuilder # text :: Text -> StringBuilder # lazyText :: Text -> StringBuilder # ascii :: ByteString -> StringBuilder # lazyAscii :: ByteString -> StringBuilder # utf8 :: ByteString -> StringBuilder # lazyUtf8 :: ByteString -> StringBuilder #
Printer p => Printer (LinePrinter p) # |
Methods char :: Char -> LinePrinter p # char7 :: Char -> LinePrinter p # string :: String -> LinePrinter p # string7 :: String -> LinePrinter p # text :: Text -> LinePrinter p # lazyText :: Text -> LinePrinter p # ascii :: ByteString -> LinePrinter p # lazyAscii :: ByteString -> LinePrinter p # utf8 :: ByteString -> LinePrinter p # lazyUtf8 :: ByteString -> LinePrinter p #

# Builders

newtype StringBuilder #

A simple string builder as used by `Show`.

Constructors

StringBuilder |
Fields stringBuilder :: String -> String

Instances

IsString StringBuilder # |
Methods fromString :: String -> StringBuilder #
Generic StringBuilder # |
Associated Types type Rep StringBuilder :: * -> * # Methods from :: StringBuilder -> Rep StringBuilder x # to :: Rep StringBuilder x -> StringBuilder #
Semigroup StringBuilder # |
Methods (<>) :: StringBuilder -> StringBuilder -> StringBuilder # sconcat :: NonEmpty StringBuilder -> StringBuilder # stimes :: Integral b => b -> StringBuilder -> StringBuilder #
Monoid StringBuilder # |
Methods mempty :: StringBuilder # mappend :: StringBuilder -> StringBuilder -> StringBuilder # mconcat :: [StringBuilder] -> StringBuilder #
Printer StringBuilder # |
Methods char :: Char -> StringBuilder # char7 :: Char -> StringBuilder # string :: String -> StringBuilder # string7 :: String -> StringBuilder # text :: Text -> StringBuilder # lazyText :: Text -> StringBuilder # ascii :: ByteString -> StringBuilder # lazyAscii :: ByteString -> StringBuilder # utf8 :: ByteString -> StringBuilder # lazyUtf8 :: ByteString -> StringBuilder #
type Rep StringBuilder # |
type Rep StringBuilder = D1 (MetaData "StringBuilder" "Text.Printer" "text-printer-0.5-841jXq5bFX06bZ6MMnsbyd" True) (C1 (MetaCons "StringBuilder" PrefixI True) (S1 (MetaSel (Just Symbol "stringBuilder") NoSourceUnpackedness NoSourceStrictness DecidedLazy) (Rec0 (String -> String))))

buildString :: StringBuilder -> String #

buildText :: Builder -> Text #

buildLazyText :: Builder -> Text #

newtype AsciiBuilder #

Use this builder when you are sure that only ASCII characters will get printed to it.

Constructors

AsciiBuilder |
Fields asciiBuilder :: Builder

Instances

IsString AsciiBuilder # |
Methods fromString :: String -> AsciiBuilder #
Generic AsciiBuilder # |
Associated Types type Rep AsciiBuilder :: * -> * # Methods from :: AsciiBuilder -> Rep AsciiBuilder x # to :: Rep AsciiBuilder x -> AsciiBuilder #
Semigroup AsciiBuilder # |
Methods (<>) :: AsciiBuilder -> AsciiBuilder -> AsciiBuilder # sconcat :: NonEmpty AsciiBuilder -> AsciiBuilder # stimes :: Integral b => b -> AsciiBuilder -> AsciiBuilder #
Monoid AsciiBuilder # |
Methods mempty :: AsciiBuilder # mappend :: AsciiBuilder -> AsciiBuilder -> AsciiBuilder # mconcat :: [AsciiBuilder] -> AsciiBuilder #
Printer AsciiBuilder # |
Methods char :: Char -> AsciiBuilder # char7 :: Char -> AsciiBuilder # string :: String -> AsciiBuilder # string7 :: String -> AsciiBuilder # text :: Text -> AsciiBuilder # lazyText :: Text -> AsciiBuilder # ascii :: ByteString -> AsciiBuilder # lazyAscii :: ByteString -> AsciiBuilder # utf8 :: ByteString -> AsciiBuilder # lazyUtf8 :: ByteString -> AsciiBuilder #
type Rep AsciiBuilder # |
type Rep AsciiBuilder = D1 (MetaData "AsciiBuilder" "Text.Printer" "text-printer-0.5-841jXq5bFX06bZ6MMnsbyd" True) (C1 (MetaCons "AsciiBuilder" PrefixI True) (S1 (MetaSel (Just Symbol "asciiBuilder") NoSourceUnpackedness NoSourceStrictness DecidedLazy) (Rec0 Builder)))

buildAscii :: AsciiBuilder -> ByteString #

buildLazyAscii :: AsciiBuilder -> ByteString #

newtype Utf8Builder #

UTF-8 lazy `ByteString` builder.

Constructors

Utf8Builder |
Fields utf8Builder :: Builder

Instances

IsString Utf8Builder # |
Methods fromString :: String -> Utf8Builder #
Generic Utf8Builder # |
Associated Types type Rep Utf8Builder :: * -> * # Methods from :: Utf8Builder -> Rep Utf8Builder x # to :: Rep Utf8Builder x -> Utf8Builder #
Semigroup Utf8Builder # |
Methods (<>) :: Utf8Builder -> Utf8Builder -> Utf8Builder # sconcat :: NonEmpty Utf8Builder -> Utf8Builder # stimes :: Integral b => b -> Utf8Builder -> Utf8Builder #
Monoid Utf8Builder # |
Methods mempty :: Utf8Builder # mappend :: Utf8Builder -> Utf8Builder -> Utf8Builder # mconcat :: [Utf8Builder] -> Utf8Builder #
Printer Utf8Builder # |
Methods char :: Char -> Utf8Builder # char7 :: Char -> Utf8Builder # string :: String -> Utf8Builder # string7 :: String -> Utf8Builder # text :: Text -> Utf8Builder # lazyText :: Text -> Utf8Builder # ascii :: ByteString -> Utf8Builder # lazyAscii :: ByteString -> Utf8Builder # utf8 :: ByteString -> Utf8Builder # lazyUtf8 :: ByteString -> Utf8Builder #
type Rep Utf8Builder # |
type Rep Utf8Builder = D1 (MetaData "Utf8Builder" "Text.Printer" "text-printer-0.5-841jXq5bFX06bZ6MMnsbyd" True) (C1 (MetaCons "Utf8Builder" PrefixI True) (S1 (MetaSel (Just Symbol "utf8Builder") NoSourceUnpackedness NoSourceStrictness DecidedLazy) (Rec0 Builder)))

buildUtf8 :: Utf8Builder -> ByteString #

buildLazyUtf8 :: Utf8Builder -> ByteString #

newtype PrettyPrinter #

Constructors

PrettyPrinter |
Fields prettyPrinter :: Doc

Instances

IsString PrettyPrinter # |
Methods fromString :: String -> PrettyPrinter #
Generic PrettyPrinter # |
Associated Types type Rep PrettyPrinter :: * -> * # Methods from :: PrettyPrinter -> Rep PrettyPrinter x # to :: Rep PrettyPrinter x -> PrettyPrinter #
Semigroup PrettyPrinter # |
Methods (<>) :: PrettyPrinter -> PrettyPrinter -> PrettyPrinter # sconcat :: NonEmpty PrettyPrinter -> PrettyPrinter # stimes :: Integral b => b -> PrettyPrinter -> PrettyPrinter #
Monoid PrettyPrinter # |
Methods mempty :: PrettyPrinter # mappend :: PrettyPrinter -> PrettyPrinter -> PrettyPrinter # mconcat :: [PrettyPrinter] -> PrettyPrinter #
MultilinePrinter PrettyPrinter # |
Methods (<->) :: PrettyPrinter -> PrettyPrinter -> PrettyPrinter #
Printer PrettyPrinter # |
Methods char :: Char -> PrettyPrinter # char7 :: Char -> PrettyPrinter # string :: String -> PrettyPrinter # string7 :: String -> PrettyPrinter # text :: Text -> PrettyPrinter # lazyText :: Text -> PrettyPrinter # ascii :: ByteString -> PrettyPrinter # lazyAscii :: ByteString -> PrettyPrinter # utf8 :: ByteString -> PrettyPrinter # lazyUtf8 :: ByteString -> PrettyPrinter #
type Rep PrettyPrinter # |
type Rep PrettyPrinter = D1 (MetaData "PrettyPrinter" "Text.Printer" "text-printer-0.5-841jXq5bFX06bZ6MMnsbyd" True) (C1 (MetaCons "PrettyPrinter" PrefixI True) (S1 (MetaSel (Just Symbol "prettyPrinter") NoSourceUnpackedness NoSourceStrictness DecidedLazy) (Rec0 Doc)))

renderPretty :: PrettyPrinter -> String #

An alias for ``render` . `prettyPrinter``

# Combinators

(<>) :: Monoid m => m -> m -> m infixr 6 #

An infix synonym for `mappend`.

Since: 4.5.0.0

hcat :: (Printer p, Foldable f) => f p -> p #

`mconcat` for `Foldable` data structures.

fcat :: (Foldable f, Printer p) => (p -> p -> p) -> f p -> p #

Combine the items of a `Foldable` data structure using the provided function. If the data structure is empty, `mempty` is returned.

separate #

Arguments

:: Printer p |
=> p | The separator
-> p |
-> p |
-> p |

Concatenate two `Printer`s with a separator between them.

(<+>) :: Printer p => p -> p -> p infixr 6 #

Concatenate two `Printer`s with a space between them.

hsep :: (Printer p, Foldable f) => f p -> p #

Concatenate the items of a `Foldable` data structure with spaces between them.

fsep :: (Foldable f, Printer p) => p -> f p -> p #

A shorthand for ``fcat` . `separate``.

list :: (Foldable f, Printer p) => f p -> p #

Concatenate the items of a `Foldable` data structure with commas between them.

      list = fsep (char7 ',')

parens :: Printer p => p -> p #

Enclose a `Printer` with parentheses.

brackets :: Printer p => p -> p #

Enclose a `Printer` with square brackets.

braces :: Printer p => p -> p #

Enclose a `Printer` with curly braces.

angles :: Printer p => p -> p #

Enclose a `Printer` with angle brackets.

squotes :: Printer p => p -> p #

Enclose a `Printer` with single quotes.

dquotes :: Printer p => p -> p #

Enclose a `Printer` with double quotes.

punctuateL :: (Traversable t, Printer p) => p -> t p -> t p #

Prepend all but the first element of a `Traversable` with the provided value, e.g. ``punctuateL` p [x1, x2, ..., xN] = [x1, p `<>` x2, ..., p `<>` xN]`

punctuateR :: (Traversable t, Printer p) => p -> t p -> t p #

Append the provided value to all but the last element of a `Traversable`, e.g. ``punctuateR` p [x1, ..., xN-1, xN] = [x1 `<>` p, ..., xN-1 `<>` p, xN]`

# Multiline printers

class Printer p => MultilinePrinter p where #

Printers that can produce multiple lines of text.

Minimal complete definition

(<->)

Methods

(<->) :: p -> p -> p infixr 5 #

Combine two lines. Must be associative, i.e. x `<->` (y `<->` z) = (x `<->` y) `<->` z.

Instances

MultilinePrinter PrettyPrinter # |
Methods (<->) :: PrettyPrinter -> PrettyPrinter -> PrettyPrinter #
Printer p => MultilinePrinter (LinePrinter p) # |
Methods (<->) :: LinePrinter p -> LinePrinter p -> LinePrinter p #

lines :: (MultilinePrinter p, Foldable f) => f p -> p #

Combine the items of a `Foldable` data structure with `<->`.

newLine :: Printer p => p #

Print the LF character ('\n').

crlf :: Printer p => p #

Print CR ('\r') followed by LF ('\n').

newtype LinePrinter p #

A multiline printer that combines lines with the provided function.

Constructors

LinePrinter |
Fields linePrinter :: (p -> p -> p) -> p

Instances

IsString p => IsString (LinePrinter p) # |
Methods fromString :: String -> LinePrinter p #
Generic (LinePrinter p) # |
Associated Types type Rep (LinePrinter p) :: * -> * # Methods from :: LinePrinter p -> Rep (LinePrinter p) x # to :: Rep (LinePrinter p) x -> LinePrinter p #
Semigroup p => Semigroup (LinePrinter p) # |
Methods (<>) :: LinePrinter p -> LinePrinter p -> LinePrinter p # sconcat :: NonEmpty (LinePrinter p) -> LinePrinter p # stimes :: Integral b => b -> LinePrinter p -> LinePrinter p #
Monoid p => Monoid (LinePrinter p) # |
Methods mempty :: LinePrinter p # mappend :: LinePrinter p -> LinePrinter p -> LinePrinter p # mconcat :: [LinePrinter p] -> LinePrinter p #
Printer p => MultilinePrinter (LinePrinter p) # |
Methods (<->) :: LinePrinter p -> LinePrinter p -> LinePrinter p #
Printer p => Printer (LinePrinter p) # |
Methods char :: Char -> LinePrinter p # char7 :: Char -> LinePrinter p # string :: String -> LinePrinter p # string7 :: String -> LinePrinter p # text :: Text -> LinePrinter p # lazyText :: Text -> LinePrinter p # ascii :: ByteString -> LinePrinter p # lazyAscii :: ByteString -> LinePrinter p # utf8 :: ByteString -> LinePrinter p # lazyUtf8 :: ByteString -> LinePrinter p #
type Rep (LinePrinter p) # |
type Rep (LinePrinter p) = D1 (MetaData "LinePrinter" "Text.Printer" "text-printer-0.5-841jXq5bFX06bZ6MMnsbyd" True) (C1 (MetaCons "LinePrinter" PrefixI True) (S1 (MetaSel (Just Symbol "linePrinter") NoSourceUnpackedness NoSourceStrictness DecidedLazy) (Rec0 ((p -> p -> p) -> p))))

lfPrinter :: Printer p => LinePrinter p -> p #

Separate lines with `newLine`.

crlfPrinter :: Printer p => LinePrinter p -> p #

Separate lines with `crlf`.