mthadley / elm-typewriter / Typewriter

This module follows the Elm Architecture. If you are new, it's definitely a good idea to start by reading the Official Guide.

Basics


type Model

Opaque type representing the current state of the typewriter.


type Config

The settings for the typewriter. You can create one of these using withWords, and then customize it using functions like iterations. You can then initialize your typewriter by passing your Config to init.

Typewriter.withWords [ "Supercalifragilisticexpialidocious" ]
    |> Typewriter.iterations (Typewriter.times 3)
    |> Typewriter.withTypeDelay 600
    |> Typewriter.withBackspaceDelay 10
    |> Typewriter.withJitter (Random.float 0.5 1.5)
    |> Typewriter.init

See Customizing.

withWords : List String -> Config

Initialize your typewriter Config with the words you want it to type. This is the only way to create a Config!

init : Config -> ( Model, Platform.Cmd.Cmd Msg )

Create a new typewriter model from some settings.

Typewriter.withWords [ "Let's get typing!" ]
    |> Typwriter.init

See Config for more ways to customize it's behavior.

view : Model -> String

The view function. We just give you the String, so you can render it in whatever way makes sense for your application.


type Msg

The type of messages that typewriters emit.

update : Msg -> Model -> ( Model, Platform.Cmd.Cmd Msg )

Make sure to call this update in your own update to make the typewriter type!

Customizing

iterations : Iterations -> Config -> Config

Controls how many times the typwriter will run through it's script of words. By default it will type infinitely, but you can also constrain it. See times.

Typewriter.withWords [ "Type", "Forever" ]
    |> Typwriter.iterations Typewriter.infinite
    |> Typwriter.init

withTypeDelay : Basics.Int -> Config -> Config

Change the amount of time between each key typed. This should specified in milliseconds.

Note that this only affects how fast characters are deleted, and not how fast they are deleted. For that, see withBackspaceDelay.

withBackspaceDelay : Basics.Int -> Config -> Config

Change the amount of time between each character deleted. This should specified in milliseconds.

withJitter : Random.Generator Basics.Float -> Config -> Config

Provide a generator that produces floats, that will be used to calculate the delay in the next step of the typing process. The float is used as a coefficient with the delay configured for that step.

If generator produces 0.5, then the next step will be twice as fast. Conversely, if it were to produce 2.0, then that next step will take twice as long.

Typewriter.withWords [ "I'm", "All", "Over", "The", "Place" ]
    |> Typwriter.withJitter (Random.float 0.1 20)
    |> Typwriter.init


type Iterations

Controls how many times the typewriter will play.

infinite : Iterations

The typewriter will continuously type it's list of words, starting from the beginning after it finishes it's last word.

times : Basics.Int -> Iterations

The typewriter will go through it's list of words a specified amount of times, and then stop. There is a minimum of at least one iteration, so for any value n where n < 1, it will be treated as 1.

Utilities

togglePlay : Model -> ( Model, Platform.Cmd.Cmd Msg )

Toggles the playing state of the typewriter. If it is currently playing, then this will pause it. If it is already paused, then it will resume playing.

You can check if it is currently paused or playing using isPaused.

isPaused : Model -> Basics.Bool

Returns true if the typewriter is paused.

Typewriter.withWords [ "Type", "Forever" ]
    |> Typwriter.init
    |> Typewriter.isPaused
    |> Expect.equal False

isDone : Model -> Basics.Bool

Retuorns true if this typewriter has no more things to type! This will never return false if the iterations was set to infinite.

restart : Model -> ( Model, Platform.Cmd.Cmd Msg )

Sets the typewriter back to it's original state. Useful if you have a typewriter that is "done", and you want it to start typing from the beginning.