wsowens / term / Term

This module contains the basic term type.

Definition


type alias Term msg =
{ status : Maybe Connection
, log : Array (Html msg)
, format : Maybe ANSI.Format
, events : Handlers msg 
}

A Term is the main data structure exposed by this package. A Term can receive ANSI-encoded messages, convert user input into new Msgs, and quickly render a chat log. The fields of a Term are as follows: - status any current information about the connection - log stores all of the previously received messages (already encoded into HTML) - format stores the current format that the Term will apply to incoming messages - events stores functions to trigger if the user attempts to submit input or connect to a new address Rather than create a Term directly, I recommend using the functions in the section below.


type Connection
    = Open String
    | Connecting String
    | Closed

A Connection can be used to information about what a Term is connected to.

Creating Terms

withFormat : ANSI.Format -> Term msg

Create a formatted Term that does not accept any user input or display a URL bar. This may be useful if you just want to decorate your website or have a cool way to display incoming messages.

When selecting a format you can either use the default or build off of it.

Term.withFormat ANSI.defaultFormat -- creates a default-formatted term

greenBold = {defaultFormat | foreground = ANSI.Green, bold = True }
Term.withFormat greenBold -- creates a Term with green, bold text

offline : Maybe ANSI.Format -> (String -> msg) -> Term msg

Create a Term that has full formatting and user input, but doesn't display the URL bar. This may be useful if you want to create an interactive terminal for your user, but you don't need the users to connect to any websites.

fmt is an optional ANSI.format (see withFormat above). (Note, you can pass Nothing as the format to disable formatting entirely.) onInput should be a Msg that accepts some type of String. Once the user types something in the terminal and presses enter, a Msg of type onInput will fire.

new : Maybe Connection -> Maybe ANSI.Format -> (String -> msg) -> (String -> msg) -> Term msg

Create a Term with complete configuration. For details on fmt and onInput, see withFormat above. This function also accepts another Msg type, onConnect. Similar to onInput, whatever Msg you provide for onConnect will fire when the user presses enter after typing in the URL bar.

If status is Nothing, the URL bar will be hidden entirely. Otherwise, you can use status to store information about the current connection.

Using Terms

receive : String -> Term msg -> Term msg

Send an ANSI-escaped message to the Term. Produces a term with an updated list of messages and updated format state.

render : Term msg -> Html msg

Emits HTML code for a Term. This can be used in a typical view function for your application, for example:

view : Model -> Html.Html msg
view model =
  Html.div [ ]
    [ Html.h1 [ ] [ Html.text "check out my cool terminal:"]
    , Term.render model.term
    ]