| Safe Haskell | None |
|---|---|
| Language | Haskell2010 |
Vgrep.Ansi
Description
Utilities for printing ANSI formatted text.
Synopsis
- type AnsiFormatted = Formatted Attr
- data Formatted attr
- emptyFormatted :: Formatted attr
- bare :: Text -> Formatted attr
- format :: (Eq attr, Monoid attr) => attr -> Formatted attr -> Formatted attr
- cat :: (Eq attr, Monoid attr) => [Formatted attr] -> Formatted attr
- mapText :: (Text -> Text) -> Formatted a -> Formatted a
- mapTextWithPos :: (Int -> Text -> Text) -> Formatted a -> Formatted a
- takeFormatted :: Int -> Formatted a -> Formatted a
- dropFormatted :: Int -> Formatted a -> Formatted a
- padFormatted :: Int -> Char -> Formatted a -> Formatted a
- renderAnsi :: Attr -> AnsiFormatted -> Image
- stripAnsi :: Formatted a -> Text
ANSI formatted text
type AnsiFormatted = Formatted Attr Source #
Type alias for Text formatted with Attr from Graphics.Vty.
A representattion of formatted Text. The attribute is usually a Monoid
so that different formattings can be combined by nesting them.
Smart constructors
emptyFormatted :: Formatted attr Source #
Smart constructor for an empty Formatted text.
bare :: Text -> Formatted attr Source #
Smart constructor for bare (unformatted) text.
>>>bare ""Empty
>>>bare "Text"Text 4 "Text"
format :: (Eq attr, Monoid attr) => attr -> Formatted attr -> Formatted attr Source #
Adds formatting to a Formatted text. The Eq and Monoid instances for
attr are used to flatten redundant formattings.
>>>format (Just ()) (format Nothing (bare "Text"))Format 4 (Just ()) (Text 4 "Text")
>>>format (Just ()) (format (Just ()) (bare "Text"))Format 4 (Just ()) (Text 4 "Text")
>>>format Nothing (bare "Text")Text 4 "Text"
Modifying text nodes
mapText :: (Text -> Text) -> Formatted a -> Formatted a Source #
Apply a function to each piece of text in the Formatted tree.
>>>mapText T.toUpper (Cat 11 [Text 6 "Hello ", Format 5 () (Text 5 "World")])Cat 11 [Text 6 "HELLO ",Format 5 () (Text 5 "WORLD")]
mapTextWithPos :: (Int -> Text -> Text) -> Formatted a -> Formatted a Source #
Like mapText, but passes the position of the text chunk to the function
as well. Can be used for formatting text position-dependently, e.g. for
expanding tabs to spaces.
takeFormatted :: Int -> Formatted a -> Formatted a Source #
Crops the text to a given length. If the text is already shorter than the desired width, it is returned as-is.
dropFormatted :: Int -> Formatted a -> Formatted a Source #
Drops a prefix of the given length. If the text is already shorter than the
number of characters to be dropped, emptyFormatted is returned.
padFormatted :: Int -> Char -> Formatted a -> Formatted a Source #
Pads the text to a given width. If the text is already longer than the desired width, it is returned as-is.
Converting ANSI formatted text
renderAnsi :: Attr -> AnsiFormatted -> Image Source #
Converts ANSI formatted text to an Image. Nested formattings are
combined with combineStyles. The given Attr is used as style for the
root of the Formatted tree.
>>>import Graphics.Vty.Image.Internal (Image (HorizText, attr))>>>let HorizText { attr = attr } = renderAnsi Vty.defAttr (bare "Text")>>>attrAttr {attrStyle = Default, attrForeColor = Default, attrBackColor = Default, attrURL = Default}