prettyprinter-ansi-terminal-0.1: ANSI terminal backend for the modern, extensible and well-documented prettyprinter.

Safe HaskellSafe
LanguageHaskell2010

Data.Text.Prettyprint.Doc.Render.Terminal

Contents

Description

Render SimpleDoc in a terminal.

Synopsis

Styling

data Color Source #

8 different colors, so that all can be displayed in an ANSI terminal.

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 #

Font color

color :: Color -> Doc AnsiTerminal -> Doc AnsiTerminal Source #

Style the foreground with a vivid color.

colorDull :: Color -> Doc AnsiTerminal -> Doc AnsiTerminal Source #

Style the foreground with a dull color.

Background color

bgColor :: Color -> Doc AnsiTerminal -> Doc AnsiTerminal Source #

Style the background with a vivid color.

bgColorDull :: Color -> Doc AnsiTerminal -> Doc AnsiTerminal Source #

Style the background with a dull color.

Font style

bold :: Doc AnsiTerminal -> Doc AnsiTerminal Source #

Render the enclosed document in bold.

italics :: Doc AnsiTerminal -> Doc AnsiTerminal Source #

Render the enclosed document in italics.

underline :: Doc AnsiTerminal -> Doc AnsiTerminal Source #

Render the enclosed document underlined.

Conversion to ANSI-infused Text

renderLazy :: SimpleDoc AnsiTerminal -> 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 = color Red ("red" <+> align (vsep [color Blue ("blue" <+> bold "bold" <+> "blue"), "red"]))
>>> render (unAnnotate doc)
red blue bold blue
    red
>>> render doc
\e[0;91mred \e[0;94mblue \e[0;94;1mbold\e[0;94m blue\e[0;91m
    red\e[0m

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

renderStrict :: SimpleDoc AnsiTerminal -> Text Source #

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

Render directly to stdout

renderIO :: Handle -> SimpleDoc AnsiTerminal -> IO () Source #

(renderIO h sdoc) writes sdoc to the file h.

>>> renderIO System.IO.stdout (layoutPretty defaultLayoutOptions "hello\nworld")
hello
world

Convenience functions

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

(putDoc doc) prettyprints document doc to standard output, with a page width of 80 characters and a ribbon width of 32 characters.

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

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

Like putDoc, but instead of using stdout, print to a user-provided handle, e.g. a file or a socket. Uses a line length of 80, and a ribbon width of 32 characters.

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