vito/elm-ansi/9.0.2

NOTE: the latest version is 12.0.1

Ansi.Log

Log interprets a stream of text and ANSI escape codes.

init : LineDiscipline -> Model

Construct an empty model.

update : String -> Model -> Model

Parse and interpret a chunk of ANSI output.

Trailing partial ANSI escape codes will be prepended to the chunk in the next call to update.

view : Model -> Html x

Render the model's logs as HTML.

Wraps a <pre> around the the result of calling viewLine for each line.

As a cheap optimization, each line is rendered lazily.

viewLine : Line -> Html x

Render an individual line as HTML.

The line is rendered as a <div> containing <span> elements with styling and classes for each Chunk.

The span elements will have the following attributes:

  • style="font-weight: bold|normal"
  • class="ansi-COLOR-fg ansi-COLOR-bg ansi-bold"

...where each class is optional, and COLOR is one of:

  • black
  • red
  • green
  • yellow
  • blue
  • magenta
  • cyan
  • white
  • bright-black
  • bright-red
  • bright-green
  • bright-yellow
  • bright-blue
  • bright-magenta
  • bright-cyan
  • bright-white

If the chunk is inverted, the -fg and -bg classes will have their colors swapped. If the chunk is bold, the ansi-bold class will be present.

type alias Model =
{ lineDiscipline : LineDiscipline
, lines : Array Line
, position : CursorPosition
, savedPosition : Maybe CursorPosition
, style : Style
, remainder : String
}

Model is populated by parsing ANSI character sequences and escape codes via update.

  • lines contains all of the output that's been parsed
  • position is the current position of the cursor
  • style is the style to be applied to any text that's printed
  • remainder is a partial ANSI escape sequence left around from an incomplete segment from the stream
= Raw
| Cooked

How to interpret linebreaks.

  • Raw: interpret \n as just \n, i.e. move down a line, retaining the cursor column
  • Cooked: interpret \n as \r\n, i.e. move down a line and go to the first column
type alias Line =
( List Chunk, Int )

A list of arbitrarily-sized chunks of output.

type alias Chunk =
{ text : String
, style : Style
}

A blob of text paired with the style that was configured at the time.

type alias CursorPosition =
{ row : Int
, column : Int
}

The coordinate in the window where text will be printed.

type alias Style =
{ foreground : Maybe Color
, background : Maybe Color
, bold : Bool
, faint : Bool
, italic : Bool
, underline : Bool
, blink : Bool
, inverted : Bool
, fraktur : Bool
, framed : Bool
}

The current presentation state for any text that's printed.