Terminalwire.Server.Context (Terminalwire v0.1.0)

Copy Markdown View Source

The server's handle on the client's terminal — the API your CLI code calls. Output (puts/print/warn) is fire-and-forget; input and resource ops (gets, read_secret, file/env reads) are synchronous request/response. Mirrors Terminalwire::V2::Server::Context.

def run(ctx) do
  Terminalwire.Server.Context.puts(ctx, "Deploying…")
  name = Terminalwire.Server.Context.gets(ctx, "Environment? ")
  Terminalwire.Server.Context.puts(ctx, "→ #{name}")
  0
end

Summary

Functions

The program name + args the client launched with.

End the session with an exit status (defaults to 0).

Read keystrokes for the duration of fun: the client puts its terminal in mode ("raw" or "cbreak") and streams keypresses, restoring it when the block exits. fun receives a 0-arity reader returning the next chunk (nil at close).

Drain the client's stdin to EOF (for piped input).

Pull up to n bytes from the client's stdin. Returns {data, eof}.

Read a single keypress (cbreak: echo + signal keys on). Returns the chunk.

Lower-level: returns {:ok, value} | {:error, code, message}.

The client's terminal as a Terminalwire.Server.Terminal struct (cols, rows, tty?, …), so server code can do Context.terminal(ctx).cols instead of digging through the raw handshake map.

Types

t()

@type t() :: %Terminalwire.Server.Context{
  capabilities: term(),
  entitlement: term(),
  program: term(),
  session: term(),
  terminal: term()
}

Functions

args(arg1)

The program name + args the client launched with.

browser_launch(ctx, url)

dir_create(ctx, path)

dir_delete(ctx, path)

dir_exists?(ctx, path)

dir_list(ctx, path)

env(ctx, name)

exit(ctx, status \\ 0)

End the session with an exit status (defaults to 0).

file_append(ctx, path, content)

file_delete(ctx, path)

file_exists?(ctx, path)

file_read(ctx, path)

file_write(ctx, path, content)

gets(ctx, prompt \\ nil)

print(ctx, data)

puts(ctx, data \\ "")

raw_input(ctx, fun)

Read keystrokes for the duration of fun: the client puts its terminal in mode ("raw" or "cbreak") and streams keypresses, restoring it when the block exits. fun receives a 0-arity reader returning the next chunk (nil at close).

raw_input(ctx, mode, fun)

read(ctx)

Drain the client's stdin to EOF (for piped input).

read_chunk(ctx, n \\ 65536)

Pull up to n bytes from the client's stdin. Returns {data, eof}.

read_key(ctx, mode \\ "cbreak")

Read a single keypress (cbreak: echo + signal keys on). Returns the chunk.

read_secret(ctx, prompt \\ nil)

request(ctx, resource, method, params, timeout \\ 300_000)

Lower-level: returns {:ok, value} | {:error, code, message}.

terminal(context)

The client's terminal as a Terminalwire.Server.Terminal struct (cols, rows, tty?, …), so server code can do Context.terminal(ctx).cols instead of digging through the raw handshake map.

warn(ctx, data \\ "")