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

Safe HaskellSafe
LanguageHaskell2010

Data.Text.Prettyprint.Doc.Render.Terminal.Internal

Description

Warning: Internal module. May change arbitrarily between versions.

Synopsis

Documentation

(Definitions for the doctests)

>>> :set -XOverloadedStrings
>>> import qualified Data.Text.Lazy.IO as TL
>>> import qualified Data.Text.Lazy as TL
>>> import Data.Text.Prettyprint.Doc.Render.Terminal

data Color Source #

The 8 ANSI terminal colors.

Constructors

Black 
Red 
Green 
Yellow 
Blue 
Magenta 
Cyan 
White 

Instances

Eq Color Source # 

Methods

(==) :: Color -> Color -> Bool #

(/=) :: Color -> Color -> Bool #

Ord Color Source # 

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 #

Show Color Source # 

Methods

showsPrec :: Int -> Color -> ShowS #

show :: Color -> String #

showList :: [Color] -> ShowS #

data Intensity Source #

Dull or vivid coloring, as supported by ANSI terminals.

Constructors

Vivid 
Dull 

data Layer Source #

Foreground (text) or background (paper) color

Constructors

Foreground 
Background 

Instances

Eq Layer Source # 

Methods

(==) :: Layer -> Layer -> Bool #

(/=) :: Layer -> Layer -> Bool #

Ord Layer Source # 

Methods

compare :: Layer -> Layer -> Ordering #

(<) :: Layer -> Layer -> Bool #

(<=) :: Layer -> Layer -> Bool #

(>) :: Layer -> Layer -> Bool #

(>=) :: Layer -> Layer -> Bool #

max :: Layer -> Layer -> Layer #

min :: Layer -> Layer -> Layer #

Show Layer Source # 

Methods

showsPrec :: Int -> Layer -> ShowS #

show :: Layer -> String #

showList :: [Layer] -> ShowS #

data Bold Source #

Constructors

Bold 

Instances

Eq Bold Source # 

Methods

(==) :: Bold -> Bold -> Bool #

(/=) :: Bold -> Bold -> Bool #

Ord Bold Source # 

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 #

Show Bold Source # 

Methods

showsPrec :: Int -> Bold -> ShowS #

show :: Bold -> String #

showList :: [Bold] -> ShowS #

color :: Color -> AnsiStyle Source #

Style the foreground with a vivid color.

bgColor :: Color -> AnsiStyle Source #

Style the background with a vivid color.

colorDull :: Color -> AnsiStyle Source #

Style the foreground with a dull color.

bgColorDull :: Color -> AnsiStyle Source #

Style the background with a dull color.

bold :: AnsiStyle Source #

Render in bold.

italicized :: AnsiStyle Source #

Render in italics.

underlined :: AnsiStyle Source #

Render underlined.

renderLazy :: SimpleDocStream AnsiStyle -> Text Source #

(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.

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

(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 unAnnotated 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.

>>> 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"]))
>>> render (unAnnotate doc)
red blue+u bold blue+u
    red

This test won’t work since I don’t know how to type ESC for doctest :-/ -- >>> render doc -- ESC[0;91mred ESC[0;94;4mblue+u ESC[0;94;1;4mboldESC[0;94;4m blue+uESC[0;91m -- redESC[0m

data AnsiStyle Source #

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

Instances

Eq AnsiStyle Source # 
Ord AnsiStyle Source # 
Show AnsiStyle Source # 
Semigroup AnsiStyle Source #

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.

Monoid AnsiStyle Source #

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.

renderStrict :: SimpleDocStream AnsiStyle -> Text Source #

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

putDoc :: Doc AnsiStyle -> IO () Source #

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

>>> putDoc ("hello" <+> "world")
hello world
putDoc = hPutDoc stdout

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

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)