wsowens / term / Term.ANSI

Parsing Strings with ANSI-escape codes.

This module contains functions to parse strings with ANSI-encoded data. The main function for this purpose is parseEscaped:

parseEscaped : Maybe Format -> String -> Buffer msg

Parse and process a stream of ANSI-escaped data. Optionally, a starting Format state may be provided. If Nothing is provided, all SGR commands will be ignored. (Provide Just defaultFormat to enable formatting but start with the default Format state.)


type alias Buffer msg =
{ nodes : List (Html msg)
, format : Maybe Format 
}

An ANSI.Buffer contains a stack of processed HTML nodes and possibly a Format state. If Nothing is provided for the format state, then no formats will be stored in the Buffer and all SGR commands will be ignored.

Formatting

This package supports most of the basic SGR function parameters. All of these possible configurations are represented by the Format record:


type alias Format =
{ foreground : Color
, background : Color
, bold : Basics.Bool
, italic : Basics.Bool
, underline : Basics.Bool
, strike : Basics.Bool
, blink : Basics.Bool
, reverse : Basics.Bool 
}

At any time, the terminal emulator can be represented by record above. The basic text decoration attributes (bold, italics, underline, etc.), are all represented by basic booleans. You can update these using the record update syntax: blinkingItalicFormat = {defaultFormat | italic = True, blink = True} The foreground and background fields are represented by the Color type, which we discuss in detail below.


type Color
    = Black
    | Red
    | Green
    | Yellow
    | Blue
    | Magenta
    | Cyan
    | White
    | Default
    | BrightBlack
    | BrightRed
    | BrightGreen
    | BrightYellow
    | BrightBlue
    | BrightMagenta
    | BrightCyan
    | BrightWhite

Type representing the color of text. Currently, the basic 8 colors are supported (for SGR codes 30-37 and 40-47) as well as the 8 nonstandard bright colors (for SGR codes 90-97 and 100-107).

Note that, since these colors are rendered as simple CSS classes, you can write your own CSS files if you want to produce your own color scheme.

defaultFormat : Format

The default Format for text. The Default foreground and background colors refer to SGR codes 39 and 49, respectively. In practice, these codes usually mean white for the foreground, and black for the background.

format : Format -> String -> Html msg

Apply a Format to a string of content, producing an HTML node.