| Copyright | Daniel Mendler (c) 2017 |
|---|---|
| License | MIT (see the file LICENSE) |
| Maintainer | mail@daniel-mendler.de |
| Stability | experimental |
| Portability | portable |
| Safe Haskell | Safe |
| Language | Haskell2010 |
Data.Monoid.Colorful
Contents
Description
This library provides styled text output using ANSI escape sequences. The colored text is modeled as nested Colored values, which form a Monoid. As a result the colored code has a relatively concise form.
For rendering, the Colored Monoid is flattended and then printed out. The library keeps track of a stack of the active styles internally, such that correct and minimal escape sequences are generated.
This library is used by wl-pprint-console, which is a pretty printer with support for annotations.
Warning: Windows support is currently not implemented, but is planned (by using ansi-terminal or the ffi).
Example:
basicExample :: IO ()
basicExample = do
term <- getTerm
printColoredS term $ Style Underline (Style Bold "Basic Example\n")
<> Style Bold "Bold"
<> Style Italic (Bg Red "Italic Red")
<> Style Underline "Underline"
putChar '\n'For many more examples, see the example.hs file.
- data Colored a
- data Style
- data Color
- data Term
- hGetTerm :: Handle -> IO Term
- getTerm :: IO Term
- hPrintColored :: (Handle -> a -> IO ()) -> Handle -> Term -> Colored a -> IO ()
- printColored :: (a -> IO ()) -> Term -> Colored a -> IO ()
- hPrintColoredIO :: Handle -> Term -> Colored (IO ()) -> IO ()
- printColoredIO :: Term -> Colored (IO ()) -> IO ()
- hPrintColoredS :: Handle -> Term -> Colored String -> IO ()
- printColoredS :: Term -> Colored String -> IO ()
- showColored :: Monoid o => (a -> o) -> (SGRCode -> o) -> Term -> Colored a -> o
- showColoredM :: (Monad f, Monoid o) => (a -> f o) -> (SGRCode -> f o) -> Term -> Colored a -> f o
- showColoredS :: Term -> Colored String -> ShowS
- (<>) :: Semigroup a => a -> a -> a
The Monoid
Colored Monoid
Constructors
| Nil | |
| Value a | |
| Style !Style (Colored a) | |
| Unstyle !Style (Colored a) | |
| Fg !Color (Colored a) | |
| Bg !Color (Colored a) | |
| Pair (Colored a) (Colored a) |
Instances
| Monad Colored Source # | Free monad! |
| Functor Colored Source # | |
| Applicative Colored Source # | Free monad! |
| Foldable Colored Source # | |
| Traversable Colored Source # | |
| IsList (Colored a) Source # | |
| Eq a => Eq (Colored a) Source # | |
| Ord a => Ord (Colored a) Source # | |
| Read a => Read (Colored a) Source # | |
| Show a => Show (Colored a) Source # | |
| IsString a => IsString (Colored a) Source # | |
| Generic (Colored a) Source # | |
| Semigroup (Colored a) Source # | |
| Monoid (Colored a) Source # | |
| Generic1 * Colored Source # | |
| type Rep (Colored a) Source # | |
| type Item (Colored a) Source # | |
| type Rep1 * Colored Source # | |
Rendering style
Named colors, 256 and RGB colors for more capable terminals.
Constructors
| DefaultColor | Default terminal color (terminal specific) |
| Black | |
| Red | |
| Green | |
| Yellow | |
| Blue | |
| Magenta | |
| Cyan | |
| White | |
| DullBlack | |
| DullRed | |
| DullGreen | |
| DullYellow | |
| DullBlue | |
| DullMagenta | |
| DullCyan | |
| DullWhite | |
| Color256 !Word8 | Color from 256 color scheme. Color is automatically reduced to 8 colors for less capable terminals. |
| RGB !Word8 !Word8 !Word8 | True color. Color is automatically reduced to 256 or 8 colors for less capable terminals. |
Terminal type
Terminal type. For less capable terminals the color depth is automatically reduced.
hGetTerm :: Handle -> IO Term Source #
The action (hGetTerm handle) determines the terminal type of the file handle.
The terminal type is determined by checking if the file handle points to a device
and by looking at the $TERM environment variable.
Colorful printing to file handle
Show with ANSI escape sequences
showColoredM :: (Monad f, Monoid o) => (a -> f o) -> (SGRCode -> f o) -> Term -> Colored a -> f o Source #