Safe Haskell | Safe |
---|---|
Language | Haskell2010 |
Render SimpleDocStream
in a terminal.
- data AnsiTerminal
- data Color
- data Intensity
- data Layer
- color :: Color -> Doc AnsiTerminal -> Doc AnsiTerminal
- colorDull :: Color -> Doc AnsiTerminal -> Doc AnsiTerminal
- bgColor :: Color -> Doc AnsiTerminal -> Doc AnsiTerminal
- bgColorDull :: Color -> Doc AnsiTerminal -> Doc AnsiTerminal
- bold :: Doc AnsiTerminal -> Doc AnsiTerminal
- italics :: Doc AnsiTerminal -> Doc AnsiTerminal
- underline :: Doc AnsiTerminal -> Doc AnsiTerminal
- renderLazy :: SimpleDocStream AnsiTerminal -> Text
- renderStrict :: SimpleDocStream AnsiTerminal -> Text
- renderIO :: Handle -> SimpleDocStream AnsiTerminal -> IO ()
- putDoc :: Doc AnsiTerminal -> IO ()
- hPutDoc :: Handle -> Doc AnsiTerminal -> IO ()
Styling
data AnsiTerminal Source #
8 different colors, so that all can be displayed in an ANSI terminal.
Dull or vivid coloring, as supported by ANSI terminals.
Foreground (text) or background (paper) color
Font color convenience definitions
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 convenience definitions
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 convenience definitions
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 :: SimpleDocStream AnsiTerminal -> Text Source #
(
takes the output renderLazy
doc)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 :: SimpleDocStream AnsiTerminal -> Text Source #
(
takes the output renderStrict
sdoc)sdoc
from a rendering and
transforms it to strict text.
Render directly to stdout
renderIO :: Handle -> SimpleDocStream AnsiTerminal -> IO () Source #
(
writes renderIO
h sdoc)sdoc
to the file h
.
>>>
renderIO System.IO.stdout (layoutPretty defaultLayoutOptions "hello\nworld")
hello world
Convenience functions
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)