Safe Haskell | Safe |
---|---|
Language | Haskell2010 |
Render SimpleDocStream
in a terminal.
- data AnsiStyle
- data Color
- color :: Color -> AnsiStyle
- colorDull :: Color -> AnsiStyle
- bgColor :: Color -> AnsiStyle
- bgColorDull :: Color -> AnsiStyle
- bold :: AnsiStyle
- italicized :: AnsiStyle
- underlined :: AnsiStyle
- data Intensity
- 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
The 8 ANSI terminal colors.
Font color
Background color
bgColorDull :: Color -> AnsiStyle Source #
Style the background with a dull color.
Font style
italicized :: AnsiStyle Source #
Render in italics.
underlined :: AnsiStyle Source #
Render underlined.
Internal markers
These should only be used for writing adaptors to other libraries; for
the average use case, use bold
, bgColorDull
, etc.
Dull or vivid coloring, as supported by ANSI terminals.
data Underlined Source #
data Italicized Source #
Conversion to ANSI-infused Text
renderLazy :: SimpleDocStream AnsiStyle -> 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 = 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 Source #
(
takes the output renderStrict
sdoc)sdoc
from a rendering and
transforms it to strict text.
Render directly to stdout
renderIO :: Handle -> SimpleDocStream AnsiStyle -> IO () Source #
(
writes renderIO
h sdoc)sdoc
to the file h
.
>>>
renderIO System.IO.stdout (layoutPretty defaultLayoutOptions "hello\nworld")
hello world
Convenience functions
putDoc :: Doc AnsiStyle -> IO () Source #
(
prettyprints document putDoc
doc)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)