vgrep-0.2.2.0: A pager for grep

Safe HaskellNone
LanguageHaskell2010

Vgrep.Ansi

Contents

Description

Utilities for printing ANSI formatted text.

Synopsis

ANSI formatted text

type AnsiFormatted = Formatted Attr Source #

Type alias for Text formatted with Attr from Graphics.Vty.

data Formatted attr Source #

A representattion of formatted Text. The attribute is usually a Monoid so that different formattings can be combined by nesting them.

Instances

Functor Formatted Source # 

Methods

fmap :: (a -> b) -> Formatted a -> Formatted b #

(<$) :: a -> Formatted b -> Formatted a #

Eq attr => Eq (Formatted attr) Source # 

Methods

(==) :: Formatted attr -> Formatted attr -> Bool #

(/=) :: Formatted attr -> Formatted attr -> Bool #

Show attr => Show (Formatted attr) Source # 

Methods

showsPrec :: Int -> Formatted attr -> ShowS #

show :: Formatted attr -> String #

showList :: [Formatted attr] -> ShowS #

(Eq attr, Monoid attr) => Monoid (Formatted attr) Source # 

Methods

mempty :: Formatted attr #

mappend :: Formatted attr -> Formatted attr -> Formatted attr #

mconcat :: [Formatted attr] -> Formatted attr #

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"

cat :: (Eq attr, Monoid attr) => [Formatted attr] -> Formatted attr Source #

Concatenates pieces of Formatted text. Redundant formattings and blocks of equal formatting are fused together.

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.

>>> renderAnsi Vty.defAttr (bare "Text")
HorizText "Text"@(Attr {attrStyle = Default, attrForeColor = Default, attrBackColor = Default, attrURL = Default},4,4)

stripAnsi :: Formatted a -> Text Source #

Strips away all formattings to plain Text.