module

Term2::Model

Model contains the program's state as well as its core functions.

Any type that includes this module and implements the required methods can be used as a model. This mirrors Go's interface-based approach where any struct with the right methods implements Model.

class Counter
  include Term2::Model

  getter count : Int32 = 0

  def init : Term2::Cmd
    nil # no initial command
  end

  def update(msg : Term2::Msg) : {Term2::Model, Term2::Cmd}
    case msg
    when Term2::KeyMsg
      case msg.key.to_s
      when "q" then {self, Term2.quit}
      when "+" then {Counter.new(@count + 1), nil}
      else          {self, nil}
      end
    else
      {self, nil}
    end
  end

  def view : String
    "Count: #{@count}"
  end
end

Instance methods

blur

Blur (unfocus) this model's zone.

Source
focus

Focus this model's zone.

Source
focused?

Whether this model is currently focused.

Source
init

Init is the first function that will be called. It returns an optional initial command. To not perform an initial command return nil.

Source
update(msg : Msg) : Tuple(Model, Cmd)

Update is called when a message is received. Use it to inspect the message and, in response, update the model and/or send a command.

Source
view

View renders the program's UI, which is just a string. The view is rendered after every Update.

Source
zone_id

Zone ID for this model (used by BubbleZone for focus/click tracking). Override this to provide a custom zone ID.

Source