Prettyprinter.Render.Terminal.Internal

prettyprinter-ansi-terminal-1.1.3: ANSI terminal backend for the »prettyprinter« package.

Safe Haskell | Safe-Inferred
Language | Haskell2010

Prettyprinter.Render.Terminal.Internal

Contents

- Styling
  - Font color
  - Background color
  - Font style
  - Internal markers
- Conversion to ANSI-infused `Text`
- Render directly to `stdout`
  - Convenience functions

Description

Warning: Internal module. May change arbitrarily between versions.

Synopsis

- data AnsiStyle = SetAnsiStyle {
  - ansiForeground :: Maybe (Intensity, Color)
  - ansiBackground :: Maybe (Intensity, Color)
  - ansiBold :: Maybe Bold
  - ansiItalics :: Maybe Italicized
  - ansiUnderlining :: Maybe Underlined
  }
- data Color
  - = Black
  - | Red
  - | Green
  - | Yellow
  - | Blue
  - | Magenta
  - | Cyan
  - | White
- color :: Color -> AnsiStyle
- colorDull :: Color -> AnsiStyle
- bgColor :: Color -> AnsiStyle
- bgColorDull :: Color -> AnsiStyle
- bold :: AnsiStyle
- italicized :: AnsiStyle
- underlined :: AnsiStyle
- data Intensity
  - = Vivid
  - | Dull
- data Bold = Bold
- data Underlined = Underlined
- data Italicized = Italicized
- renderLazy :: SimpleDocStream AnsiStyle -> Text
- renderStrict :: SimpleDocStream AnsiStyle -> Text
- renderIO :: Handle -> SimpleDocStream AnsiStyle -> IO ()
- putDoc :: Doc AnsiStyle -> IO ()
- hPutDoc :: Handle -> Doc AnsiStyle -> IO ()

# Styling

data AnsiStyle #

Render the annotated document in a certain style. Styles not set in the annotation will use the style of the surrounding document, or the terminal’s default if none has been set yet.

    style = color Green <> bold
    styledDoc = annotate style "hello world"

Constructors

SetAnsiStyle |
Fields ansiForeground :: Maybe (Intensity, Color) Set the foreground color, or keep the old one. ansiBackground :: Maybe (Intensity, Color) Set the background color, or keep the old one. ansiBold :: Maybe Bold Switch on boldness, or don’t do anything. ansiItalics :: Maybe Italicized Switch on italics, or don’t do anything. ansiUnderlining :: Maybe Underlined Switch on underlining, or don’t do anything.

#### Instances

Instances details

Monoid AnsiStyle # | `mempty` does nothing, which is equivalent to inheriting the style of the surrounding doc, or the terminal’s default if no style has been set yet.
Instance details Defined in Prettyprinter.Render.Terminal.Internal Methods mempty :: AnsiStyle # mappend :: AnsiStyle -> AnsiStyle -> AnsiStyle # mconcat :: [AnsiStyle] -> AnsiStyle #
Semigroup AnsiStyle # | Keep the first decision for each of foreground color, background color, boldness, italication, and underlining. If a certain style is not set, the terminal’s default will be used. Example:
    color Red <> color Green
is red because the first color wins, and not bold because (or if) that’s the terminal’s default.
Instance details Defined in Prettyprinter.Render.Terminal.Internal Methods (<>) :: AnsiStyle -> AnsiStyle -> AnsiStyle # sconcat :: NonEmpty AnsiStyle -> AnsiStyle # stimes :: Integral b => b -> AnsiStyle -> AnsiStyle #
Show AnsiStyle # |
Instance details Defined in Prettyprinter.Render.Terminal.Internal Methods showsPrec :: Int -> AnsiStyle -> ShowS # show :: AnsiStyle -> String # showList :: [AnsiStyle] -> ShowS #
Eq AnsiStyle # |
Instance details Defined in Prettyprinter.Render.Terminal.Internal Methods (==) :: AnsiStyle -> AnsiStyle -> Bool # (/=) :: AnsiStyle -> AnsiStyle -> Bool #
Ord AnsiStyle # |
Instance details Defined in Prettyprinter.Render.Terminal.Internal Methods compare :: AnsiStyle -> AnsiStyle -> Ordering # (<) :: AnsiStyle -> AnsiStyle -> Bool # (<=) :: AnsiStyle -> AnsiStyle -> Bool # (>) :: AnsiStyle -> AnsiStyle -> Bool # (>=) :: AnsiStyle -> AnsiStyle -> Bool # max :: AnsiStyle -> AnsiStyle -> AnsiStyle # min :: AnsiStyle -> AnsiStyle -> AnsiStyle #

data Color #

The 8 ANSI terminal colors.

Constructors

Black |
Red |
Green |
Yellow |
Blue |
Magenta |
Cyan |
White |

#### Instances

Instances details

Show Color # |
Instance details Defined in Prettyprinter.Render.Terminal.Internal Methods showsPrec :: Int -> Color -> ShowS # show :: Color -> String # showList :: [Color] -> ShowS #
Eq Color # |
Instance details Defined in Prettyprinter.Render.Terminal.Internal Methods (==) :: Color -> Color -> Bool # (/=) :: Color -> Color -> Bool #
Ord Color # |
Instance details Defined in Prettyprinter.Render.Terminal.Internal Methods compare :: Color -> Color -> Ordering # (<) :: Color -> Color -> Bool # (<=) :: Color -> Color -> Bool # (>) :: Color -> Color -> Bool # (>=) :: Color -> Color -> Bool # max :: Color -> Color -> Color # min :: Color -> Color -> Color #

## Font color

color :: Color -> AnsiStyle #

Style the foreground with a vivid color.

colorDull :: Color -> AnsiStyle #

Style the foreground with a dull color.

## Background color

bgColor :: Color -> AnsiStyle #

Style the background with a vivid color.

bgColorDull :: Color -> AnsiStyle #

Style the background with a dull color.

## Font style

bold :: AnsiStyle #

Render in bold.

italicized :: AnsiStyle #

Render in italics.

underlined :: AnsiStyle #

Render underlined.

## Internal markers

data Intensity #

Dull or vivid coloring, as supported by ANSI terminals.

Constructors

Vivid |
Dull |

#### Instances

Instances details

Show Intensity # |
Instance details Defined in Prettyprinter.Render.Terminal.Internal Methods showsPrec :: Int -> Intensity -> ShowS # show :: Intensity -> String # showList :: [Intensity] -> ShowS #
Eq Intensity # |
Instance details Defined in Prettyprinter.Render.Terminal.Internal Methods (==) :: Intensity -> Intensity -> Bool # (/=) :: Intensity -> Intensity -> Bool #
Ord Intensity # |
Instance details Defined in Prettyprinter.Render.Terminal.Internal Methods compare :: Intensity -> Intensity -> Ordering # (<) :: Intensity -> Intensity -> Bool # (<=) :: Intensity -> Intensity -> Bool # (>) :: Intensity -> Intensity -> Bool # (>=) :: Intensity -> Intensity -> Bool # max :: Intensity -> Intensity -> Intensity # min :: Intensity -> Intensity -> Intensity #

data Bold #

Constructors

Bold |

#### Instances

Instances details

Show Bold # |
Instance details Defined in Prettyprinter.Render.Terminal.Internal Methods showsPrec :: Int -> Bold -> ShowS # show :: Bold -> String # showList :: [Bold] -> ShowS #
Eq Bold # |
Instance details Defined in Prettyprinter.Render.Terminal.Internal Methods (==) :: Bold -> Bold -> Bool # (/=) :: Bold -> Bold -> Bool #
Ord Bold # |
Instance details Defined in Prettyprinter.Render.Terminal.Internal Methods compare :: Bold -> Bold -> Ordering # (<) :: Bold -> Bold -> Bool # (<=) :: Bold -> Bold -> Bool # (>) :: Bold -> Bold -> Bool # (>=) :: Bold -> Bold -> Bool # max :: Bold -> Bold -> Bold # min :: Bold -> Bold -> Bold #

data Underlined #

Constructors

Underlined |

#### Instances

Instances details

Show Underlined # |
Instance details Defined in Prettyprinter.Render.Terminal.Internal Methods showsPrec :: Int -> Underlined -> ShowS # show :: Underlined -> String # showList :: [Underlined] -> ShowS #
Eq Underlined # |
Instance details Defined in Prettyprinter.Render.Terminal.Internal Methods (==) :: Underlined -> Underlined -> Bool # (/=) :: Underlined -> Underlined -> Bool #
Ord Underlined # |
Instance details Defined in Prettyprinter.Render.Terminal.Internal Methods compare :: Underlined -> Underlined -> Ordering # (<) :: Underlined -> Underlined -> Bool # (<=) :: Underlined -> Underlined -> Bool # (>) :: Underlined -> Underlined -> Bool # (>=) :: Underlined -> Underlined -> Bool # max :: Underlined -> Underlined -> Underlined # min :: Underlined -> Underlined -> Underlined #

data Italicized #

Constructors

Italicized |

#### Instances

Instances details

Show Italicized # |
Instance details Defined in Prettyprinter.Render.Terminal.Internal Methods showsPrec :: Int -> Italicized -> ShowS # show :: Italicized -> String # showList :: [Italicized] -> ShowS #
Eq Italicized # |
Instance details Defined in Prettyprinter.Render.Terminal.Internal Methods (==) :: Italicized -> Italicized -> Bool # (/=) :: Italicized -> Italicized -> Bool #
Ord Italicized # |
Instance details Defined in Prettyprinter.Render.Terminal.Internal Methods compare :: Italicized -> Italicized -> Ordering # (<) :: Italicized -> Italicized -> Bool # (<=) :: Italicized -> Italicized -> Bool # (>) :: Italicized -> Italicized -> Bool # (>=) :: Italicized -> Italicized -> Bool # max :: Italicized -> Italicized -> Italicized # min :: Italicized -> Italicized -> Italicized #

# Conversion to ANSI-infused `Text`

renderLazy :: SimpleDocStream AnsiStyle -> Text #

`(`renderLazy` doc)` takes the output `doc` from a rendering function and transforms it to lazy text, including ANSI styling directives for things like colorization.

ANSI color information will be discarded by this function unless you are running on a Unix-like operating system. This is due to a technical limitation in Windows ANSI support.

With a bit of trickery to make the ANSI codes printable, here is an example that would render colored in an ANSI terminal:

    >>> let render = TL.putStrLn . TL.replace "\ESC" "\\e" . renderLazy . layoutPretty defaultLayoutOptions
    >>> let doc = annotate (color Red) ("red" <+> align (vsep [annotate (color Blue <> underlined) ("blue+u" <+> annotate bold "bold" <+> "blue+u"), "red"]))
    >>> render (unAnnotate doc)
    red blue+u bold blue+u
        red
    >>> render doc
    \e[0;91mred \e[0;94;4mblue+u \e[0;94;1;4mbold\e[0;94;4m blue+u\e[0;91m
        red\e[0m

Run the above via `echo -e `...`` in your terminal to see the coloring.

renderStrict :: SimpleDocStream AnsiStyle -> Text #

`(`renderStrict` sdoc)` takes the output `sdoc` from a rendering and transforms it to strict text.

# Render directly to `stdout`

renderIO :: Handle -> SimpleDocStream AnsiStyle -> IO () #

`(`renderIO` h sdoc)` writes `sdoc` to the handle `h`.

    >>> let render = renderIO System.IO.stdout . layoutPretty defaultLayoutOptions
    >>> let doc = annotate (color Red) ("red" <+> align (vsep [annotate (color Blue <> underlined) ("blue+u" <+> annotate bold "bold" <+> "blue+u"), "red"]))

We render the `unAnnotate`d version here, since the ANSI codes don’t display well in Haddock,

    >>> render (unAnnotate doc)
    red blue+u bold blue+u
        red

This function behaves just like

    renderIO h sdoc = hPutStr h (renderLazy sdoc)

but will not generate any intermediate text, rendering directly to the handle.

## Convenience functions

putDoc :: Doc AnsiStyle -> IO () #

`(`putDoc` doc)` prettyprints document `doc` to standard output using `defaultLayoutOptions`.

    >>> putDoc ("hello" <+> "world")
    hello world

    putDoc = hPutDoc stdout

hPutDoc :: Handle -> Doc AnsiStyle -> IO () #

Like `putDoc`, but instead of using `stdout`, print to a user-provided handle, e.g. a file or a socket using `defaultLayoutOptions`.

    main = withFile "someFile.txt" (\h -> hPutDoc h (vcat ["vertical", "text"]))

    hPutDoc h doc = renderIO h (layoutPretty defaultLayoutOptions doc)