vito / elm-ansi / 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

 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

containing 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


type LineDiscipline
    = 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, Basics.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 : Basics.Int
, column : Basics.Int 
}

The coordinate in the window where text will be printed.


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

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