/ic-mini-terminal

Internet Computer Mini Terminal

Primary LanguageRustApache License 2.0Apache-2.0

IC Mini Terminal (ic-mt)

Minimal keyboard input (⌨) and graphical output (📺) for programs on the Internet Computer.

Example

The Motoko package icmt (in this repo, under src) provides a simple framework for building services that interact with an icmt instance.

Below, we show an interactive Counter example.

class Counter(initCount : Nat) {
  public var count = initCount;

  public func clone () : Counter {
    Counter(count)
  };

  public func draw (d: Dim) : Types.Graphics.Elm {
    let r = Render.Render();
    let atts = Style.txtAtts(d.width / 256 + 1);
    let cr = Render.CharRender(r, Mono5x5.bitmapOfChar, atts);
    let tr = Render.TextRender(cr);
    tr.textAtts("count = " # Nat.toText(count), atts);
    r.getElm()
  };

  public func update (e : EventInfo) {
    switch (e.event) {
      case (#keyDown(keys)) {
        for (k in keys.vals()) {
          switch (k.key) {
            case ("=" or "+" or "ArrowUp" or "ArrowRight") {
              count += 1;
            };
            case ("-" or "_" or "ArrowDown" or "ArrowLeft") {
              if (count > 0) { count -= 1 };
            };
            case _ { /* ignore key */ };
          }
        }
      };
      case _ { /* ignore event */ };
    }
  };
};

The full example uses this class to instantiate the terminal/Terminal.Basic class, which adapts the simple (single-event) draw-update protocol shown below to that of the "full" icmt service protocol.

Technical spec

The ic-mt tool talks to any service on the Internet Computer that uses the mini-terminal update-view service protocol, given below in Candid syntax (eliding message-type details):

service : {
  view: (Dim, vec EventInfo) -> (Graphics) query;
  update: (vec EventInfo, GraphicsRequest) -> (vec Graphics);
}

See the full Candid spec, and general docs for Candid for further details.

Building and testing

Dependencies:

The mini terminal is a Rust project.

We typically use dfx to run the Internet Computer services (e.g., within a local replica) to run applications for the terminal.

We often write these applications in Motoko.

Inspired by